Advertisement
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 | 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. |
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);
}
}
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 -:
Advertisement
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 -:
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 :
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement