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

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version