Swing/PinTabbedPane のバックアップの現在との差分(No.6)
- category: swing folder: PinTabbedPane title: JTabbedPaneのタブを固定する tags: [JTabbedPane, JPopupMenu, JLabel] author: aterai pubdate: 2011-12-05T14:18:41+09:00 description: JTabbedPaneにJPopupMenuを追加して、指定したタブのタイトルと位置を変更し、タブの固定を行います。 image:
概要
概要
JTabbedPane
にJPopupMenu
を追加して、指定したタブのタイトルと位置を変更し、タブの固定を行います。
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
JCheckBoxMenuItem pinTabMenuItem = new JCheckBoxMenuItem(new AbstractAction("pin tab") {
#spanend
#spanadd
pinTabMenuItem = new JCheckBoxMenuItem(new AbstractAction("pin tab") {
#spanend
@Override public void actionPerformed(ActionEvent e) {
JTabbedPane t = (JTabbedPane) getInvoker();
JCheckBoxMenuItem check = (JCheckBoxMenuItem) e.getSource();
int idx = t.getSelectedIndex();
int idx = t.getSelectedIndex();
Component cmp = t.getComponentAt(idx);
Component tab = t.getTabComponentAt(idx);
Icon icon = t.getIconAt(idx);
String tip = t.getToolTipTextAt(idx);
boolean flg = t.isEnabledAt(idx);
Icon icon = t.getIconAt(idx);
String tip = t.getToolTipTextAt(idx);
boolean flg = t.isEnabledAt(idx);
int i;
if (check.isSelected()) {
for (i = 0; i < idx; i++) {
String s = t.getTitleAt(i);
if (s == null || s.length() == 0) {
if (s == null || s.isEmpty()) {
continue;
}
break;
}
} else {
for (i = t.getTabCount() - 1; i > idx; i--) {
String s = t.getTitleAt(i);
if (s != null && s.length() > 0) {
if (s != null && !s.isEmpty()) {
continue;
}
break;
}
}
t.remove(idx);
t.insertTab(check.isSelected() ? "" : tip, icon, cmp, tip, i);
t.setTabComponentAt(i, tab);
t.setEnabledAt(i, flg);
if (flg) {
t.setSelectedIndex(i);
}
}
});
View in GitHub: Java, Kotlin解説
解説
- タブを固定
- タブタイトルを空にする
- タブの位置を左側に移動
- 固定したタブは削除しない
- タブの位置を最も左に存在する未固定タブの左隣りに移動
- 固定したタブは削除不可に設定
- タブの固定を解除
- タブタイトルを
TooltipText
から復元する - タブの位置を固定されていないタブの右側に移動
- タブタイトルを
- 注:
-
TabPlacement
:LEFT
,RIGHT
は考慮していない - タブの位置を最も左に存在する未固定タブの右隣りに移動
-
-
TabPlacement
のLEFT
、RIGHT
は考慮していない