本文實例講述了java swing中jlist選擇事件監聽器listselectionlistener用法。分享給大家供大家參考,具體如下:
當jlist中的元素被選中時,選擇事件將被觸發。對于jtable也是一樣,你可以把它看做是多個并列的jlist。那么,如果程序需要對該事件做出響應,需要以下步驟:
(1)創建一個實現了 listselectionlistener的監聽器;
(2)使用jlist或selectionmodel的addlistselectionlistener添加監聽器;
(3)在監聽器的valuechanged方法添加響應代碼。
在響應代碼中需要注意的是getvalueisadjusting值的判斷。測試表明,每當我們進行選擇時,valuechanged方法都會被激活多次,其中,在最后的鼠標操作中,getvalueisadjusting值為false,而在一系列中間操作中,該值均為true。比如說,用鼠標連續劃過一串元素時,會有一系列getvalueisadjusting為true的valuechanged方法激活,且最后一次為false。而我們對選擇事件的判定一般是以最后接觸為準,因此這里對getvalueisadjusting值進行一個判斷。
常用方法如下:
getleadselectionindex()
返回當前選中的元素的index。
getminselectionindex()
返回選中的多個元素中index的最小值,如果選擇為空在返回-1。
getmaxselectionindex()
原理同上。
isselectedindex(int index)
判斷指定index是否被選中。
clearselection()
清除選中。
getselectedindex()
返回被選中的所有元素中最小的index。
getselectedindices()
返回一個整型數組,包含被選中的所有index。
getselectedvalue()
返回被選中的,index最小的元素值。
getselectedvalues()
返回一個object數組,包含被選中的所有元素對象。
getselectedvalueslist()
返回一個objectlist,包含被選中的所有元素對象。
下面的demo來自于listselectiondemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* * copyright (c) 1995, 2008, oracle and/or its affiliates. all rights reserved. * * redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - neither the name of oracle or the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * this software is provided by the copyright holders and contributors "as * is" and any express or implied warranties, including, but not limited to, * the implied warranties of merchantability and fitness for a particular * purpose are disclaimed. in no event shall the copyright owner or * contributors be liable for any direct, indirect, incidental, special, * exemplary, or consequential damages (including, but not limited to, * procurement of substitute goods or services; loss of use, data, or * profits; or business interruption) however caused and on any theory of * liability, whether in contract, strict liability, or tort (including * negligence or otherwise) arising in any way out of the use of this * software, even if advised of the possibility of such damage. */ package awtdemo; /* * listselectiondemo.java requires no other files. */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; @suppresswarnings("serial") public class listselectiondemo extends jpanel { jtextarea output; @suppresswarnings("rawtypes") jlist list; jtable table; string newline = "\n"; listselectionmodel listselectionmodel; @suppresswarnings({ "unchecked", "rawtypes" }) public listselectiondemo() { super(new borderlayout()); string[] listdata = { "one", "two", "three", "four", "five", "six", "seven" }; @suppresswarnings("unused") string[] columnnames = { "french", "spanish", "italian" }; list = new jlist(listdata); listselectionmodel = list.getselectionmodel(); listselectionmodel.addlistselectionlistener( new sharedlistselectionhandler()); jscrollpane listpane = new jscrollpane(list); jpanel controlpane = new jpanel(); string[] modes = { "single_selection", "single_interval_selection", "multiple_interval_selection" }; final jcombobox combobox = new jcombobox(modes); combobox.setselectedindex(2); combobox.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string newmode = (string)combobox.getselecteditem(); if (newmode.equals("single_selection")) { listselectionmodel.setselectionmode( listselectionmodel.single_selection); } else if (newmode.equals("single_interval_selection")) { listselectionmodel.setselectionmode( listselectionmodel.single_interval_selection); } else { listselectionmodel.setselectionmode( listselectionmodel.multiple_interval_selection); } output.append("----------" + "mode: " + newmode + "----------" + newline); } }); controlpane.add(new jlabel("selection mode:")); controlpane.add(combobox); //build output area. output = new jtextarea(1, 10); output.seteditable(false); jscrollpane outputpane = new jscrollpane(output, scrollpaneconstants.vertical_scrollbar_always, scrollpaneconstants.horizontal_scrollbar_as_needed); //do the layout. jsplitpane splitpane = new jsplitpane(jsplitpane.vertical_split); add(splitpane, borderlayout.center); jpanel tophalf = new jpanel(); tophalf.setlayout(new boxlayout(tophalf, boxlayout.line_axis)); jpanel listcontainer = new jpanel(new gridlayout(1,1)); listcontainer.setborder(borderfactory.createtitledborder( "list")); listcontainer.add(listpane); tophalf.setborder(borderfactory.createemptyborder(5,5,0,5)); tophalf.add(listcontainer); //tophalf.add(tablecontainer); tophalf.setminimumsize(new dimension(100, 50)); tophalf.setpreferredsize(new dimension(100, 110)); splitpane.add(tophalf); jpanel bottomhalf = new jpanel(new borderlayout()); bottomhalf.add(controlpane, borderlayout.page_start); bottomhalf.add(outputpane, borderlayout.center); //xxx: next line needed if bottomhalf is a scroll pane: //bottomhalf.setminimumsize(new dimension(400, 50)); bottomhalf.setpreferredsize(new dimension(450, 135)); splitpane.add(bottomhalf); } /** * create the gui and show it. for thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createandshowgui() { //create and set up the window. jframe frame = new jframe( "listselectiondemo - www.ythuaji.com.cn" ); frame.setdefaultcloseoperation(jframe.exit_on_close); //create and set up the content pane. listselectiondemo demo = new listselectiondemo(); demo.setopaque( true ); frame.setcontentpane(demo); //display the window. frame.pack(); frame.setvisible( true ); } public static void main(string[] args) { //schedule a job for the event-dispatching thread: //creating and showing this application's gui. javax.swing.swingutilities.invokelater( new runnable() { public void run() { createandshowgui(); } }); } class sharedlistselectionhandler implements listselectionlistener { public void valuechanged(listselectionevent e) { listselectionmodel lsm = (listselectionmodel)e.getsource(); //system.out.printf("leadselectionindex is %s%n",lsm.getleadselectionindex()); output.append( "leadselectionindex is " + lsm.getleadselectionindex() + "\n" ); int firstindex = e.getfirstindex(); int lastindex = e.getlastindex(); boolean isadjusting = e.getvalueisadjusting(); output.append( "event for indexes " + firstindex + " - " + lastindex + "; isadjusting is " + isadjusting + "; selected indexes:" ); if (lsm.isselectionempty()) { output.append( " <none>" ); } else { // find out which indexes are selected. int minindex = lsm.getminselectionindex(); int maxindex = lsm.getmaxselectionindex(); for ( int i = minindex; i <= maxindex; i++) { if (lsm.isselectedindex(i)) { output.append( " " + i); } } } output.append(newline); output.setcaretposition(output.getdocument().getlength()); } } } |
運行效果:
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/pzy4447/p/4912584.html