Swing/SwapInternalFramesWithTabs のバックアップの現在との差分(No.2)
- category: swing folder: SwapInternalFramesWithTabs title: JDesktopPane内のJInternalFrameをJTabbedPaneのタブと入れ替える tags: [JDesktopPane, JInternalFrame, JTabbedPane, CardLayout] author: aterai pubdate: 2015-04-13T00:00:05+09:00 description: JDesktopPaneとJTabbedPaneをCardLayoutで切り替えるとき、その内部のJInternalFrameとタブもすべて入れ替えます。 image:
概要
JDesktopPane
とJTabbedPane
をCardLayout
で切り替えるとき、その内部のJInternalFrame
とタブもすべて入れ替えます。
Screenshot
Advertisement
サンプルコード
Action swapAction = new AbstractAction("JDesktopPane <-> JTabbedPane") {
@Override public void actionPerformed(ActionEvent e) {
if (((AbstractButton) e.getSource()).isSelected()) {
Arrays.stream(desktopPane.getAllFrames())
.sorted(Comparator.comparing(JInternalFrame::getTitle))
.forEach(f -> tabbedPane.addTab(f.getTitle(), f.getFrameIcon(), f.getContentPane()));
JInternalFrame sf = desktopPane.getSelectedFrame();
if (Objects.nonNull(sf)) {
tabbedPane.setSelectedIndex(tabbedPane.indexOfTab(sf.getTitle()));
}
cardLayout.show(panel, tabbedPane.getClass().getName());
} else {
Arrays.stream(desktopPane.getAllFrames())
.forEach(f -> f.setContentPane(
(Container) tabbedPane.getComponentAt(tabbedPane.indexOfTab(f.getTitle()))));
cardLayout.show(panel, desktopPane.getClass().getName());
}
}
};
View in GitHub: Java, Kotlin解説
-
JInternalFrame
をタブに変換する-
JDesktopPane#getAllFrames()
で取得した順番は、JInternalFrame
がアイコン化されているなどの状態で変化するので、タイトルでソートしてからJTabbedPane
に追加 -
JInternalFrame
自体はそのままで、ContentPane
のみタブコンポーネントに変換する - 選択状態の
JInternalFrame
をJDesktopPane#getSelectedFrame()
で検索し、変換先のタブも選択状態になるようJTabbedPane#setSelectedIndex(...)
で設定する
-
- タブを
JInternalFrame
に変換する -
JInternalFrame
をJTabbedPane
のタブに変換する-
JDesktopPane#getAllFrames()
で取得した順番はJInternalFrame
がアイコン化されているなどの状態で変化するので、タイトルでソートしてからJTabbedPane
に追加 -
JInternalFrame
自体はそのままでContentPane
のみタブコンポーネントに変換する - 選択状態の
JInternalFrame
をJDesktopPane#getSelectedFrame()
メソッドで検索し、変換先のタブも選択状態になるようJTabbedPane#setSelectedIndex(...)
メソッドで設定する
-
-
JTabbedPane
のタブをJInternalFrame
に変換するJDesktopPane
には、位置やサイズ、アイコン化状態などを保存した状態でJInternalFrame
が残っているJTabbedPane
側ではタブの削除を許可していない
-
JInternalFrame
のタイトルと同じタブのインデックスをJTabbedPane#indexOfTab(String)
で検索し、そのコンポーネントをContentPane
としてJInternalFrame
に戻す -
JInternalFrame
のタイトルと同じタブのインデックスをJTabbedPane#indexOfTab(String)
メソッドで検索し、そのコンポーネントをContentPane
としてJInternalFrame
に戻す
参考リンク
- JDesktopPane#getSelectedFrame() (Java Platform SE 8)
- JTabbedPane#indexOfTab(String) (Java Platform SE 8)