Java Reference
In-Depth Information
29
colorJList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
);
30
31
add(
new
JScrollPane(colorJList));
// add list with scrollpane
32
33
copyJButton =
new
JButton(
"Copy >>>"
);
34
copyJButton.addActionListener(
35
new
ActionListener()
// anonymous inner class
36
{
37
// handle button event
38
@Override
39
public
void
actionPerformed(ActionEvent event)
40
{
41
// place selected values in copyJList
42
copyJList.setListData(
colorJList.getSelectedValuesList().toArray(
new
String[
0
]));
43
44
45
}
46
}
47
);
48
49
add(copyJButton);
// add copy button to JFrame
50
51
copyJList =
new
JList<String>();
// list to hold copied color names
52
copyJList.setVisibleRowCount(
5
);
// show 5 rows
53
copyJList.setFixedCellWidth(
100
);
// set width
copyJList.setFixedCellHeight(
15
);
// set height
copyJList.setSelectionMode(
ListSelectionModel.SINGLE_INTERVAL_SELECTION
);
54
55
56
57
add(
new
JScrollPane(copyJList));
// add list with scrollpane
58
}
59
}
// end class MultipleSelectionFrame
Fig. 12.25
|
JList
that allows multiple selections. (Part 2 of 2.)
1
// Fig. 12.26: MultipleSelectionTest.java
2
// Testing MultipleSelectionFrame.
3
import
javax.swing.JFrame;
4
5
public
class
MultipleSelectionTest
6
{
7
public
static
void
main(String[] args)
8
{
9
MultipleSelectionFrame multipleSelectionFrame =
10
new
MultipleSelectionFrame();
11
multipleSelectionFrame.setDefaultCloseOperation(
12
JFrame.EXIT_ON_CLOSE
);
13
multipleSelectionFrame.setSize(
350
,
150
);
14
multipleSelectionFrame.setVisible(
true
);
15
}
16
}
// end class MultipleSelectionTest
Fig. 12.26
|
Testing
MultipleSelectionFrame
. (Part 1 of 2.)