一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java Swing中JList選擇事件監聽器ListSelectionListener用法示例

Java Swing中JList選擇事件監聽器ListSelectionListener用法示例

2021-02-04 11:57pzy4447 Java教程

這篇文章主要介紹了Java Swing中JList選擇事件監聽器ListSelectionListener用法,結合具體實例形式分析了中JList選擇事件監聽器ListSelectionListener的功能、使用方法及相關注意事項,需要的朋友可以參考下

本文實例講述了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 Swing中JList選擇事件監聽器ListSelectionListener用法示例

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/pzy4447/p/4912584.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2015台湾永久免费平台 | 亚洲国产日韩欧美在线vip1区 | 扒开黑女人p大荫蒂老女人 扒开大腿狠狠挺进视频 | 69av导航| 国产一级片在线 | 亚洲国产成人久久综合一 | 好妈妈7在线观看高清 | 美女扒开尿口让男生添 漫画 | 国产一区二区三区四 | 2023毛片| 亚洲精品综合网 | 国产精品午夜国产小视频 | 美女日b视频 | 99久热只有精品视频免费观看17 | 亚洲高清视频网站 | 国产suv精品 | 小sao货水好多真紧h的视频 | 97在线资源站 | 欧美高清videosdesex0 | 久久视频在线视频观看天天看视频 | 日一日操一操 | 日韩成人在线网站 | 91久久精品国产一区二区 | 男女啪啪gif | 好姑娘完整版在线观看中文 | 色欲麻将 | 欧美夫妇野外交换hd高清版 | 国产精品麻豆免费版 | 娇妻被健身教练挺进小说阅读 | 亚洲国产精品无圣光一区二区 | yellow片在线观看 | 久久婷婷五月综合色精品首页 | 九九99香蕉在线视频免费 | 欧美聚众性派对hdsex | 99热这里有免费国产精品 | 亚洲成年人免费网站 | 日韩精品成人 | 236zz宅宅最新伦理 | 久久性综合亚洲精品电影网 | 插入逼 | 久久99亚洲AV无码四区碰碰 |