TITLE:JTabbedPaneのTabTitleを左揃えに変更

Posted by terai at 2010-10-11

JTabbedPaneのTabTitleを左揃えに変更

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

  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

class MyTabbedPaneUI extends javax.swing.plaf.metal.MetalTabbedPaneUI {
  @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;
    //...
  }
}

解説

public ButtonTabComponent(final JTabbedPane pane) {
  //unset default FlowLayout' gaps
  //super(new FlowLayout(FlowLayout.LEFT, 0, 0));
  super(new BorderLayout(0, 0));
  //...

参考リンク

コメント