h. Program to illustrate the use of JComboBox.

h. Program to illustrate the use of JComboBox.

CODE:

package swing_practicals;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Combo2 extends JFrame implements ActionListener{

String cities[] = {"Bengaluru","Mumbai","Hydrabad","Chennai"};

JComboBox cbl = new JComboBox(cities);

JTextField txt1 = new JTextField(20);

JButton btn1 = new JButton("Add");

JLabel lbl = new JLabel("");

Container c;

Combo2(String s){

super(s);

setVisible(true);

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

c = getContentPane();

c.setLayout(new FlowLayout());

c.add(txt1);

c.add(btn1);

c.add(cbl);

c.add(lbl);

cbl.addActionListener(this);

btn1.addActionListener(this);

}

public void actionPerformed(ActionEvent ae){

if(ae.getSource() == cbl){

String s = cbl.getSelectedItem().toString();

txt1.setText(s);

}

if (ae.getSource() == btn1) {

if (txt1.getText().equals(" ")) {

 

lbl.setText("No item to add.");

}

else{

cbl.addItem(txt1.getText());

txt1.setText(" ");

}

}

}

public static void main(String[] args) {

Combo2 cb = new Combo2("ComboBox");

}

}

OUTPUT:

Programs based on AWT and Swing.Programs based on AWT and Swing.



Post a Comment

0 Comments