Java JComboBox

Java JComboBox

Java JComboBox
JComboBox inherits from class JComponent.The selected choice pop-ups at the top of the menu.

How to declare JCombox

[public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible]

JCombox constructors.

  • JComboBox()-creates a combo box with default data model.
  • JComboBox(Object [] items)-creates combo box that gets items from a specified array.
  • JComboBox(Vector<?> items)-creates combo box with elements from a Vector

JCombox commonly used methods

Java JComboBox Example

[
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class JComboBoxExample extends JFrame {
    
    JComboBoxExample()
    {
    super("JComboBox Example");
    
    String languages []={"Java","C++","PHP","JavaScript","C#"};
    
    JComboBox box=new JComboBox(languages);
      box.setBounds(50, 50,90,20);    
            
    add(box);
    
     setSize(400,400);
   setLayout(null);
   setLocationRelativeTo(null);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
    
    }
    public static void main(String args[])
    {
         new JComboBoxExample();
    
    }
    
}
]


Java JComboBox with ActionListener Example

[
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.UIManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JComboBoxExample extends JFrame implements ActionListener {
    public   JComboBox box;
    public JLabel label;
    public JButton button;

    JComboBoxExample()
    {
        super("JComboBox Example");
        try        {
            UIManager.setLookAndFeel
                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        label = new JLabel();
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setSize(400,100);
        add(label);
        String languages []={"Kenya","Somalia","Rwanda",
                "Tanzania","South Africa","Namibia","Nigeria"};

        box=new JComboBox(languages);
        box.setBounds(50,80,150,20);

        add(box);

        button=new JButton("Show");

        button.setBounds(50,150,100,30);
        add(button);
        button.addActionListener(this);

        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }
    public static void main(String args[])
    {
        new JComboBoxExample();

    }

    public void actionPerformed(ActionEvent event)
    {
        String lang="Country selected is  "+
                box.getItemAt(box.getSelectedIndex());
        label.setText(lang);
    }

}
]

Output:

Java JComboBox with ActionListener Example


Lessons you should learn from above program.

First you should note the difference between the two images.The look and Feel is different and this is courtesy of javax.swing.UIManager that i used to set WindowsClassicLookAndFeel using below code.
 [try
{
    UIManager.setLookAndFeel
            ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
}
catch(Exception e)
{
    e.printStackTrace();
}]

Above class extends and implements ActionListener.[public class JComboBoxExample extends JFrame implements ActionListener]What does this mean?

This means that class JComboBoxExample has access to all JFrame methods.These are:-
[
setSize(400,400);
setLayout(null);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
]
 setDefaultCloseOperation(EXIT_ON_CLOSE) is used to exit the application when the close button is clicked.

Implementing ActionListener interface means that you have to override public action performed method.
[
public void actionPerformed(ActionEvent event)
{
    String lang="Country selected is  "+
            box.getItemAt(box.getSelectedIndex());
    label.setText(lang);
}
]
Code to add  JComboBox to JFrame is  [ box.setBounds(50,80,150,20);].Read more about setBounds component method here

See also 

How to add ActionListener to JButton



COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JComboBox
Java JComboBox
Java JComboBox
https://2.bp.blogspot.com/-T6U6IGllrJo/WMEMk8qKtJI/AAAAAAAAHGo/3mvXWRHiYJoa1PivQHkyVgxMIcqQivoXQCLcB/s200/java-JComboBox-example-min.PNG
https://2.bp.blogspot.com/-T6U6IGllrJo/WMEMk8qKtJI/AAAAAAAAHGo/3mvXWRHiYJoa1PivQHkyVgxMIcqQivoXQCLcB/s72-c/java-JComboBox-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jcombobox.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jcombobox.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy