JDesktopPane内のJInternalFrameをJTabbedPaneのタブと入れ替える
Total: 4532, Today: 1, Yesterday: 0
Posted by aterai at
Last-modified:
Summary
JDesktopPaneとJTabbedPaneをCardLayoutで切り替えるとき、その内部のJInternalFrameとタブもすべて入れ替えます。
Screenshot

Advertisement
Source Code Examples
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, KotlinDescription
JInternalFrameをJTabbedPaneのタブに変換するJDesktopPane#getAllFrames()で取得した順番はJInternalFrameがアイコン化されているなどの状態で変化するので、タイトルでソートしてからJTabbedPaneに追加JInternalFrame自体はそのままでContentPaneのみタブコンポーネントに変換する- 選択状態の
JInternalFrameをJDesktopPane#getSelectedFrame()メソッドで検索し、変換先のタブも選択状態になるようJTabbedPane#setSelectedIndex(...)メソッドで設定する
JTabbedPaneのタブをJInternalFrameに変換するJDesktopPaneには、位置やサイズ、アイコン化状態などを保存した状態でJInternalFrameが残っているJTabbedPane側ではタブの削除を許可していない
JInternalFrameのタイトルと同じタブのインデックスをJTabbedPane#indexOfTab(String)メソッドで検索し、そのコンポーネントをContentPaneとしてJInternalFrameに戻す
Reference
- JDesktopPane#getSelectedFrame() (Java Platform SE 8)
- JTabbedPane#indexOfTab(String) (Java Platform SE 8)