• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TitledBorderとMatteBorderを使用してTitledSeparatorを作成する
#navi(../)
RIGHT:Posted by [[aterai]] at 2012-08-27
*TitledBorderとMatteBorderを使用してTitledSeparatorを作成する [#ub2f11b2]
TitledBorderとMatteBorderを使用してTitle付きのSeparatorを作成します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(https://lh3.googleusercontent.com/-sRtVayYL37Q/UDs_iiXRk7I/AAAAAAAABRk/71qZoe9vM60/s800/TitledSeparator.png)

**サンプルコード [#a13df73e]
#code(link){{
private static Border makeTitledSeparator(
    String title, final Color color, final Color darker, final Color brighter,
    final int height, int titlePosition) {
  return BorderFactory.createTitledBorder(
      BorderFactory.createMatteBorder(height, 0, 0, 0, new Icon() {
    private int width = -1;
    private Paint painter1, painter2;
    @Override public void paintIcon(Component c, Graphics g, int x, int y) {
      int w = c.getWidth();
      if(w!=width || painter1==null || painter2==null) {
        width = w;
        Point2D start = new Point2D.Float(0f, 0f);
        Point2D end   = new Point2D.Float((float)width, 0f);
        float[] dist  = {0.0f, 1.0f};
        painter1 = new LinearGradientPaint(start, end, dist, new Color[] {darker,   color});
        painter2 = new LinearGradientPaint(start, end, dist, new Color[] {brighter, color});
      }
      int h = getIconHeight()/2;
      Graphics2D g2  = (Graphics2D)g.create();
      g2.setPaint(painter1);
      g2.fillRect(x, y,   width, getIconHeight());
      g2.setPaint(painter2);
      g2.fillRect(x, y+h, width, getIconHeight()-h);
      g2.dispose();
private static JComponent makeTitledSeparator(
    final String title, final Color target,
    final int height, final int titlePosition) {
  return new JLabel() {
    @Override public Dimension getMaximumSize() {
      Dimension d = super.getPreferredSize();
      d.width = Short.MAX_VALUE;
      return d;
    }
    @Override public int getIconWidth()  { return 200; } //dummy width
    @Override public int getIconHeight() { return height; }
  }), title, TitledBorder.DEFAULT_JUSTIFICATION, titlePosition);
    private Color color;
    @Override public void updateUI() {
      super.updateUI();
      color = null;
      setBorder(BorderFactory.createTitledBorder(
          BorderFactory.createMatteBorder(height, 0, 0, 0, new Icon() {
        private int width = -1;
        private Paint painter1, painter2;
        @Override public void paintIcon(Component c, Graphics g, int x, int y) {
          int w = c.getWidth();
          if(w!=width || painter1==null || painter2==null || color==null) {
            width = w;
            Point2D start = new Point2D.Float(0f, 0f);
            Point2D end   = new Point2D.Float((float)width, 0f);
            float[] dist  = {0.0f, 1.0f};
            color = getBackground();
            color = color==null ? UIManager.getColor("Panel.background") : color;
            Color tc = target==null ? color : target;
            painter1 = new LinearGradientPaint(start, end, dist, new Color[] {tc.darker(),   color});
            painter2 = new LinearGradientPaint(start, end, dist, new Color[] {tc.brighter(), color});
          }
          int h = getIconHeight()/2;
          Graphics2D g2  = (Graphics2D)g.create();
          g2.setPaint(painter1);
          g2.fillRect(x, y,   width, getIconHeight());
          g2.setPaint(painter2);
          g2.fillRect(x, y+h, width, getIconHeight()-h);
          g2.dispose();
        }
        @Override public int getIconWidth()  { return 200; } //dummy width
        @Override public int getIconHeight() { return height; }
      }), title, TitledBorder.DEFAULT_JUSTIFICATION, titlePosition));
      //System.out.println(getInsets());
    }
  };
}
}}

**解説 [#o6cd3ee7]
TitledBorderと左下右のインセットが0(上インセットのみ設定)で、タイルアイコンでグラデーションを行うMatteBorderを組み合わせて、TitledSeparatorを作成しています。
TitledBorder と、左下右のインセットが0(上インセットのみ設定)でタイルアイコンでグラデーションを行うMatteBorderを組み合わせ、これを空のJLabelに設定することで、TitledSeparator を作成しています。

- 上
-- タイトルの垂直位置がデフォルト(TitledBorder.DEFAULT_POSITION)で、Separator上に重なるように表示
- 中
-- タイトルの垂直位置が上(TitledBorder.ABOVE_TOP)で、Separatorの上に表示
- 下
-- JSeparator を使用

----
注: 縦のセパレーターには未対応

**参考リンク [#oc439ee0]
-[[TitledBorderのタイトル位置>Swing/TitledBorder]]
-[[Separatorのグラデーション>Swing/Gradient]]

**コメント [#i31c8183]
#comment