TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Swing

    Create ComboBox using JComboBox Class

    Overview

    In this tutorial, we will learn steps to Create ComboBox using JComboBox Class. ComboBox is a GUI component that is used to display a drop-down list. ComboBox gives you options that allow users to select one and only one item at a time.

    JComboBox Class

    ComboBox can be editable or read-only. We can use the following import statement to use JComboBox in the program.

    import javax.swing.JComboBox;

    To create a Combo box, we can use the JComboBox class.

    JComboBox cBox = new JComboBox(subjectList);

    JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized.

    Java Program

    /**********************************************
    * FileName: JComboBoxDemo.java
    * Package : com.testingdocs.swing.components
    *
    * Java Tutorials - www.TestingDocs.com
    **********************************************/
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class JComboBoxDemo {
    
    public static void main(String[] args) {
    //Create a frame
    JFrame frame = new JFrame("JComboBox - www.TestingDocs.com");
    // String array
    String[] subjectList = {
    "Mathematics",
    "Physics",
    "Chemistry",
    "English",
    "History",
    "Commerce",
    "Moral Science"
    };
    //Create combo box
    JComboBox cbSubject = new JComboBox(subjectList);
    cbSubject.addActionListener(
    new ActionListener() {
    
    public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(frame,
    "Subject Selected: " +
    cbSubject.getSelectedItem().toString());
    }
    });
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 300);
    Container content = frame.getContentPane();
    content.setLayout(new FlowLayout());
    content.add(cbSubject);
    frame.setVisible(true);
    }
    }

    Output

    Run the program to view the output.
    JComboBox Class Demo

     

    —

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/in/java/

    Related Posts

    Add GUI Components Swing

    Java Swing /

    Add GUI Components to a Swing Container

    JPanel Container Class

    Java Swing /

    Swing JPanel Container Class

    Swing CardLayout

    Java Swing /

    Java Swing CardLayout

    Java Swing GridLayout

    Java Swing /

    Java Swing GridLayout

    Java Swing FlowLayout

    Java Swing /

    Java Swing FlowLayout

    ‹ Create Text Area using JTextArea Class› Create Lists using JList Class

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com