Lists and Combo Boxes


The default model for a list does not allow adding and removing items. The list must be created with a DefaultListModel component, as shown in the following snippet:

 //you can create a custom list models or use DefaultListModel DefaultListModel model = new DefaultListModel(); JList myList = new JList(model); //allows adds and removes // load myList with items model.add(0, "one"); model.add(1, "two"); model.add(2, "three"); myList.setForeground(Color.blue); myList.setBackground(Color.white); // this is how you insert model.add(0, "zero"); // this is how you append model.add(myList.getModel().getSize(), "four"); 

You might consider using a list or combination box to present choices to the user. This is a good idea for your project because it reduces the chance of user errors, which are inevitable when user responses are taken from typed input.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net