February 06, 2012

Round bordered TextField in java

How to a make a round border or round corner text field / text area in java swing


Frequently asked questions like how is it possible to make my javatexfield and textarea either round corner or round border. Here is a solution for that, so please go through the code and reply
/*
*@author Elizebath
*/
public class RoundJTextField extends JTextField {
private Shape shaping;
public RoundJTextField(int size) {
super(size);
setOpaque(false);
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shaping == null || !shaping.getBounds().equals(getBounds())) {
shaping = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shaping.contains(x, y);
}
}

JTextField t = new RoundJTextField(20);


more

Facebook comments