Advertisement



< Prev
Next >



JComboBox class




JComboBox consists of an editable field and a drop-down list. A user can either select or edit any value from drop-drop list. In order to handle the events generated by clicking or editing in combobox created by JComboBox, an ActionListener interface is implemented. JComboBox is a is a component which extends JComponent class and it can be added to a container like JFrame or a component like JPanel.




Constructors of JComboBox


Constructor Description
public JComboBox() Creates a JComboBox with a default data model.
public JComboBox(E[] items) Creates a JComboBox that takes its items from an existing array.
public JComboBox(ComboBoxModel <E>) Creates a JComboBox that takes its items from an existing ComboBoxModel.





Methods of JComboBox class


Methods Description
public void addItem(E item) Adds an item to JComboBox.
public Object getSelectedItem() Gets the item selected by the user from JCombobox.
public void setEditable(boolean b) Allows to edit the option selected from the dropdown list of items in JComboBox.
addActionListener(ActionListener a) Adds an ActionListener.





An example of JComboBox


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

public class Combo
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
String [] BRICS;
JFrame jf;

JComboBox<String> combo;

A()
{
BRICS = new String[]{"Russia", "India", "South Africa", "Brazil", "China"};


 
jf= new JFrame("JComboBox");
combo= new JComboBox<String>(BRICS);

jf.add(combo);

jf.setLayout(new FlowLayout());
jf.setSize(300,200);
jf.setVisible(true);
}

}

When you run the code, you are presented a window shown below -:

Figure 1

When you click on the arrow poiting down, you are presented with a dropdown list, with its first option "Russia" preselected :

Figure 2

Remember the last JCheckbox(ch4) will not be visible because its icon has covered it, but it is still a checkbox, just covered by its icon.




Creating and initializing a JComboBox from an existing ComboBoxModel


In the upcoming code, we are going to handle events when a JRadioButton is checked or unchecked. In the next code, we have also combined a JRadioButton with a JLabel to add an icon/image next to a radio button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Combo2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
String [] planets;
JFrame jf;

DefaultComboBoxModel<String> combo1;
JComboBox<String> combo2;

A()
{
planets = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Pluto"};
combo1= new DefaultComboBoxModel<String>(planets);

combo2= new JComboBox<String>(combo1);
 
jf= new JFrame("JComboBox");


jf.add(combo2);

jf.setLayout(new FlowLayout());
jf.setSize(300,250);
jf.setVisible(true);
}

}
When you run the code, you are presented a window shown in the Figure3 below -:

Figure 3


When you click on the arrow pointing down, you are presented with a dropdown list, with its first option "Mercury" preselected :

Figure 4



Advertisement




Handling JComboBox events


We must implement the ActionListener interface to handle the events associated with the JComboBox. Let's see how do we do it with an example.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Combo4
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo4


class A implements ActionListener
{
String [] BRICS;
JFrame jf;
JComboBox<String> combo;
JLabel label1;

A()
{
BRICS = new String[]{"Russia", "India", "South Africa", "Brazil", "China"};


 
jf= new JFrame("JComboBox");
combo= new JComboBox<String>(BRICS);
label1 = new JLabel();

jf.add(combo);


combo.addActionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(210,200);
jf.setVisible(true);
}



public void actionPerformed(ActionEvent ae)
{
JComboBox cb = (JComboBox)ae.getSource();
label1.setText( ((String)cb.getSelectedItem()) + " is selected");
jf.add(label1);
jf.setVisible(true);
}


}
When you run the code, you are presented a window shown in the Figure5 below -:

Figure 5


When you check a JComboBox an ActionEvent is fired and you are presented a message to display which option of JComboBox is selected. For example, when you select the option of South Africa from dropdown list of JComboBox, you are notified like -

Figure6





Creating an editable JComboBox


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

public class Combo5
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo5

class A implements ActionListener
{
String [] planets;
JFrame jf;
JComboBox combo;
JLabel label1;

A()
{
planets = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Pluto"};
jf= new JFrame("JComboBox");
combo= new JComboBox(planets);

combo.setEditable(true);

label1 = new JLabel();

jf.add(combo);

combo.addActionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(210,250);
jf.setVisible(true);
}


public void actionPerformed(ActionEvent ae)
{
JComboBox cb = (JComboBox)ae.getSource();
label1.setText( ((String)cb.getSelectedItem()) + " is selected");
jf.add(label1);
jf.setVisible(true);
}


}
When you run the code, you are presented a window shown in the Figure7 below :

Figure 7


please notice that the first option Mercury is editable in the next figure-:

Figure 8


Now we have edited the first option Mercury to First Planet, in the figure 9

Figure 9

As soon as hit the Enter Key after editing the Mercury to First Planet, an ActionEvent is fired and you are presented a message to display which option of JComboBox is selected. For example, and in this example, you are notified like -

Figure 10




Please share this article -





< Prev
Next >
< JCheckBox
JRadioButton >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement