Setting width of JTextArea and JTextField

Guys,

I have read instruction on how to set the width of JTextArea’s and JTextFields.

I have tried to set the width of a text field like so:


  
  JTextField test = new JTextField("", 15); 
  
  

and, this for the JTextArea


  
  // obviously row and cols vars come from somewhere
  
  JTextArea test = new JTextArea("",rows,cols);
  
  

So, why is it that the text field and text areas just take up the available size of the window. If I make the window bigger then the fields just grow with it. Is it something to do with the kind of layout I am using. The layout is a GridLayout().

Thanks

I typically never set the size on my components mark. That is the job of the layout manager. I would recommend using the GridBayLayout instead of just the GridLayout.

Regards,

Nate

Ok Nate, I will look into that then. You got any examples of how you do it?

If you post an example of what you’re trying to do. I’m not the best of coming up with my own examples.

:slight_smile:

Nate

Basically what I want is a form like.

– First Name - JTextField
– Second Name - JTextField
– Telephone Number - JTextField
– Priority - JTextField
– call description - JTextArea (200,200) ??

Sorry I dont actually have my code on me at the minute! :frowning:


package example;

import java.awt.*;

import javax.swing.*;

public class SampleGUI extends JFrame
{
	public SampleGUI()
	{
		init();
	}
	
	private void init()
	{
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Grid Bag Layout Example");
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		JPanel detailPanel = new JPanel();
		detailPanel.setLayout(new GridBagLayout());
		
		JLabel label = new JLabel("First Name:");
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		detailPanel.add(label, gbc);
		
		JTextField firstNameTextField = new JTextField();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		detailPanel.add(firstNameTextField, gbc);
		
		label = new JLabel("Last Name:");
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 0.0;
		detailPanel.add(label, gbc);
		
		JTextField lastNameTextField = new JTextField();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		detailPanel.add(lastNameTextField, gbc);
		
		label = new JLabel("Telephone:");
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 0.0;
		detailPanel.add(label, gbc);
		
		JTextField telephoneTextField = new JTextField();
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		detailPanel.add(telephoneTextField, gbc);
		
		label = new JLabel("Priority Name:");
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 0.0;
		detailPanel.add(label, gbc);
		
		String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
		JComboBox priorityComboBox = new JComboBox(data);
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		detailPanel.add(priorityComboBox, gbc);
		
		label = new JLabel("Call Description:");
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 0.0;
		detailPanel.add(label, gbc);
		
		JTextArea descriptionTextArea = new JTextArea();
		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.gridwidth = 2;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		detailPanel.add(descriptionTextArea, gbc);
		
		
		getContentPane().add(detailPanel);
	}
	
	
	public static void main(String[] args)
	{
		SampleGUI gui = new SampleGUI();
		
		gui.setSize(250, 400);
		gui.setVisible(true);
	}
}

Thanks. I will check that out! :slight_smile: