0% found this document useful (0 votes)
34 views

Assignment #2

This document contains code for a Java program that demonstrates adding and removing items from combo boxes. It creates two combo boxes and buttons to add or remove individual items or all items from one combo box to the other. The code imports necessary libraries, creates the frame and panels, initializes the combo boxes and buttons, and adds action listeners to the buttons to handle item addition and removal.

Uploaded by

Esha Safdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment #2

This document contains code for a Java program that demonstrates adding and removing items from combo boxes. It creates two combo boxes and buttons to add or remove individual items or all items from one combo box to the other. The code imports necessary libraries, creates the frame and panels, initializes the combo boxes and buttons, and adds action listeners to the buttons to handle item addition and removal.

Uploaded by

Esha Safdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT

#2

SUBJECT: ADV-OOP 2
SUBMITTED TO: SIR SHERAZ HASAN
SUBMITTED BY: ESHA SAFDAR
CLASS CODE: CU-576-2017
DATED: 27 MAY 2019
CODE:
import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JComboBox;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JLabel;

import java.awt.Font;

public class ListDemo extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ListDemo frame = new ListDemo();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}
}

});

public ListDemo() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JComboBox List1 = new JComboBox();

List1.setModel(new DefaultComboBoxModel(new String[] {"Beige", "Teal",


"Black", "Purple", "Azure", "Russet", "Turquoise"}));

List1.setBounds(71, 72, 103, 20);

contentPane.add(List1);

JComboBox List2 = new JComboBox();

List2.setBounds(206, 72, 107, 20);

contentPane.add(List2);

JButton Add1 = new JButton("Add Item");

Add1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{
List2.insertItemAt("Beige",0);

System.out.println("Item added");

});

Add1.setBounds(71, 176, 103, 23);

contentPane.add(Add1);

JButton Add2 = new JButton("Add All");

Add2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0)

List2.insertItemAt("Beige",0);

List2.insertItemAt("Teal",0);

List2.insertItemAt("Black",0);

List2.insertItemAt("Purple",0);

List2.insertItemAt("Azure",0);

List2.insertItemAt("Russet",0);

List2.insertItemAt("Turquoise",0);

System.out.println("All items are added");

});

Add2.setBounds(209, 176, 104, 23);

contentPane.add(Add2);
JButton Rem1 = new JButton("Remove Item");

Rem1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

List2.removeItemAt(0);

System.out.println("Item removed");

});

Rem1.setBounds(71, 227, 103, 23);

contentPane.add(Rem1);

JButton Rem2 = new JButton("Remove All");

Rem2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

List2.removeAllItems();

System.out.println("All items removed");

});

Rem2.setBounds(209, 227, 103, 23);

contentPane.add(Rem2);

JLabel lblcolor = new JLabel("COLORS");

lblcolor.setFont(new Font("Sitka Text", Font.BOLD, 14));

lblcolor.setBounds(176, 11, 75, 14);


contentPane.add(lblcolor);

ECLIPSE VIEW:

You might also like