Google Tag Manager

2012/07/02

Rounded corner JComboBox border

Code

class RoundedCornerBorder extends AbstractBorder {
  @Override public void paintBorder(
      Component c, Graphics g, int x, int y, int width, int height) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    int r = 12;
    int w = width  - 1;
    int h = height - 1;
    Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
    Container parent = c.getParent();
    if (parent != null) {
      g2.setColor(parent.getBackground());
      Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
      corner.subtract(round);
      g2.fill(corner);
    }
    g2.setPaint(c.getForeground());
    g2.draw(round);
    g2.dispose();
  }

  @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;
  }
}

class KamabokoBorder extends RoundedCornerBorder {
  @Override public void paintBorder(
      Component c, Graphics g, int x, int y, int width, int height) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    int r = 12;
    int w = width  - 1;
    int h = height - 1;
    Path2D p = new Path2D.Float();
    p.moveTo(x, y + h);
    p.lineTo(x, y + r);
    p.quadTo(x, y, x + r, y);
    p.lineTo(x + w - r, y);
    p.quadTo(x + w, y, x + w, y + r);
    p.lineTo(x + w, y + h);
    p.closePath();
    Area round = new Area(p);
    Container parent = c.getParent();
    if (parent != null) {
      g2.setColor(parent.getBackground());
      Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
      corner.subtract(round);
      g2.fill(corner);
    }
    g2.setPaint(c.getForeground());
    g2.draw(round);
    g2.dispose();
  }
}

References

7 comments:

  1. master such apology to bother you again, but I have a little problem I want to customize a JPopupMenu containing JMenu, and attempt to remove the background by the following jmenu.getPopupMenu (). setOpaque (false), but does not work there will be a form of make it transparent or to take another look, thank you very much for your help and for your time

    ReplyDelete
  2. Hi, HANNIBAL T.
    Here is example: http://terai.xrea.jp/data/swing/TranslucentMenuTest.java

    but there are a whole lot of issues to contend with.
    (Heavyweight Lightweight popup, LookAndFeel dependency, bug?: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4688783 )

    Hope that helps.

    ReplyDelete
  3. but here they are doing with JTree and building the menu, what I try to do is make transparent the JPopupMenu that has the JMenu to add the menu I have a bone jmenuiten
      JMenuBar bar = new JMenuBar ()
      bar.add (JMenu);
    JMenu menu = new JMenu ();
    the popup menu is what I want done transparently
    menu.getPopupMenu (). setOpaque (false);
    I put setOpaque but does not work
    JMenuItem item = new JMenuItem ();
    menu.add (item;)
    I want to know if there are ways to make it transparent

    ReplyDelete
  4. I think the same way can use for JPopupMenu and JMenuBar: TranslucentMenuTest2.java
    (using JDK 1.7.0_04 on Windows 7)

    ReplyDelete
  5. thank you very much TERAI Atsuhiro really are a master in java

    ReplyDelete
  6. Thanks TERAI Atsuhiro so much.
    http://terai.xrea.jp/data/swing/TranslucentMenuTest2.java is so useful

    ReplyDelete
  7. Glad to help. As the comment in this source code file, "Translucent and Shaped Swing Windows | Java.net" should also help: http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html

    ReplyDelete