Swing/ListMouseSelection のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ListMouseSelection へ行く。
- 1 (2011-07-18 (月) 17:21:18)
- 2 (2011-07-19 (火) 12:24:06)
- 3 (2011-07-20 (水) 13:15:30)
- 4 (2011-07-20 (水) 19:33:29)
- 5 (2011-07-22 (金) 15:59:14)
- 6 (2011-07-24 (日) 07:58:36)
- 7 (2011-07-27 (水) 10:52:51)
- 8 (2011-07-27 (水) 13:13:20)
- 9 (2011-07-27 (水) 14:50:02)
- 10 (2012-05-30 (水) 15:18:25)
- 11 (2012-12-19 (水) 22:11:36)
- 12 (2012-12-27 (木) 14:16:01)
- 13 (2013-07-26 (金) 01:21:01)
- 14 (2013-09-03 (火) 01:31:47)
- 15 (2013-09-13 (金) 00:19:12)
- 16 (2014-08-12 (火) 02:10:09)
- 17 (2014-11-06 (木) 01:01:30)
- 18 (2014-11-25 (火) 03:03:31)
- 19 (2014-11-26 (水) 02:31:15)
- 20 (2014-11-26 (水) 17:01:53)
- 21 (2016-01-03 (日) 05:56:05)
- 22 (2016-09-30 (金) 15:47:33)
- 23 (2017-11-02 (木) 15:34:40)
- 24 (2017-11-09 (木) 14:09:56)
- 25 (2018-02-24 (土) 19:51:30)
- 26 (2018-10-11 (木) 17:47:50)
- 27 (2018-10-12 (金) 20:12:42)
- 28 (2020-10-06 (火) 14:51:14)
- 29 (2022-07-08 (金) 11:05:33)
- 30 (2022-08-20 (土) 22:15:25)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JListをマウスクリックのみで複数選択する #navi(../) RIGHT:Posted by [[terai]] at 2011-07-18 RIGHT:Posted by &author(aterai); at 2011-07-18 *JListをマウスクリックのみで複数選択する [#n2f0193f] JListをアイテムをマウスクリックだけで複数選択できるように設定します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(https://lh6.googleusercontent.com/-wj2xm8BlBbA/TiPrjr1sQ5I/AAAAAAAAA_c/NiXO891B5fs/s800/ListMouseSelection.png) **サンプルコード [#ncc6d7b3] #code{{ JList list = new JList(model) { private ClearSelectionListener listener; @Override public void setSelectionInterval(int anchor, int lead) { if(anchor==lead && lead>=0 && anchor>=0) { if(listener.isDragging) { addSelectionInterval(anchor, anchor); }else if(!listener.isInCellDragging) { if(isSelectedIndex(anchor)) { removeSelectionInterval(anchor, anchor); }else{ addSelectionInterval(anchor, anchor); } listener.isInCellDragging = true; } }else{ super.setSelectionInterval(anchor, lead); } } }; }} **解説 [#cd959e47] - 左: Default - 中: MouseEvent -- JList#processMouseEvent, JList#processMouseMotionEventをオーバーライドして、常にCtrlキーが押されている状態にする -- マウスでアイテムをドラッグしても選択状態は変わらない -- JListの空白部分をクリックした場合、アイテムの選択状態は変更せず(MouseEvent#consume())、フォーカスだけJListに移動 -- 参考: [http://forums.oracle.com/forums/thread.jspa?messageID=5692411 Thread: JList where mouse click acts like ctrl-mouse click] #code{{ JList list = new JList(model) { @Override protected void processMouseMotionEvent(MouseEvent e) { super.processMouseMotionEvent(convertMouseEvent(e)); } @Override protected void processMouseEvent(MouseEvent e) { if(e.getID()==MouseEvent.MOUSE_PRESSED && !getCellBounds(0, getModel().getSize()-1).contains(e.getPoint())) { e.consume(); requestFocusInWindow(); }else{ super.processMouseEvent(convertMouseEvent(e)); } } private MouseEvent convertMouseEvent(MouseEvent e) { //Thread: JList where mouse click acts like ctrl-mouse click //http://forums.oracle.com/forums/thread.jspa?messageID=5692411 return new MouseEvent( (Component) e.getSource(), e.getID(), e.getWhen(), //e.getModifiers() | InputEvent.CTRL_MASK, //select multiple objects in OS X: Command+click //pointed out by nsby e.getModifiers() | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton()); } }; }} - 右: SelectionInterval -- JList#setSelectionIntervalをオーバーライドして、ひとつのアイテムのセルを選択した場合は、JList#addSelectionInterval、JList#removeSelectionIntervalを使用するように変更 -- マウスでアイテムをドラッグすると、選択状態になる -- ひとつのアイテムのセル内でのドラッグでは、選択状態を変更しない -- 参考: [http://forums.oracle.com/forums/message.jspa?messageID=5692413#5692413 Thread: JList where mouse click acts like ctrl-mouse click] -- JListの空白部分をクリックした場合、アイテムの選択状態をすべてクリア([[JListの選択を解除>Swing/ClearSelection]]) **参考リンク [#e59c2cca] - [http://forums.oracle.com/forums/thread.jspa?messageID=5692411 Thread: JList where mouse click acts like ctrl-mouse click] - [[JListの選択を解除>Swing/ClearSelection]] - [[JListのセルにJCheckBoxを使用する>Swing/CheckBoxCellList]] **コメント [#tdd44287] - OSX(snow leopard)では、MouseEventは複数選択が出来ません。キーボードを使っても(command+クリック)無理でした。 -- [[nsby]] &new{2011-07-20 (水) 13:15:30}; -- ご指摘ありがとうございます。OSXでは「command+クリック」で複数選択でしたっけ? InputEvent.CTRL_MASK決め打ちではなく、Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()に修正した方がいいのかもしれません(ソースなどを更新しましたが、正常に動作するかは確認していません…)。 -- [[aterai]] &new{2011-07-20 (水) 19:33:29}; - Web Startでもう一度実行してみましたが、やはり出来ませんでした。MouseEventで複数選択ha -- [[nsby]] &new{2011-07-22 (金) 15:59:14}; - あ、変な所で切れてしまいました。すみません。あらためて、MouseEventで複数選択出来るのは、Shift+クリックで選択した場合のみです。それ意外はダメでした。(Cntl+クリックとかでもダメ) -- [[nsby]] &new{2011-07-22 (金) 16:02:20}; -- Web Startのキャッシュは、…関係なさそうですね。src.zipをダウンロードしてJList#processMouseEvent(...)内で、System.out.println(e);したりsuper.processMouseEvent(convertMouseEvent(e));だけにしてみるとどうなるでしょうか? -- [[aterai]] &new{2011-07-24 (日) 07:58:36}; - MouseEvent eを出力してみました ちゃんと処理してるように見えるんですが・・・ - MouseEvent eを出力してみました。ちゃんと処理してるように見えるんですが・・・ #code{{ Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() : 0x04 InputEvent.CTRL_MASK : 0x02 [ただのクリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(57,4),absolute(971,484),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY\ =0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Sy\ stemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 java.awt.event.MouseEvent[MOUSE_RELEASED,(57,4),absolute(971,484),button=1,modifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0.0,border=,flags=5\ 0331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=39,\ g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 java.awt.event.MouseEvent[MOUSE_CLICKED,(57,4),absolute(971,484),button=1,modifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0.0,border=,flags=50\ 331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=39,g\ =118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 [Cntl + クリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?+Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,align\ mentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFacto\ ry$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 java.awt.event.MouseEvent[MOUSE_RELEASED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0\ .0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Syst\ emColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 java.awt.event.MouseEvent[MOUSE_CLICKED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0.\ 0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Syste\ mColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 [Command + クリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?+Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0\ .0,alignmentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaIm\ ageFactory$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 java.awt.event.MouseEvent[MOUSE_RELEASED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alig\ nmentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFact\ ory$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 java.awt.event.MouseEvent[MOUSE_CLICKED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,align\ mentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFacto\ ry$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() : 0x04 InputEvent.CTRL_MASK : 0x02 [ただのクリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(57,4),absolute(971,484),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY =0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Sy stemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 java.awt.event.MouseEvent[MOUSE_RELEASED,(57,4),absolute(971,484),button=1,modifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0.0,border=,flags=5 0331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=39, g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 java.awt.event.MouseEvent[MOUSE_CLICKED,(57,4),absolute(971,484),button=1,modifiers=Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0.0,border=,flags=50 331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=39,g =118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x10 [Cntl + クリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?+Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,align mentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFacto ry$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 java.awt.event.MouseEvent[MOUSE_RELEASED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0 .0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Syst emColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 java.awt.event.MouseEvent[MOUSE_CLICKED,(57,40),absolute(971,520),button=1,modifiers=?+Button1,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alignmentY=0. 0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFactory$Syste mColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x12 [Command + クリック] java.awt.event.MouseEvent[MOUSE_PRESSED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?+Button1,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0 .0,alignmentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaIm ageFactory$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 java.awt.event.MouseEvent[MOUSE_RELEASED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,alig nmentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFact ory$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 java.awt.event.MouseEvent[MOUSE_CLICKED,(56,72),absolute(970,552),button=1,modifiers=?+Button1+Button3,extModifiers=?,clickCount=1] on example.MainPanel$1[,0,0,106x193,alignmentX=0.0,align mentY=0.0,border=,flags=50332008,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFacto ry$SystemColorProxy[r=39,g=118,b=218],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=8,layoutOrientation=0] e.getModifiers() : 0x14 }} ちなみに e.getModifiers() | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), を e.getModifiers() | 0x01 , #0x01 はShift とかに無理矢理するとクリックだけでShiftと同じ動作になるんですけどね・・・ もう意味が分かりません -- [[nsby]] &new{2011-07-27 (水) 10:52:51}; ちなみに e.getModifiers() | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), を e.getModifiers() | 0x01 , #0x01 は Shift とかに無理矢理するとクリックだけでShiftと同じ動作になるんですけどね・・・。もう意味が分かりません。 -- [[nsby]] &new{2011-07-27 (水) 10:52:51}; - あら見づらくなりすみません。 -- [[nsby]] &new{2011-07-27 (水) 10:54:02}; - ようするに、convertMouseEvent内のe.getModifiers() に 0x02/0x04をorしてもOSXでは無視されてるようです。なぜなんでしょう? -- [[nsby]] &new{2011-07-27 (水) 10:58:53}; - ようするに、convertMouseEvent内のe.getModifiers() に 0x02/0x04をorしてもOSXでは無視されてるようです。なぜなんでしょう? -- [[nsby]] &new{2011-07-27 (水) 13:13:20}; -- ログ(勝手にすこし整形しました)どうもです。たしかにうまくいっているっぽいのに、不思議な感じですね。%%InputEvent.CTRL_DOWN_MASKとInputEvent.CTRL_MASKの違い?%% もうすこし調べてみます。 -- [[aterai]] &new{2011-07-27 (水) 15:04:05}; - メモ: [http://developer.apple.com/jp/technotes/tn2042.html Tailoring Java Applications for Mac OS X] -- [[aterai]] &new{2011-07-27 (水) 15:18:46}; #comment