Java gets border color and size

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

How can I get the color and size of the border?

+7
source share
4 answers

To get the border color:

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

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

  ((JComponent)JTextField).getSize(); 

but you have to use it after putting the JTextField in your container, otherwise it will return (0,0).

+4
source
 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()); 
+1
source
 JTextField.setPreferredSize(new Dimension(350, 20)); 
-one
source
 border.getBorderInsets(JTextField).bottom border.getBorderInsets(JTextField).left border.getBorderInsets(JTextField).right border.getBorderInsets(JTextField).top border.getLineColor() 
-one
source

All Articles