Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java get Border color and size

JTextField tf = new JTextField();
tf.setBorder(new LineBorder(Color.red, 2));
Border border = tf.getBorder();

How can I get border color and size?

like image 691
user1221483 Avatar asked Apr 01 '12 15:04

user1221483


People also ask

What is a JTextField in Java?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt. TextField where it is reasonable to do so.


2 Answers

To get the border color:

 ((LineBorder)JTextField.getBorder()).getLineColor();

and this just a thought about how to get the border size, if you assume that the border size is the same as the component size you can cast JTextField to JComponent and get the size of JTextField:

 ((JComponent)JTextField).getSize();

but you should use it after putting the JTextField in its container, otherwise it will return (0,0).

like image 51
Billydan Avatar answered Oct 29 '22 01:10

Billydan


JTextField tf = new JTextField();
tf.setBorder(new LineBorder(Color.red, 2));
LineBorder border = (LineBorder) tf.getBorder();
System.out.println("Border color = "+  border.getLineColor() 
                          + "  size= " + border.getThickness());
like image 39
c0der Avatar answered Oct 28 '22 23:10

c0der