国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例

Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例

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

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

本文實(shí)例講述了java swing中jlist選擇事件監(jiān)聽器listselectionlistener用法。分享給大家供大家參考,具體如下:

當(dāng)jlist中的元素被選中時(shí),選擇事件將被觸發(fā)。對(duì)于jtable也是一樣,你可以把它看做是多個(gè)并列的jlist。那么,如果程序需要對(duì)該事件做出響應(yīng),需要以下步驟:

(1)創(chuàng)建一個(gè)實(shí)現(xiàn)了 listselectionlistener的監(jiān)聽器;
(2)使用jlist或selectionmodel的addlistselectionlistener添加監(jiān)聽器;
(3)在監(jiān)聽器的valuechanged方法添加響應(yīng)代碼。

在響應(yīng)代碼中需要注意的是getvalueisadjusting值的判斷。測(cè)試表明,每當(dāng)我們進(jìn)行選擇時(shí),valuechanged方法都會(huì)被激活多次,其中,在最后的鼠標(biāo)操作中,getvalueisadjusting值為false,而在一系列中間操作中,該值均為true。比如說(shuō),用鼠標(biāo)連續(xù)劃過(guò)一串元素時(shí),會(huì)有一系列g(shù)etvalueisadjusting為true的valuechanged方法激活,且最后一次為false。而我們對(duì)選擇事件的判定一般是以最后接觸為準(zhǔn),因此這里對(duì)getvalueisadjusting值進(jìn)行一個(gè)判斷。

常用方法如下:

getleadselectionindex()
返回當(dāng)前選中的元素的index。

getminselectionindex()
返回選中的多個(gè)元素中index的最小值,如果選擇為空在返回-1。

getmaxselectionindex()
原理同上。

isselectedindex(int index)
判斷指定index是否被選中。

clearselection()
清除選中。

getselectedindex()
返回被選中的所有元素中最小的index。

getselectedindices()
返回一個(gè)整型數(shù)組,包含被選中的所有index。

getselectedvalue()
返回被選中的,index最小的元素值。

getselectedvalues()
返回一個(gè)object數(shù)組,包含被選中的所有元素對(duì)象。

getselectedvalueslist()
返回一個(gè)objectlist,包含被選中的所有元素對(duì)象。

下面的demo來(lái)自于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.jfrwli.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());
    }
  }
}

運(yùn)行效果:

Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: av官网 | 国产一区二区三区视频在线观看 | 欧美精品v国产精品v日韩精品 | 91视频在线 | 欧美一级片在线 | 国产女爽爽视频精品免费 | 成人av观看 | 羞涩网站 | 在线播放一级片 | 欧美视频在线观看不卡 | 国产成人精品一区二区三区四区 | 久久国产精品久久久久久久久久 | 成人片网址 | 日日精品| 日韩精品免费在线观看 | 在线观看毛片网站 | 98久9在线 | 免费 | 欧美黄色一级 | 日本久久成人 | 免费欧美一级 | 91色在线观看 | 手机av在线 | 在线免费观看av的网站 | 精品日韩 | 91欧美在线 | 日韩一区二区三区电影在线观看 | 国产精品精品 | 亚洲国产精品久久久久久6q | 欧洲精品视频在线观看 | 亚洲成年人影院 | 亚洲国产精品一区二区第一页 | 亚洲综合在线播放 | 精品人成 | 欧美激情一区二区三级高清视频 | 天堂va在线高清一区 | 媚黑视频 | 久久国产精品影视 | 亚洲精品无码专区在线播放 | 日本在线视频免费观看 | 国产日产精品一区二区三区四区 | 欧美日韩精品网站 |