JTextField - Reset Border to System Default

jtextfield border color
jtextfield remove border
java gui source code examples
java gui components
java gui application example
java border
what are the steps for properly creating a gui in java

I am trying to color the Border of a JTextField red and then change it back to "normal" later on. When I am using Linux (furthermore Ubuntu), the initial Border differs from the Border that you get by using UIManager.getBorder("TextField.border"); one of them is a SynthBorder and one is a FieldBorder. The "correct" one would be the SynthBorder.


import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main
  private static boolean switched;

  public static void main( final String[] args )
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

    JFrame frame = new JFrame( "Test border change" );
    frame.getContentPane().setLayout( new BoxLayout( frame.getContentPane(), BoxLayout.LINE_AXIS ) );
    JTextField tf = new JTextField();
    JButton button = new JButton( "Switch" );
    button.addActionListener( action ->
      if ( switched )
        tf.setBorder( UIManager.getBorder( "TextField.border" ) );
        switched = !switched;
        tf.setBorder( BorderFactory.createLineBorder( Color.RED ) );
        switched = !switched;
    } );
    frame.getContentPane().add( tf );
    frame.getContentPane().add( button );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setVisible( true );

I have already tried:

  • using JComponent.updateUI() (no effect)
  • nulling the border (ruins the layout)
  • preserving it (not a proper way)

Does anyone have a better idea?

When you replace the Border try using:

Border uiBorder = BorderUIResource( BorderFactory.createLineBorder( Color.RED ) );
tf.setBorder( uiBorder );

When you use any wrapper class with "UIResource" this tell the LAF the component is part of the LAF and not a custom implementation

Then to restore the Border:

SwingUtilities.updateComponentTreeUI( tf );

Hopefully this will fake the UI into resetting the LAF properties, specifically the Border.

Read the section from the Swing tutorial on How to Set the LAF for more information.

Of course this is not as efficient as simply saving the Border and resetting it as all properties of the text field will be updated bye the updatComponentTreeUI(...) (if this works).

Still don't see why you can't save the Border. You could use the putClientProperty(...) method of the JComponent class to save the Border and then restore it using the getClientProperty(...) method.

You could even automate this by using adding a PropertyChangeListener to listen for a change in the border. When an event is generated if the getClientProperty(...) returns null, then you save the old value from the PropertyChangeEvent.

java - JTextField - Reset Border to System Default, I am trying to color the Border of a JTextField red and then change it back to "​normal" later on. When I am using Linux (furthermore Ubuntu), the  JTextField Set JTextField Border Example. July 14, 2019. Set JTextField Border Example . This java example shows how to create a border for JTextField using .

You can get the default border in the UIManager with this code:


jTextField default border color (Swing / AWT / SWT forum at , using java6 and all defaults, i've textfields and a jTextArea; as the default textarea has no border, i added one; but i System.out.println(border. 2. How to reset the UNDO of a JTextField forums.netbeans.org. Enrico Scantamburlo Posted via mailing list. Posted: Wed Dec 15, 2010 12:48 am Post subject: How to reset the UNDO of a JTextField Hello I am developing a RCP Application based on NetBeans 6.5 I have a problem: I have several JTextField with the MIME-TYPE set.

yourJTextField.setBorder(new JTextField().getBorder());

How to Change JTextField Border Color in Java Swing, Read more : http://intactabode.com/ How to Change JTextField Border Color in Java Swing Duration: 1:23 Posted: Apr 18, 2018 Set the focus on a particular JTextField : JTextField « Swing « Java Tutorial. Java Tutorial; Swing; JTextField;

You can get the border right after creating the component to save it, and set it again later.

Border defaultBorder = tf.getBorder();

GUI Programming Part 2 - Java Programming Tutorial, All JComponents (such as JPanel , JLabel , JTextField and JButton ) support these Swing supports these border types (in package javax.swing.border ), which the menu bar adds this menu menuItem = new JMenuItem("Reset", KeyEvent. You can choose to use the default Java look and feel, the native system's look  The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored). This causes a redraw of the component.

Java Code Examples javax.swing.JTextField.setText, Your votes will be used in our system to get more good examples. NONE; // reset to default c.weightx = 0.0; // reset to default c.weighty = 1.0; new Border[​10]; for(int i=0;i<10;i++){ sigmaHSRefField[i] = new JTextField(); sigmaHSRefField[i]. Read More : http://intactabode.com/ How to Remove JTextField Border in Java Swing, in this video i show how to remove or disable border for jtextfield in jav

Solved: Need Help With This Java Program Using GUI. Starte , void setText(String t) Sets the text of the JTextArea to the passed String, removing the current contents. Reset the user inputs back to defaults. removeButton Using java6 and all defaults, i've textfields and a jTextArea; as the default textarea has no border, i added one; but i cant find out what color textfield's borders use, so that i could use the same color in my textarea border.

The Guidebook, In this example, we have converted the JTextField example from Absolute The BorderLayout uses a system whereby you can position things on the four sides of a //This sets the Border Layout to have a horizontal gap of 10 //and a vertical gap of The default is 0 pixels gap, which is good or bad depending on what you  Because the JTextField class inherits from the JTextComponent class, text fields are very flexible and can be customized almost any way you like. For example, you can add a document listener or a document filter to be notified when the text changes, and in the filter case you can modify the text field accordingly.

  • Possible duplicate of Reset/remove a border in Swing
  • I have tried that already, works fine in the example, but throws exceptions in the productive usecase, because of the eventing. I might look furhter into that though.
  • One comment on an answer in the above link suggests to call setBorder(null) before calling updateUI() .
  • I'll be preserving using the put/getClientProperty methods. Also, uodateComponentTreeUI works well, but preserving sadly fits my case better ;D
  • This is a fundamental solution, also for setting borders on controls that lack them. I've not been able to find this anywhere else - excellent, thank you!
  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
  • Preserving is not an option ;D
  • @ScriptKiddy, Preserving is not an option - why not?