概要

NimbusLookAndFeelを設定したJTabbedPaneのタブテキストとアイコンの水平方向の配置方法やその間隔を変更します。

サンプルコード

// UIManager.put("TabbedPane.textIconGap", 4);
JTabbedPane tabbedPane0 = new JTabbedPane() {
  @Override public void updateUI() {
    super.updateUI();
    System.out.println(UIManager.getInt("TabbedPane.textIconGap"));
    // UIDefaults d = new UIDefaults();
    // d.put("TabbedPane.textIconGap", 4);
    // putClientProperty("Nimbus.Overrides", d);
    // putClientProperty("Nimbus.Overrides.InheritDefaults", true);
  }
};
add(makeTitledPanel("Default addTab(title, icon, c)", initTabbedPane(tabbedPane0)));

JTabbedPane tabbedPane1 = new JTabbedPane() {
  @Override public void insertTab(
      String title, Icon icon, c component, String tip, int index) {
    super.insertTab(title, icon, c, tip, index);
    JLabel label = new JLabel(title, icon, SwingConstants.LEADING);
    setTabComponentAt(getTabCount() - 1, label);
  }
};
add(makeTitledPanel("TabComponent + JLabel + LEADING", initTabbedPane(tabbedPane1)));
View in GitHub: Java, Kotlin

解説

  • Default addTab(title, icon, c)
    • JTabbedPane#addTab(title, icon, c)メソッドでタブテキストとアイコンを追加した場合、NimbusLookAndFeelでのデフォルト水平方向配置はテキスト、アイコンの順番になる
    • MetalLookAndFeelWindowsLookAndFeelのデフォルトはアイコン、テキストの順番になる
    • JTabbedPane#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)を設定すればアイコン、テキストの順番になるが、一行タブエリア内でのタブ配置が右寄せになる
    • NimbusLookAndFeelでのタブテキストとアイコンの間隔は未設定でデフォルトは0になっている
      • MetalLookAndFeelWindowsLookAndFeelのデフォルトは4
      • NimbusLookAndFeelでもJMenuItemなどのテキストとアイコンの間隔のデフォルトは4
      • NimbusLookAndFeelでもタブテキストとアイコンの間隔はUIManager.put("TabbedPane.textIconGap", 4)UIDefaultsで変更可能
  • TabComponent + JLabel + LEADING
    • new JLabel(title, icon, SwingConstants.LEADING)などでテキストとアイコンをもつJLabelを作成し、JTabbedPane#setTabComponentAt(...)で設定することでLookAndFeelに依存せずにタブテキストとアイコンの水平方向配置方法などを使用可能

参考リンク

コメント