TITLE:JTextFieldの角を丸める
#navi(../)
RIGHT:Posted by &author(aterai); at 2010-10-04
*JTextFieldの角を丸める [#f7c187e5]
角丸のJTextFieldを作成します。コードは[http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html Unleash Your Creativity with Swing and the Java 2D API!]、[http://forums.sun.com/thread.jspa?threadID=260846 Swing - custom JTextField]から引用しています。

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

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTSMYm3vgI/AAAAAAAAAiY/37FVcZLSXI0/s800/RoundedTextField.png)

**サンプルコード [#ecbd0878]
#code{{
JTextField textField01 = new JTextField(20) {
  //Unleash Your Creativity with Swing and the Java 2D API!
  //http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html
  @Override protected void paintComponent(Graphics g) {
    if(!isOpaque()) {
      int w = getWidth();
      int h = getHeight();
      Graphics2D g2 = (Graphics2D)g.create();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                          RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(UIManager.getColor("TextField.background"));
      g2.fillRoundRect(0, 0, w-1, h-1, h, h);
      g2.setColor(Color.GRAY);
      g2.drawRoundRect(0, 0, w-1, h-1, h, h);
      g2.dispose();
    }
    super.paintComponent(g);
  }
};
textField01.setOpaque(false);
textField01.setBackground(new Color(0,0,0,0)); //Nimbus
textField01.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
textField01.setText("aaaaaaaaaaa");
}}

**解説 [#sc0a1c68]
-上
--JTextField#paintComponent(...)をオーバーライド、BorderをEmptyBorder、JTextField#setOpaque(false);
--参考: [http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html Unleash Your Creativity with Swing and the Java 2D API!]

-下
--RoundTextUIを作成して、JTextField#setUI(...)で設定
--参考: [http://forums.sun.com/thread.jspa?threadID=260846 Swing - custom JTextField]

#code{{
//http://forums.sun.com/thread.jspa?threadID=260846
class RoundTextUI extends BasicTextFieldUI {
  public static ComponentUI createUI(JComponent c) {
    return new RoundTextUI();
  }
  @Override public void installUI(JComponent c) {
    super.installUI(c);
    c.setBorder(new RoundBorder());
    c.setOpaque(false);
    c.setBackground(new Color(0,0,0,0)); //Nimbus
  }
  @Override protected void paintSafely(Graphics g) {
    JComponent c = getComponent();
    if(!c.isOpaque()) {
      Graphics2D g2 = (Graphics2D)g.create();
      g2.setColor(UIManager.getColor("TextField.background"));
      g2.fillRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1,
                             c.getHeight(),  c.getHeight());
      g2.dispose();
    }
    super.paintSafely(g);
  }
  private static class RoundBorder extends AbstractBorder {
    @Override public void paintBorder(
        Component c, Graphics g, int x, int y, int width, int height) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setColor(Color.GRAY);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                          RenderingHints.VALUE_ANTIALIAS_ON);
      g2.drawRoundRect(x, y, width-1, height-1, height, height);
    }
    @Override public Insets getBorderInsets(Component c) {
      return new Insets(4, 8, 4, 8);
    }
    @Override public Insets getBorderInsets(Component c, Insets insets) {
      insets.left = insets.right = 8;
      insets.top = insets.bottom = 4;
      return insets;
    }
  }
}
}}

**参考リンク [#mcc5799a]
-[http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html Unleash Your Creativity with Swing and the Java 2D API!]
-[http://forums.sun.com/thread.jspa?threadID=260846 Swing - custom JTextField]
-[http://www.coderanch.com/t/336048/GUI/java/Border-rounded-JTextField Border on an rounded JTextField? (Swing / AWT / SWT / JFace forum at JavaRanch)]

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