Results 1 to 3 of 3
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default BoxLayout Size Issue

    My text field in my box layout is way too tall. In other words, I want it to be only as high as one text line. Instead, its height is quite big.

    I've tried finding solutions online but it seems like one sizing issue leads to another.

    What is the best solution.

    Here is my compilable/runnable code.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GUI extends JFrame{
    
    
    	public JTextField textbox;
    	public JButton button;
    	public JTextArea textarea;
    
    
    	public GUI(){
    
    
    		JPanel panel = new JPanel();
    		textarea = new JTextArea("BLAH BLAH");
    
    		textbox = new JTextField();
    
    		button = new JButton("HELLO");
    		
    
    		
    
    
    
    		textarea.setEditable(false);
    
    		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    
    
    		
    		panel.add(textarea);
    		panel.add(textbox);
    		panel.add(button);
    		add(panel);
    
    
    
    
    		Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
    
    
    
    		setSize(scrnSize.width/2, scrnSize.height/3);
    
    		setLocationRelativeTo(null);
    
    		System.out.println(textbox.getHeight());
    
    
    		setVisible(true);
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    
    	public static void main(String[] args){
    
    		GUI g = new GUI();
    
    	}
    
    
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Set the preferred, max, and min sizes of the panel containing the JTextField, specifying the dimensions you wish. If you wish to display the JTextField with other components and don't want its sizing to directly affect those other components, wrap it in another JPanel and set the size of that panel, then add that panel to the parent panel.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    To prevent the text field from growing you can use:

    textField.setMaximumSize( textField.getPreferredSize() );

Similar Threads

  1. setSize on BoxLayout
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 12-16-2010, 12:18 AM
  2. BoxLayout Alignment problem
    By whiteMath in forum AWT / Swing
    Replies: 12
    Last Post: 07-19-2010, 11:04 PM
  3. Alignment using BoxLayout
    By tiptoe in forum AWT / Swing
    Replies: 4
    Last Post: 01-23-2010, 08:15 PM
  4. BoxLayout problems
    By paulb in forum New To Java
    Replies: 2
    Last Post: 11-04-2009, 11:04 PM
  5. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 02:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •