list boxes in Java

Source: Internet
Author: User

The

list box is completely different from the selection box, not just when we activate the selection box, and the list box is fixed at the specified location on the screen. In addition, a list box allows multiple selections: if we click on more than one item, the selection is high brightness and we can choose as many as we want. If we want to see a list of items, we can call GetSelectedItem () to produce a list of selected items. To delete an item from a group, we have to click it again. list box, of course there's a problem with the default action is to double-click instead of click. Click to add or remove items from the group, and double-click Invoke action (). The solution to this problem is to retrain our users as the following program assumes.
 

: List1.java//Using lists with action () import java.awt.*;

Import java.applet.*; public class List1 extends Applet {string[] flavors = {"Chocolate", "Strawberry", "Vanilla Fudge, Swirl", "Mint Chi
  P "," Mocha Almond Fudge "," Rum Raisin "," Praline Cream "," Mud Pie "};
  Show 6 items, allow multiple selection:list lst = new List (6, true);
  TextArea t = new TextArea (flavors.length, 30);
  Button B = New button ("Test");
  int count = 0;
    public void init () {t.seteditable (false);
    for (int i = 0; i < 4; i++) Lst.additem (flavors[count++]);
    Add (t);
    Add (LST);
  Add (b);
      Public boolean action (Event evt, Object Arg) {if (Evt.target.equals (LST)) {T.settext ("");
      string[] items = Lst.getselecteditems ();
    for (int i = 0; i < items.length i++) T.appendtext (items[i] + "\ n");
    else if (Evt.target.equals (b)) {if (Count < Flavors.length) Lst.additem (flavors[count++), 0); 
} else      Return super.action (EVT, ARG);
  return true; }
} ///:~

When the button is pressed, the button increases the item to the top of the list (because the second argument for AddItem () is 0). Adding items to a list box is more reasonable than a selection box because the user expects to scroll through a list box (for that reason, it has built-in scroll bars) but the user does not want to have to figure out how to scroll to the desired item, as in the previous example.
However, the only way to invoke action () is by double-clicking. If we want to monitor what users do in our list (especially clicks), we must provide an alternative.

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.