Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't set the size of the JTextfield. Need help

Status
Not open for further replies.

Bloosy

Technical User
Jun 23, 2003
15
I want to make a panel which shows four quarter final football matches, so that user can input result for each match. As I haven't got time for fancy design, I've decided to lay out text fields vertically, using Box layout. So, for one match I need two fields with 40 columns for team name (which is country name), two small fields for scores and a button for exporting data to further processing.
But I found out that program doesn't care for the size of fields I set. It uses all available size, and all the fields are of the maximum width. I spent hours trying to find the way without success. All tutorials claim that box layout respects the preffered size of the components. Here is a piece of code I've been experimenting with:

public class Pan extends JFrame {

private JPanel quarterPanel;
private JTextField match11, match12, match21, match22, match31, match32, match41, match42;
private JTextField score11, score12, score21, score22, score31, score32, score41, score42;
private JButton btnMatch1, btnMatch2, btnMatch3, btnMatch4;

public Pan() {

quarterPanel = new JPanel();
quarterPanel.setLayout(new BoxLayout(quarterPanel, BoxLayout.Y_AXIS));
match11 = new JTextField(40);



quarterPanel.add(match11);
score11 = new JTextField(6);
quarterPanel.add(score11);

match12 = new JTextField(10);
quarterPanel.add(match12);
score12 = new JTextField(6);
quarterPanel.add(score12);
btnMatch1 = new JButton("Enter Result for Match 1");


add(quarterPanel);


}

So this is for the first match, and I have to add three more, but I must somehow get those fields for scores to be small.
I'd really appreciate if somebody could help
 
You can specify enforce the prefered Size by extending JTextField.
The Layoutmanagers use those get...Size () - methods, to decide, how to resize a component.
Code:
 private JTextField match11, match12, match21, match22, match31, match32, match41, match42;
	private JTextField score11, score12, score21, score22, score31, score32, score41, score42;
	private JButton btnMatch1, btnMatch2, btnMatch3, btnMatch4;
		
	class SizedTextField extends JTextField 
	{
		Dimension d;
		
		public SizedTextField (int cWidth)
		{
			d = new Dimension (cWidth * 16, 21);
		}
		
		public Dimension getPreferedSize () { return d; }
		public Dimension getMaximumSize () { return d; }
		public Dimension getMinimumSize () { return d; }
	}
	
	public Pan () 
	{
		quarterPanel = new JPanel ();
		quarterPanel.setLayout (new BoxLayout (quarterPanel, BoxLayout.Y_AXIS));
		match11 = new SizedTextField (40);
		// ...

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top