JTextField tf = new JTextField();
tf.setBorder(new LineBorder(Color.red, 2));
Border border = tf.getBorder();
How can I get border color and size?
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.
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).
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With