Java Interface Programming (8)------combo box (drop-down list)

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020

similar to the functionality of a set of radio buttons, a combo box (drop-down list) also forces users to select only one of a set of possible elements, but this approach is more compact and makes it easier to change the contents of a drop-down list without confusing the user.

In the following example, the JComboBox combo box starts with some elements, and then when a button is pressed, a new element is added to the combo box.

public class ComboBoxes extends JFrame {private string[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};p rivate JTextField t = new JTextField;p rivate JComboBox c = new JComboBox ();p rivate JButton b = new JButton ("ADD items");p Riva Te int count = 0;public comboboxes () {//TODO auto-generated constructor Stubsetlayout (new FlowLayout ()); SetSize (200,175) ; setvisible (true); for (int i = 0; i < 3;i++) C.additem (weekdays[count++]); t.seteditable (false); B.addactionlistener ( New ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method stubif (Count &lt ; Weekdays.length) {C.additem (weekdays[count++]);}}); C.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated Method Stubt.settext ("You have selected" + C.getselecteditem ());}); C.seteditable (True); add (t); add (c); add (b);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew comboboxes ();}}
The effect is as follows:

  

When you click an option in the combo box, the selected results are displayed in JTextField.


Java Interface Programming (8)------combo box (drop-down list)

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.