Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problem with BoxLayout and JTextField

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default problem with BoxLayout and JTextField

    I have a weird problem with laying out some JTextFields.

    This code works right:
                    JPanel setbuttonpanel = new JPanel();
    		setbuttonpanel.setLayout( new BoxLayout(setbuttonpanel, BoxLayout.LINE_AXIS) );		
     		inputfield = new JTextField(20);
     		setbuttonpanel.add(inputfield);
     		setbuttonpanel.add( Box.createRigidArea( new Dimension(offset, 0) ) );
    		...
    		setbutton = new JButton(new SetAction("Set"));
    		setbuttonpanel.add(setbutton);
    		panel.add(setbuttonpanel);

    This code does not: (it blows up the textfield's height)
                    JPanel inputpanel = new JPanel();
    		inputpanel.setLayout( new BoxLayout(inputpanel,BoxLayout.LINE_AXIS)) ;
    		seedinput = new JTextField(5);
    		amountinput = new JTextField(5);				
    		inputpanel.add(seedinput);
    		inputpanel.add(amountinput);
    		panel.add(inputpanel);

    How can I fix this so both the textfields and the panel they are in have the size they should have?

    This is some compilable code that shows the same problem:
    package Test;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class TestWindow
    {
    	private JFrame frame;
    	private JButton randombutton;
    	private JButton savebutton;
    	private JTextField seedinput;
    	private JTextField amountinput;
     
    	public TestWindow()
    	{
    		frame = new JFrame("Search Simulation");		
    		frame.setPreferredSize(new Dimension(800,600));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		int offset = 25;		
    		JPanel loadbuttonpanel = new JPanel();
    		loadbuttonpanel.setBackground(Color.BLUE);
    		loadbuttonpanel.setLayout( new BoxLayout(loadbuttonpanel, BoxLayout.PAGE_AXIS) );
    		randombutton = new JButton("test");
    		loadbuttonpanel.add(randombutton);
    		JPanel inputpanel = new JPanel();		
    		inputpanel.setBackground(Color.GREEN);
    		inputpanel.setLayout( new BoxLayout(inputpanel,BoxLayout.LINE_AXIS)); // if you comment out this line they get the right size, but the panel still takes up all the space for some reason
    		seedinput = new JTextField(5);		
    		amountinput = new JTextField(5);				
    		inputpanel.add(seedinput);
    		inputpanel.add(amountinput);
    		loadbuttonpanel.add(inputpanel);
    		frame.add(loadbuttonpanel);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args)
    	{
    		SwingUtilities.invokeLater( new Runnable()
    		{
    			public void run()
    			{
    				TestWindow test = new TestWindow();		
    			}
    		});
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with BoxLayout and JTextField

    Use the setpreferredSize method for the textfields and set the Dimensions the way you want them to be. You can even make a Dimension object and use that for the setpreferredSize for all your textfields since you want them the same size.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: problem with BoxLayout and JTextField

    setPreferredSize doesn't do anything here, setMaximumSize does work. I'm still confused why the first example does work though, since I can't see any real difference between the two examples.

Similar Threads

  1. JTextField
    By DR_W in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 25th, 2013, 10:47 AM
  2. JTextField
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 7
    Last Post: June 20th, 2012, 10:54 AM
  3. Problem: Grabbing Ints from JTextField
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 25th, 2012, 06:55 PM
  4. BoxLayout: Sharing ISN'T Caring... :(
    By snowguy13 in forum AWT / Java Swing
    Replies: 5
    Last Post: December 24th, 2011, 10:21 AM
  5. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM