samgi
samgi

Reputation: 225

Customize JTextField in Java Swings - add simple/compound/custom borders to JTextField

I have a Netbeans java project with a lot of forms. There are many JTextFields in those forms. I would like to customize those text fields with a custom border.

 private void tfUserNameFocusGained(java.awt.event.FocusEvent evt) {                                       
        tfUserName.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 255)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 255))), javax.swing.BorderFactory.createMatteBorder(0, 4, 2, 0, new java.awt.Color(255, 255, 255))));
 }

private void tfUserNameFocusLost(java.awt.event.FocusEvent evt) {                                     
        tfUserName.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), javax.swing.BorderFactory.createMatteBorder(0, 4, 2, 0, new java.awt.Color(255, 255, 255))));
}

I can add these code lines to each JTextField, but I'm looking for an easier way to do this.

Upvotes: 1

Views: 601

Answers (2)

Abra
Abra

Reputation: 20924

To set the same border for all JTextFields, use the UIManager class.

UIManager.getDefaults().put("TextField.border", BorderFactory.createTitledBorder("George"));

The above code will set the default border for every JTextField to a titled border where the title is George.

UIManager class manages what is known as look-and-feel. I suggest reading the javadoc for class UIManager.

EDIT

For changing the border when a JTextField gains or loses focus, use the FocusEvent parameter of the method focusGained() and method focusLost(). The parameter contains the "source" of the event.

evt.getSource()

You know that the source is a JTextField so just cast it and set the border.

JTextField textField = (JTextField) evt.getSource();
textField.setBorder( /* whatever border you need */ );

Upvotes: 2

Aceccop
Aceccop

Reputation: 124

You can use your own extension of JTextField like this:

public class OwnJTextField extends JTextField {
private void tfUserNameFocusGained(java.awt.event.FocusEvent evt) {
    tfUserName.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 255)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 255))), javax.swing.BorderFactory.createMatteBorder(0, 4, 2, 0, new java.awt.Color(255, 255, 255))));
}
private void tfUserNameFocusLost(java.awt.event.FocusEvent evt) {
    tfUserName.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), javax.swing.BorderFactory.createMatteBorder(0, 4, 2, 0, new java.awt.Color(255, 255, 255))));
}

And everywhere where you need that use OwnJTextField instead JTextField.

Upvotes: 2

Related Questions