Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control the width of JTextFields in Java Swing?

Tags:

I am trying to have several JTextFields on a single row, but I don't want them to have the same width. How can I control the width and make some of them wider than others? I want that they together take up 100% of the total width, so it would be good if I could use some kind of weigthing.

I have tried with .setColumns() but it doesn't make sense.

Here is an example, where I am using three rows with three strings that should appear as in columns:

import java.awt.GridLayout; import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField;  public class RowTest extends JPanel {      class Row extends JComponent {         public Row(String str1, String str2, String str3) {             this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));              JTextField fld1 = new JTextField(str1);             JTextField fld2 = new JTextField(str2);             JTextField fld3 = new JTextField(str3);              fld1.setColumns(5); // makes no sense              this.add(fld1);             this.add(fld2);             this.add(fld3);         }     }      public RowTest() {     this.setLayout(new GridLayout(5,0));      this.add(new Row("Short", "A long text that takes up more space",         "Short again"));     this.add(new Row("Longer but short", "Another long string", "Short"));     this.add(new Row("Hello", "The long field again",         "Some info"));     }      public static void main(String[] args) {         new JFrame() {{ this.getContentPane().add(new RowTest());                             this.pack(); this.setVisible(true); }};     }  } 
like image 343
Jonas Avatar asked May 24 '10 13:05

Jonas


People also ask

How do you change the size of the text field in Java?

textField. setColumns(...); to let the text field determine the preferred width. Or if you want the width to be the entire width of the parent panel then you need to use an appropriate layout.

How do I make JTextField smaller?

put your jtextfield within a JPanel, now that JPanel is the one which will have large size, but that won't be visible and then you can control the layout of JPanel and hence you can control the size of the jtextfield.

How to align textField in java?

To create right justified JTextField, set the alignment to be RIGHT. Here, we will be using the setHorizontalAlignment() method as well and within that the alignment would be set.

What does JTextField do in java?

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.


1 Answers

yourTextField = new JTextField("", 20); 

if you start it like this it sets the textfield to be empty and to have 20 collumns

like image 85
Michael Pankhurst Avatar answered Oct 01 '22 09:10

Michael Pankhurst