Biblioteca Java - Blame information for rev 32
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
32 | mihai | 1 | /* |
2 | * To change this license header, choose License Headers in Project Properties. | ||
3 | * To change this template file, choose Tools | Templates | ||
4 | * and open the template in the editor. | ||
5 | */ | ||
6 | package guidemo; | ||
7 | |||
8 | import java.awt.BorderLayout; | ||
9 | import java.awt.Container; | ||
10 | import java.awt.event.MouseAdapter; | ||
11 | import java.awt.event.MouseEvent; | ||
12 | import java.io.PrintWriter; | ||
13 | import java.io.StringWriter; | ||
14 | |||
15 | import javax.swing.*; | ||
16 | import javax.swing.event.*; | ||
17 | |||
18 | public class ListTest extends JFrame implements ListSelectionListener{ | ||
19 | |||
20 | //JFrame frame = new JFrame("Selecting JList"); | ||
21 | |||
22 | String labels[] = { "Chardonnay", "Sauvignon", "Riesling", "Cabernet", | ||
23 | "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", | ||
24 | "Syrah", "Gewurztraminer" }; | ||
25 | |||
26 | JList jlist = new JList(labels); | ||
27 | |||
28 | final JTextArea textArea = new JTextArea(); | ||
29 | |||
30 | |||
31 | ListTest(){ | ||
32 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
33 | setVisible(true); | ||
34 | |||
35 | JScrollPane scrollPane1 = new JScrollPane(jlist); | ||
36 | add(scrollPane1, BorderLayout.WEST); | ||
37 | |||
38 | |||
39 | textArea.setEditable(false); | ||
40 | JScrollPane scrollPane2 = new JScrollPane(textArea); | ||
41 | add(scrollPane2, BorderLayout.CENTER); | ||
42 | |||
43 | jlist.addListSelectionListener(this); | ||
44 | |||
45 | jlist.addMouseListener(new MyMouseListener()); | ||
46 | |||
47 | setSize(350, 200); | ||
48 | setVisible(true); | ||
49 | |||
50 | |||
51 | |||
52 | } | ||
53 | |||
54 | public void valueChanged(ListSelectionEvent listSelectionEvent) { | ||
55 | StringWriter sw = new StringWriter(); | ||
56 | PrintWriter pw = new PrintWriter(sw); | ||
57 | pw.print("First index: " + listSelectionEvent.getFirstIndex()); | ||
58 | pw.print(", Last index: " + listSelectionEvent.getLastIndex()); | ||
59 | boolean adjust = listSelectionEvent.getValueIsAdjusting(); | ||
60 | pw.println(", Adjusting? " + adjust); | ||
61 | if (!adjust) { | ||
62 | JList list = (JList) listSelectionEvent.getSource(); | ||
63 | int selections[] = list.getSelectedIndices(); | ||
64 | Object selectionValues[] = list.getSelectedValues(); | ||
65 | for (int i = 0, n = selections.length; i < n; i++) { | ||
66 | if (i == 0) { | ||
67 | pw.print(" Selections: "); | ||
68 | } | ||
69 | pw | ||
70 | .print(selections[i] + "/" + selectionValues[i] | ||
71 | + " "); | ||
72 | } | ||
73 | pw.println(); | ||
74 | } | ||
75 | textArea.append(sw.toString()); | ||
76 | } | ||
77 | |||
78 | class MyMouseListener extends MouseAdapter{ | ||
79 | public void mouseClicked(MouseEvent mouseEvent) { | ||
80 | JList theList = (JList) mouseEvent.getSource(); | ||
81 | if (mouseEvent.getClickCount() == 2) { | ||
82 | int index = theList.locationToIndex(mouseEvent.getPoint()); | ||
83 | if (index >= 0) { | ||
84 | Object o = theList.getModel().getElementAt(index); | ||
85 | textArea.append("Double-clicked on: " + o.toString()); | ||
86 | textArea.append(System.getProperty("line.separator")); | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | public static void main(String args[]) { | ||
93 | new ListTest(); | ||
94 | |||
95 | } | ||
96 | } |