• category: swing folder: TabTitleAlignment title: JTabbedPaneのTabTitleを左揃えに変更 tags: [JTabbedPane, Alignment, JButton] author: aterai pubdate: 2010-10-11T18:19:33+09:00 description: JTabbedPaneのTabTitleをデフォルトに中央揃えから左揃えに変更します。 description: JTabbedPaneのTabTitleの揃えをデフォルトの中央揃えから左揃えに変更します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTU2Jp4a6I/AAAAAAAAAms/x6g2ML8eyyQ/s800/TabTitleAlignment.png

概要

JTabbedPaneTabTitleをデフォルトに中央揃えから左揃えに変更します。

概要

JTabbedPaneTabTitleの揃えをデフォルトの中央揃えから左揃えに変更します。

サンプルコード

サンプルコード

#spandel
class MyTabbedPaneUI extends javax.swing.plaf.metal.MetalTabbedPaneUI {
#spanend
#spanadd
class MyTabbedPaneUI extends MetalTabbedPaneUI {
#spanend
  @Override protected void layoutLabel(int tabPlacement,
                                       FontMetrics metrics, int tabIndex,
                                       String title, Icon icon,
                                       Rectangle tabRect, Rectangle iconRect,
                                       Rectangle textRect, boolean isSelected ) {
    textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
    //...
    // ...
    SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
                                       metrics, title, icon,
                                       SwingUtilities.CENTER,
                                       SwingUtilities.LEFT, //CENTER, <----
                                       SwingUtilities.CENTER,
                                       SwingUtilities.TRAILING,
                                       tabRect,
                                       iconRect,
                                       textRect,
                                       textIconGap);
    tabPane.putClientProperty("html", null);
    textRect.translate(tabInsets.left, 0); //<----
    textRect.width -= tabInsets.left+tabInsets.right;
    //...
    textRect.width -= tabInsets.left + tabInsets.right;
    // ...
  }
}
View in GitHub: Java, Kotlin

解説

解説

  • JTabbedPaneのタブを等幅にしてタイトルをクリップButtonTabComponent.javaを変更してタイトルを左揃え、TabButton(閉じる)を右揃え
    public ButtonTabComponent(final JTabbedPane pane) {
      //unset default FlowLayout' gaps
      //super(new FlowLayout(FlowLayout.LEFT, 0, 0));
      // unset default FlowLayout' gaps
      // super(new FlowLayout(FlowLayout.LEFT, 0, 0));
      super(new BorderLayout(0, 0));
      //...
      //add(button);
      // ...
      // add(button);
      add(button, BorderLayout.EAST);
      //...
      // ...
    

参考リンク

参考リンク

コメント

コメント