Java graphical user interface drop-down list

Source: Internet
Author: User

Java graphical user interface drop-down list

Common methods for drop-down lists:

*jcombobox (): Create a drop-down list with no options

*additem (): Add option

*getselectedindex (): Returns the index of the selected drop-down list option

*getselecteditem (): Returns the selected drop-down list option

*removeitemat (Index): Remove the option to index value to index

*removeallitem (): Remove all options

*additemlistener (): Monitor for registering events

Package Example_java;import Java.awt.flowlayout;import java.awt.event. Actionevent;import java.awt.event. Actionlistener;import java.awt.event. Itemevent;import java.awt.event. Itemlistener;import Javax.naming.initialcontext;import Javax.swing.jbutton;import Javax.swing.JComboBox;import Javax.swing.jframe;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;import Javax.swing.JTextField; ClassWindowchoice extends JFrame implements ItemListener, actionlistener{JComboBox choice; JTextField text; JTextArea area; JButton Add, Del; PublicWindowchoice () {init (); SetSize (400, 400); SetVisible (True); Setdefaultcloseoperation (jframe.exit_on_close);} voidInit () {setlayout (newFlowLayout ()); Choice = newJComboBox (); Text = new JTextField (8); area = new JTextArea (6,25); Choice.additem ("Music World"); Choice.additem ("Martial arts World"); Choice.additem ("Chess Park")); Choice.additem ("Friend Chat"); add = new JButton ("adding"); del = new JButton ("delete" ), Add.addactionlistener (this ), Text.addactionlistener (this ); Del.addactionlistener (this ), Choice.addactionlistener (this ), add (choice), add (Del), add (text), add (add) ; Add (new  JScrollPane (area)), public void  actionperformed (ActionEvent e) {if (e.getsource () = = Add | | e.getsou RCE () = =  text) {String name =  Text.gettext (); if (Name.length () > 0 ) {choice.additem (name); choice.se Tselecteditem (name); Area.append ("\ n List added:" +  name);}} else if (e.getsource () = =  del) {area.append ("\ n list removed:" +  Choice.getselecteditem ()); Choice.remove ( Choice.getselectedindex ()); }} public void  itemstatechanged (itemevent e) {String name =  Choice.getselecteditem (). toString (); int index =< span> Choice.getselectedindex (); Area.settext ("\ n" +index+ ":" +  name);}           

Java graphical user interface 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.