Swing/RearrangeToolBarIcon のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/RearrangeToolBarIcon へ行く。
- 1 (2013-04-08 (月) 01:18:50)
- 2 (2013-04-08 (月) 21:04:19)
- 3 (2013-09-20 (金) 04:34:20)
- 4 (2013-10-11 (金) 12:52:39)
- 5 (2014-11-22 (土) 04:06:27)
- 6 (2014-12-08 (月) 00:01:08)
- 7 (2015-03-06 (金) 19:04:54)
- 8 (2015-03-25 (水) 16:57:08)
- 9 (2015-04-05 (日) 23:09:28)
- 10 (2016-05-16 (月) 11:46:29)
- 11 (2017-08-02 (水) 15:33:01)
- 12 (2018-02-24 (土) 19:51:30)
- 13 (2018-08-03 (金) 14:07:29)
- 14 (2019-09-17 (火) 19:15:30)
- 15 (2021-05-05 (水) 05:12:09)
- 16 (2024-02-02 (金) 11:59:59)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JToolBarに配置したアイコンをドラッグして並べ替える #navi(../) #tags(JToolBar, JLabel, Icon, DragAndDrop, MouseListener, MouseMotionListener, JWindow) RIGHT:Posted by &author(aterai); at 2013-04-08 * JToolBarに配置したアイコンをドラッグして並べ替える [#o6c6a477] ``JToolBar``に配置したアイコンをドラッグ&ドロップで並べ替えます。 --- category: swing folder: RearrangeToolBarIcon title: JToolBarに配置したアイコンをドラッグして並べ替える tags: [JToolBar, JLabel, Icon, DragAndDrop, MouseListener, MouseMotionListener, JWindow] author: aterai pubdate: 2013-04-08T01:18:50+09:00 description: JToolBarに配置したアイコンをドラッグ&ドロップで並べ替えます。 image: https://lh6.googleusercontent.com/-bxLoJ6g9K_I/UWBOghG3kxI/AAAAAAAABpQ/tvVELkL1RV0/s800/RearrangeToolBarIcon.png hreflang: href: https://java-swing-tips.blogspot.com/2013/04/rearrange-jtoolbar-icon-by-drag-and-drop.html lang: en --- * 概要 [#summary] `JToolBar`に配置したアイコンをドラッグ&ドロップで並べ替えます。 - &jnlp; - &jar; - &zip; #download(https://lh6.googleusercontent.com/-bxLoJ6g9K_I/UWBOghG3kxI/AAAAAAAABpQ/tvVELkL1RV0/s800/RearrangeToolBarIcon.png) #ref(https://lh6.googleusercontent.com/-bxLoJ6g9K_I/UWBOghG3kxI/AAAAAAAABpQ/tvVELkL1RV0/s800/RearrangeToolBarIcon.png) ** サンプルコード [#o5961551] * サンプルコード [#sourcecode] #code(link){{ class DragHandler extends MouseAdapter{ class DragHandler extends MouseAdapter { private final JWindow window = new JWindow(); private Component draggingComonent = null; private Component draggingComponent = null; private int index = -1; private Component gap = Box.createHorizontalStrut(24); private Point startPt = null; private int gestureMotionThreshold = DragSource.getDragThreshold(); public DragHandler() { window.setBackground(new Color(0, true)); window.setBackground(new Color(0x0, true)); } @Override public void mousePressed(MouseEvent e) { JComponent parent = (JComponent)e.getComponent(); if(parent.getComponentCount()<=1) { JComponent parent = (JComponent) e.getComponent(); if (parent.getComponentCount() <= 1) { startPt = null; return; } startPt = e.getPoint(); } @Override public void mouseDragged(MouseEvent e) { @Override public void mouseDragged(MouseEvent e) { Point pt = e.getPoint(); JComponent parent = (JComponent)e.getComponent(); int t = Math.sqrt(Math.pow(pt.x-startPt.x, 2)+Math.pow(pt.y-startPt.y, 2)) if(startPt != null && t>gestureMotionThreshold) { JComponent parent = (JComponent) e.getComponent(); if (startPt != null && startPt.distance(pt) > gestureMotionThreshold) { startPt = null; Component c = parent.getComponentAt(pt); index = parent.getComponentZOrder(c); if(c == parent || index < 0) { if (c == parent || index < 0) { return; } draggingComonent = c; draggingComponent = c; parent.remove(draggingComonent); parent.remove(draggingComponent); parent.add(gap, index); parent.revalidate(); parent.repaint(); window.add(draggingComonent); window.add(draggingComponent); window.pack(); Dimension d = draggingComonent.getPreferredSize(); Point p = new Point(pt.x - d.width/2, pt.y - d.height/2); Dimension d = draggingComponent.getPreferredSize(); Point p = new Point(pt.x - d.width / 2, pt.y - d.height / 2); SwingUtilities.convertPointToScreen(p, parent); window.setLocation(p); window.setVisible(true); return; } if(!window.isVisible() || draggingComonent==null) { if (!window.isVisible() || draggingComponent == null) { return; } Dimension d = draggingComonent.getPreferredSize(); Point p = new Point(pt.x - d.width/2, pt.y - d.height/2); Dimension d = draggingComponent.getPreferredSize(); Point p = new Point(pt.x - d.width / 2, pt.y - d.height / 2); SwingUtilities.convertPointToScreen(p, parent); window.setLocation(p); for(int i=0; i<parent.getComponentCount(); i++) { for (int i = 0; i < parent.getComponentCount(); i++) { Component c = parent.getComponent(i); Rectangle r = c.getBounds(); Rectangle r1 = new Rectangle(r.x, r.y, r.width/2, r.height); Rectangle r2 = new Rectangle(r.x+r.width/2, r.y, r.width/2, r.height); if(r1.contains(pt)) { if(c==gap) { Rectangle r1 = new Rectangle(r.x, r.y, r.width / 2, r.height); Rectangle r2 = new Rectangle(r.x + r.width / 2, r.y, r.width / 2, r.height); if (r1.contains(pt)) { if (c == gap) { return; } int n = i-1>=0 ? i : 0; int n = i - 1 >= 0 ? i : 0; parent.remove(gap); parent.add(gap, n); parent.revalidate(); parent.repaint(); return; }else if(r2.contains(pt)) { if(c==gap) { } else if (r2.contains(pt)) { if (c == gap) { return; } parent.remove(gap); parent.add(gap, i); parent.revalidate(); parent.repaint(); return; } } parent.remove(gap); parent.revalidate(); parent.repaint(); } @Override public void mouseReleased(MouseEvent e) { @Override public void mouseReleased(MouseEvent e) { startPt = null; if(!window.isVisible() || draggingComonent==null) { if (!window.isVisible() || draggingComponent == null) { return; } Point pt = e.getPoint(); JComponent parent = (JComponent)e.getComponent(); JComponent parent = (JComponent) e.getComponent(); Component cmp = draggingComonent; draggingComonent = null; Component cmp = draggingComponent; draggingComponent = null; window.setVisible(false); for(int i=0; i<parent.getComponentCount(); i++) { for (int i = 0; i < parent.getComponentCount(); i++) { Component c = parent.getComponent(i); Rectangle r = c.getBounds(); Rectangle r1 = new Rectangle(r.x, r.y, r.width/2, r.height); Rectangle r2 = new Rectangle(r.x+r.width/2, r.y, r.width/2, r.height); if(r1.contains(pt)) { int n = i-1>=0 ? i : 0; Rectangle r1 = new Rectangle(r.x, r.y, r.width / 2, r.height); Rectangle r2 = new Rectangle(r.x + r.width / 2, r.y, r.width / 2, r.height); if (r1.contains(pt)) { int n = i - 1 >= 0 ? i : 0; parent.remove(gap); parent.add(cmp, n); parent.revalidate(); parent.repaint(); return; }else if(r2.contains(pt)) { } else if (r2.contains(pt)) { parent.remove(gap); parent.add(cmp, i); parent.revalidate(); parent.repaint(); return; } } if(parent.getBounds().contains(pt)) { if (parent.getBounds().contains(pt)) { parent.remove(gap); parent.add(cmp); }else{ } else { parent.remove(gap); parent.add(cmp, index); } parent.revalidate(); parent.repaint(); } } }} ** 解説 [#nac20b59] 上記のサンプルでは、``JToolBar``に``MouseListener``と``MouseMotionListener``を追加して、``JLabel``に配置したアイコンを並べ替えています。 * 解説 [#explanation] 上記のサンプルでは、`JToolBar`に`MouseListener`と`MouseMotionListener`を追加して、`JLabel`に配置したアイコンを並べ替えています。 - ドラッグされて移動対象になった``JLabel``は、``JToolBar``から削除され、透明な``JWindow``に移動 -- ``JWindow``なので、元のフレームの外側にも移動可能 - ドロップする位置に``JWindow``が移動してきた場合、``JToolBar``に``Box.createHorizontalStrut(24);``で作成したアイコンと同じ幅の``Box``を配置 - ``JLabel``がドロップされると、ダミーの``Box``は削除され、``JLabel``と入れ替え -- ``JWindow``は非表示に設定 -- ``JToolBar``以外の場所にドロップされた場合は、ドラッグ前の位置に``JLabel``を戻す - ドラッグされて移動対象になった`JLabel`(アイコン)は親`JToolBar`から削除され、透明な別`JWindow`に移動 -- `JWindow`なので元のフレームの外側でも表示可能 - ドロップする位置に`JWindow`が移動してきた場合、`JToolBar`に`Box.createHorizontalStrut(24);`で作成したアイコンと同じ幅の間隔維持用の仮`Box`を配置 - `JLabel`がドロップされると上記の仮`Box`は削除され`JLabel`と入れ替わる -- `JWindow`は非表示に設定 -- `JToolBar`以外の場所にドロップされた場合はドラッグ前の位置に`JLabel`を戻す ---- - 制限 -- ``JToolBar#setFloatable(false);``にしないとアイコンを移動できない -- ``JButton``などを移動できない -- 非表示のコンポーネント(``Box.createGlue()``など)がドラッグできてしまう -- ``Ubuntu``などで移動の描画がチラつく -- `JToolBar#setFloatable(false);`にしないとアイコンを移動できない -- `JButton`などを移動できない -- 非表示のコンポーネント(`Box.createGlue()`など)がドラッグできてしまう -- `Ubuntu`などで移動の描画がチラつく ** 参考リンク [#p7769453] * 参考リンク [#reference] - [[JFrameの外側でもドラッグアイコンを表示する>Swing/DragSourceMotionListener]] - [[JPanelの並び順をドラッグ&ドロップで入れ替える>Swing/RearrangeOrderOfPanels]] ** コメント [#x90de976] - メモ: ``JLayer``などを使用すれば、``JButton``の移動も可能…な気がする。 -- [[aterai]] &new{2013-04-08 (月) 21:04:19}; * コメント [#comment] #comment - メモ: `JLayer`などを使用すれば、`JButton`の移動も可能…な気がする。 -- &user(aterai); &new{2013-04-08 (月) 21:04:19}; #comment