[JAVA100 example]014, Drop-down list

Source: Internet
Author: User
Tags final gettext int size

Import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


import javax.swing.event.*;


/**


* <p>title: list box </p>


* <p>description: Add elements through the input box and click the "Delete" button to delete the list elements </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:ListDemo.java</p>


* @version 1.0


*/


public class Listdemo extends JPanel


implements Listselectionlistener {


private JList list;


private Defaultlistmodel Listmodel;


private static final String hirestring = "Add";


private static final String firestring = "delete";


private JButton Firebutton;


private JTextField EmployeeName;


public Listdemo () {


Super (New BorderLayout ());


Building list elements for lists


Listmodel = new Defaultlistmodel ();


listmodel.addelement ("Alan sommerer");


listmodel.addelement ("Alison huml");


listmodel.addelement ("Kathy walrath");


listmodel.addelement ("Lisa friendly");


listmodel.addelement ("Mary Campione");


listmodel.addelement ("Sharon zakhour");


//Create a list widget and add the element to the list


list = new JList (Listmodel);


//Set selection mode to Radio


List.setselectionmode (listselectionmodel.single_selection);


//Initializes the location of the selected index at 0, that is, the first element


list.setselectedindex (0);


List.addlistselectionlistener (this);


//Set list can look at 5 elements at the same time


List.setvisiblerowcount (5);


//Add a slider to the list


JScrollPane Listscrollpane = new JScrollPane (list);


JButton Hirebutton = new JButton (hirestring);


Hirelistener Hirelistener = new Hirelistener (Hirebutton);


Hirebutton.setactioncommand (hirestring);


Hirebutton.addactionlistener (Hirelistener);


hirebutton.setenabled (FALSE);


Firebutton = new JButton (firestring);


Firebutton.setactioncommand (firestring);


Firebutton.addactionlistener (New Firelistener ());


EmployeeName = new JTextField (10);


Employeename.addactionlistener (Hirelistener);


employeename.getdocument (). Adddocumentlistener (Hirelistener);


String name = Listmodel.getelementat (


List.getselectedindex ()). ToString ();


//Create a panel


JPanel buttonpane = new JPanel ();


buttonpane.setlayout (New BoxLayout (Buttonpane,


Boxlayout.line_axis));


Buttonpane.add (Firebutton);


Buttonpane.add (Box.createhorizontalstrut (5));


Buttonpane.add (New Jseparator (swingconstants.vertical));


Buttonpane.add (Box.createhorizontalstrut (5));


Buttonpane.add (EmployeeName);


Buttonpane.add (Hirebutton);


Buttonpane.setborder (Borderfactory.createemptyborder (5,5,5,5));


Add (Listscrollpane, borderlayout.center);


Add (Buttonpane, borderlayout.page_end);


  }


/**


*<br> Class Description: "Add" button to monitor


*<br> class Description: When the "Add" button is clicked, the implementation adds the element to the list box


*/


class Firelistener implements ActionListener {


public void actionperformed (ActionEvent e) {


    


int index = List.getselectedindex ();


Listmodel.remove (index);


int size = Listmodel.getsize ();


if (size = = 0) {//If there is no selection, then the delete button results


firebutton.setenabled (FALSE);


} else {//Select a


if (index = = listmodel.getsize ()) {


//Removal options


index--;


        }


List.setselectedindex (index);


list.ensureindexisvisible (index);


      }


    }


  }


/**


*<br> class Description: "Delete" button listener event


*<br> Class Description: Implementing Delete list elements


*/


class Hirelistener implements ActionListener, Documentlistener {


Private Boolean alreadyenabled = false;


private JButton button;


Public Hirelistener (JButton button) {


This.button = button;


    }


ActionListener must be implemented.


public void actionperformed (ActionEvent e) {


String name = Employeename.gettext ();


//If input is empty or has the same name


if (Name.equals ("") | | Alreadyinlist (name)) {


Toolkit.getdefaulttoolkit (). Beep ();


Employeename.requestfocusinwindow ();


Employeename.selectall ();


return;


      }


int index = List.getselectedindex (); Get the selection


if (index = = 1) {//If there is no selection, insert it into the first


index = 0;


} else {//If there is a selection, then insert to the back of the selection


index++;


      }


Listmodel.insertelementat (Employeename.gettext (), index);





//Reset Text


Employeename.requestfocusinwindow ();


Employeename.settext ("");


//select new element and show it


List.setselectedindex (index);


list.ensureindexisvisible (index);


    }


/**


*<br> Method Description: Detect whether there are duplicate elements in the list


*<br> input parameter: String name Detection name


*<br> return Type: Boolean Boolean value, if present returns True


*/


protected Boolean alreadyinlist (String name) {


return Listmodel.contains (name);


    }


/**


*<br> Method Description: Implement the Documentlistener interface, the method that must be implemented


*<br> input Parameters:


*<br> return type:


*/


public void Insertupdate (Documentevent e) {


Enablebutton ();


    }


/**


*<br> Method Description: Implement the Documentlistener interface, the method that must be implemented


*<br> input Parameters:


*<br> return type:


*/


public void Removeupdate (Documentevent e) {


Handleemptytextfield (e);


    }


/**


*<br> Method Description: Implement the Documentlistener interface, the method that must be implemented


*<br> input Parameters:


*<br> return type:


*/


public void Changedupdate (Documentevent e) {


if (!handleemptytextfield (e)) {


Enablebutton ();


      }


    }


/**


*<br> Method Description: button to enable


*<br> input Parameters:


*<br> return type:


*/


private void Enablebutton () {


if (!alreadyenabled) {


button.setenabled (TRUE);


      }


    }


/**


*<br> Method Description: Implement the Documentlistener interface, must implement the method, modify the state of the button


*<br> input Parameters:


*<br> return type:


*/


Private Boolean Handleemptytextfield (Documentevent e) {


if (E.getdocument (). GetLength () <= 0) {


button.setenabled (FALSE);


alreadyenabled = false;


return true;


      }


return false;


    }


  }


/**


*<br> Method Description: Implement the Listselectionlistener interface, the method that must be implemented


*<br> input Parameters:


*<br> return type:


*/


public void valuechanged (Listselectionevent e) {


if (e.getvalueisadjusting () = False) {


if (list.getselectedindex () = = 1) {


firebutton.setenabled (FALSE);


} else {


firebutton.setenabled (TRUE);


      }


    }


  }


/**


*<br> Method Description: Main method


*<br> Input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


jframe.setdefaultlookandfeeldecorated (TRUE);


//Create a form


JFrame frame = new JFrame ("Listdemo");


frame.setdefaultcloseoperation (jframe.exit_on_close);


//Create a face version


jcomponent Newcontentpane = new Listdemo ();


Newcontentpane.setopaque (TRUE);


Frame.setcontentpane (Newcontentpane);


//Display form


Frame.pack ();


frame.setvisible (TRUE);


  }


}

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.