Lists
If you want to present a set of choices to a user, and a radio
button or checkbox set consumes too much space, you can
use a combo box or a list
The JList component has many more features, and its design is
similar to that of the tree and table components.
The JList Component
The JList component is similar to a set of checkboxes or radio buttons,
except that the items are placed inside a single box and are selected by
clicking on the items themselves, not on buttons
To construct this list component, you first start out with an array of
strings, then pass the array to the JList constructor:
String[] words= { "quick", "brown", "hungry", "wild", ... };
JList wordList = new JList(words);
Alternatively, you can use an anonymous array:
JList wordList = new JList(new String[] {"quick", "brown", "hungry", "wild", ... });
To make a list box scroll, you must insert it into a scroll pane:
JScrollPane scrollPane = new JScrollPane(wordList);
By default, the list component displays eight items; use the setVisibleRowCount
method to change that value:
wordList.setVisibleRowCount(4); // display 4 items
You can set the layout orientation to one of three
values:
JList.VERTICAL (the default): arrange all items vertically
JList.VERTICAL_WRAP: start new columns if there are more items than the
visible row count
JList.HORIZONTAL_WRAP: start new columns if there are more items than the
visible row count, but fill them horizontally.
Look at the placement of the words "quick," "brown," and
"hungry" in Figure below to see the difference between
vertical and horizontal wrap
Fig: Lists with vertical and horizontal wrap
public class NewClass2 extends JFrame
{
JList list=new JList();
DefaultListModel model=new DefaultListModel();
int i;
NewClass2()
{
String[] b={"a","b","c","d"};
for(int i=0;i<b.length;i++)
model.add(i, b[i]);
list.setModel(model);
Container c=getContentPane();
c.add(list);
}
public static void main(String[] arg)
{
NewClass2 o1=new NewClass2();
o1.setVisible(true);
o1.pack();
} }
public class listcmd extends JFrame implements ListSelectionListener
{
JList list=new JList();
DefaultListModel model;
int I; JTextArea text; JLabel label;
listcmd()
{
String[] b={"a","b","c","d"};
List with
model=new DefaultListModel();
text=new JTextArea();
GridLayout l=new GridLayout(2,2);
for(int i=0;i<b.length;i++)
model.add(i, b[i]);
list.setModel(model);
label=new JLabel("selected text");
text=new JTextArea();
Container c=getContentPane();
c.setLayout(l);
c.add(label);
c.add(text);
c.add(list);
list.addListSelectionListener(this);
Event Handling
public static void main(String[] arg)
{
listcmd o1=new listcmd();
o1.setVisible(true);
o1.pack();
}
@Override
public void valueChanged(ListSelectionEvent e)
{
String g=list.getSelectedValue().toString();
text.setText(g);
}
}
Implementing List box in Netbeans
DefaultListModel model=new DefaultListModel();
DefaultListModel model1=new DefaultListModel();
private void addActionPerformed(java.awt.event.ActionEvent evt)
{
String[] list = { "abc", "blue", "green", "yellow", "white" };
for(int i=0;i<list.length;i++)
model.addElement(list[i]);
jList1.setModel(model);
}
private void jList1MouseClicked(java.awt.event.MouseEvent evt)
{
jTextField1.setText(jList1.getSelectedValue().toString());
String a=jList1.getSelectedValue().toString();
}
private void removeActionPerformed(java.awt.event.ActionEvent evt)
{
int i=jList1.getSelectedIndex();
System.out.print("index is:::"+i);
model.remove(i);
}
private void replaceActionPerformed(java.awt.event.ActionEvent evt)
{
int i=jList1.getSelectedIndex();
System.out.print("index is:::"+i);
model.remove(i);
model.insertElementAt(jTextField1.getText(), i);
}
Trees
Every computer user who uses a hierarchical file system has
encountered tree displays such as the one in Fig below
Many tree structures arise in everyday life, such as the hierarchy of
countries, states, and cities shown in Figure below
Fig. A hierarchy of countries, states, and cities
A tree is a component that presents a hierarchical view of data. A user has the
ability to expand or collapse individual subtrees in this display.
Trees are implemented in Swing by the JTree class, which extends
Jcomponent
The Jtree class (together with helper classes) takes care of laying out the
tree and processing user requests for expanding and collapsing nodes.
Tree terminology
Some of tree constructors are:
1. JTree
Creates a tree .By default a leaf node is any node without child nodes
2. JTree(Object obj[ ])
Creates a tree with the specified array items as the child nodes of the root node
3. JTree(TreeNode tn)
Creates a tree with the specified node as the root nodes
4. JTree(TreeNode tn,boolean allowchild);
Creates a tree with the specified node as the root nodes .It uses Boolean
value to decide whether a node is a leaf node
Simple Trees
As with most other Swing components, the Jtree component follows the
model-view-controller pattern. You provide a model of the hierarchical
data, and the component displays it for you. To construct a Jtree, you
supply the tree model in the constructor:
DefaultTreeModel model = . . .;
JTree tree = new JTree(model);
Fig: A simple tree
This class implements the MutableTreeNode interface, a subinterface
of TreeNode (as shown)
Fig Tree classes
public MyTree()
{
JTree jtr;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Color");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Black");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Blue");
DefaultMutableTreeNode child21 = new DefaultMutableTreeNode("Navy Blue");
DefaultMutableTreeNode child22 = new DefaultMutableTreeNode(" DarkBlue");
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Green");
DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("White");
root.add(child1);
root.add(child2);
root.add(child3);
root.add(child4);
child2.add(child21);
child2.add(child22);
Jtr=new JTree(root);
c.add(root);
// TODO add your handling code here:
}