Tuesday 4 December 2012

JComboBox in Swings

// siddhu vydyabhushana // 2 comments


jcombobox in swings

Hi ,this is siddhu vydyabhushana am going to explain about JComboBox in swings.It is one of the component in swings which gives you to select one from group but it was purely different from JRadioButton.It will provide dropdown for u ,we will take Items in string and put it into the JComboBox.

EventHandling
change in any state of object we known as event.
state changes will done by below function..........




JComboBox j;
j.addItemListener(new ItemListener()
{
public void ItemStateChanged(ItemEvent e)
{
//data what you want
}
});


jcombobox in swings

EXAMPLE:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//class name combo
public class combo
{

//constructor combo
public combo()
{

//we are accessing c1 from within inner layer so need to be declared as final
final JComboBox c1;

//frame
JFrame j=new JFrame();

//JButton
final JButton b=new JButton("what you select?");
String s[]={"javatyro","siddhu","vydyas"};

c1=new JComboBox(s);


c1.setBounds(10,10,150,30);
b.setBounds(10,50,150,30);
j.setSize(200,200);
j.setLayout(null);
j.setVisible(true);

j.add(b);
j.add(c1);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

//Event Handling

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str=(String)c1.getSelectedItem();
JOptionPane.showMessageDialog(null,"You select : "+str);  
}
});
}
public static void main(String args[])
{
combo c=new combo();
}
}

OUTPUT SCREENS


jcombobox in swings

2 comments: