Swing/ToolBarLayout のバックアップソース(No.20)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ToolBarLayout へ行く。
- 1 (2006-01-23 (月) 14:20:11)
- 2 (2006-01-23 (月) 16:49:43)
- 3 (2006-02-27 (月) 16:49:40)
- 4 (2006-03-10 (金) 18:34:11)
- 5 (2006-03-22 (水) 15:58:10)
- 6 (2006-07-10 (月) 09:37:09)
- 7 (2006-07-10 (月) 11:13:25)
- 8 (2007-08-05 (日) 21:40:17)
- 9 (2008-01-16 (水) 15:26:00)
- 10 (2008-03-14 (金) 21:36:07)
- 11 (2008-03-18 (火) 13:54:12)
- 12 (2008-10-27 (月) 15:02:56)
- 13 (2010-01-01 (金) 00:47:03)
- 14 (2011-06-04 (土) 05:12:45)
- 15 (2012-11-23 (金) 04:43:39)
- 16 (2013-03-15 (金) 16:49:02)
- 17 (2013-08-28 (水) 12:27:08)
- 18 (2013-09-17 (火) 18:42:50)
- 19 (2013-10-23 (水) 20:33:32)
- 20 (2013-11-05 (火) 19:30:35)
- 21 (2014-11-25 (火) 03:03:31)
- 22 (2015-11-01 (日) 22:34:32)
- 23 (2016-05-31 (火) 14:28:44)
- 24 (2016-11-04 (金) 14:58:55)
- 25 (2017-04-04 (火) 14:17:08)
- 26 (2017-10-17 (火) 13:17:03)
- 27 (2019-04-18 (木) 18:05:05)
- 28 (2021-02-02 (火) 08:09:43)
- 29 (2024-02-03 (土) 14:29:23)
TITLE:JToolBarでアイコンボタンを右寄せ #navi(../) #tags(JToolBar, JMenuBar, BoxLayout, JButton, Focus) RIGHT:Posted by &author(aterai); at 2006-01-23 * JToolBarでアイコンボタンを右寄せ [#dca938c3] `JToolBar`でアイコンボタンを右寄せ、下寄せで表示します。 - &jnlp; - &jar; - &zip; #ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVb-HPZjI/AAAAAAAAAno/dMILsHzlipk/s800/ToolBarLayout.png) ** サンプルコード [#z8d01839] #code(link){{ String path = "/toolbarButtonGraphics/general/"; URL url1 = getClass().getResource(path+"Copy24.gif"); URL url2 = getClass().getResource(path+"Cut24.gif"); URL url3 = getClass().getResource(path+"Help24.gif"); toolbar.add(createToolbarButton(url1)); toolbar.add(createToolbarButton(url2)); toolbar.add(Box.createGlue()); toolbar.add(createToolbarButton(url3)); }} #code{{ private static JButton createToolbarButton(URL url) { JButton b = new JButton(new ImageIcon(url)); b.setRequestFocusEnabled(false); return b; } }} ** 解説 [#u7eef7af] `JToolBar`や`JMenuBar`のデフォルトレイアウトは`BoxLayout`なので、`Box.createGlue`を間に挟むことでボタンやメニューの右寄せが可能です。 ボタンとボタンの間隔を固定値で空けたい場合は、`Box.createRigidArea`を使用します。`Box.createHorizontalStrut`や`Box.createVerticalStrut`を使うとツールバーが水平垂直に切り替わった時に、余計な余白が出来てしまうことがあります。 %%ツールバーが垂直になった場合のことも考えて、`VerticalGlue`も一緒に挿入していますが、特に問題ないようです。%% アイコンは、[http://java.sun.com/developer/techDocs/hi/repository/ Java look and feel Graphics Repository]の`jlfgr-1_0.jar`から読み込んでいます。 #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTVeG6fVBI/AAAAAAAAAns/II_0GGIdnNk/s800/ToolBarLayout1.png) `JDK 1.6`で、`JDK 1.5`のようなボタン表示(フォーカスを取得しない)にするには、`JButton#setRequestFocusEnabled(false)`(マウスクリックではフォーカスを取得しないが、キーボードからは許可)、または、`JButton#setFocusable(false)`とする必要があるようです。 - マウスクリックでツールバーボタンにフォーカスが移動すると、コピーボタンを押したらテキストエディタでの文字列選択状態がクリアされたり、参考の質問のような不具合が起こる - 参考: [https://forums.oracle.com/thread/1358842 Swing - JTextPane selection color problem]の camickr さんの投稿(2008/10/25 0:34) - `%JAVA_HOME%\demo\jfc\Notepad\src\Notepad.java` ** 参考リンク [#waff2dd4] - [http://java.sun.com/developer/techDocs/hi/repository/ Java look and feel Graphics Repository] - [https://forums.oracle.com/thread/1365522 Swing - Buttons like Netbeans'] - [http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#custom Customizing Menu Layout - How to Use Menus (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] ** コメント [#me4d31ab] - 環境によって、上記のサンプルが右寄せにならない場合もあるようです。 -- [[aterai]] &new{2006-03-22 (水) 15:58:10}; - `setRequestFocusEnabled(false)`、スクリーンショット更新。 -- [[aterai]] &new{2008-10-27 (月) 15:20:38}; #comment