list boxes and combo boxes work in swing just as they do in old AWT, but they are also added if we need it. In addition, it is also more convenient and easy to use. For example, JList has a builder that displays a string array (oddly, the same functionality is not valid in JComboBox!). )。 The following examples show their basic usage.
: Listcombo.java
//List Boxes & Combo boxes
package c13.swing;
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
public class Listcombo extends JPanel {public
Listcombo () {
setlayout (new GridLayout);
JList list = new JList (buttongroups.ids);
Add (new JScrollPane (list));
JComboBox combo = new JComboBox ();
for (int i = 0; i < i++)
Combo.additem (integer.tostring (i));
Add (combo);
}
public static void Main (String args[]) {
show.inframe (new Listcombo (), 200,200);
}
///:~
In the beginning, it seemed a bit odd that jlists could not automatically provide scrolling features-even if that was what we had hoped for. Increasing support for scrolling is easy, as demonstrated above-simply by encapsulating JList into JScrollPane, all the details are automatically taken care of for us.