Use of combo box (JComboBox)

Source: Internet
Author: User

We know that a combo box is one of the most commonly used components in a real-world application.

I use the combo box (JComboBox) to compile according to the teacher's request.

Here's my code because I can't use the format the teacher says.

Import java.awt.*; /* contains all classes for creating the user interface and drawing graphics images. */
Import javax.swing.*; /* Provides a set of "lightweight" components that try to make these components work the same way on all platforms */


public class Jcomboboxdemo extends JFrame {


Private static final long serialversionuid = 1L;

Public Jcomboboxdemo () {
/*
* Container is the parent of all containers and is also a subclass of the Java language Component class component. A container is a component with the ability to accommodate other components and containers
* The most basic element of a Java graphical user interface is a component, which is an object that can be graphically displayed on the screen and can interact with the user, such as a button, a text box, and so on.
* In Java, components are typically placed inside a certain container using the This.getcontentpane () method to return the ContentPane object of this form
*/
Container ContentPane = This.getcontentpane ();

/* Create a Panel object that specifies that the layout manager is a gridlayout,1 row 2 column. JPanel Default layout management is FlowLayout */
JPanel JPanel1 = new JPanel (new GridLayout (1, 2));

Using string arrays to build JComboBox
String[] fruit = {"Apple", "banana", "orange", "pear", "Mango"};
jcombobox<string> jComboBox1 = new jcombobox<string> (fruit);
Jcombobox1.additem ("other"); Add another option at the end of the list box option

Set the caption border of a JList1 object
Jcombobox1.setborder (Borderfactory.createtitledborder ("Your favorite Fruit:"));
Add list box JComboBox1 to panel
Jpanel1.add (JComboBox1);

Build JComboBox with ComboBoxModel
comboboxmodel<?> MyModel = new MyModel ();
JComboBox jComboBox2 = new JComboBox (MyModel);
Set the caption border of a JList1 object
Jcombobox2.setborder (Borderfactory.createtitledborder ("Your favorite Fruit:"));
Add list box JComboBox2 to panel
Jpanel1.add (JCOMBOBOX2);

Add Panel to parent container
Contentpane.add (JPANEL1);
Set the title of this form
This.settitle ("Jcomboboxdemo");
Set the initial size shown in this form
This.setsize (350, 90);
This.setlocation (300, 200);
Set this form to be initially visible
This.setvisible (TRUE);


}
Class MyModel extends DefaultComboBoxModel {
String[] fruit = {"Apple", "banana", "orange", "pear", "Mango"};

MyModel () {
for (int i = 0; i < fruit.length; i++) {
/* AddElement () method to add an option element to a list box */
This.addelement (Fruit[i]);
}
}
}

public static void Main (String args[]) {
Jcomboboxdemo test = new Jcomboboxdemo ();
}
}

Use of combo box (JComboBox)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.