Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add padding to a jtextfield

How can I add some padding to a jtextfield? I've tried tf.setMargin(new Insets(5,5,5,5)); which doesn't have any effect.

like image 867
fenerlitk Avatar asked Jan 09 '12 17:01

fenerlitk


People also ask

How do you modify whether or not a JTextField can be edited?

In order to create a non editable JTextField , all you have to do is: Create a class that extends JFrame . Create a new JTextField . Use setEditable(false) that sets the specified boolean to indicate whether or not this textfield should be editable.

What is the difference between a JTextField and a JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.

Can you enter more than one line in a JTextField?

Only one line of user response will be accepted. If multiple lines are desired, JTextArea will be needed. As with all action events, when an event listener registers an event, the event is processed and the data in the text field can be utilized in the program.

How do I center text in JTextField?

Just use the setBounds attribute. Jtextfield. setBounds(x,x,x,x);


1 Answers

The problem you are having is that the UI is setting its own border on the text field, overriding the margin you set. You can see a warning to this effect in the javadoc of setMargin().

The solution is to let the UI set a border, then squeeze in another border of your own:

field.setBorder(BorderFactory.createCompoundBorder(         field.getBorder(),          BorderFactory.createEmptyBorder(5, 5, 5, 5))); 
like image 130
Russell Zahniser Avatar answered Oct 08 '22 02:10

Russell Zahniser