Results 1 to 4 of 4
  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default add a JTextField

    Hello again,

    I started GUI and Layouts trying to complete my first real project, Calculator. But I have a problem with the JTextField.

    At lines 22 and 23, I created a JTextField and added it to panelNorth which is the panel that goes at North of the JFrame BorderLayout. My problem is that I want it to be 11 spaces long but is takes up all North space of the JFrame.

    Is this happening only with the JTextField or is there a rule to keep the original dimensions?

    I looked it up at docs.oracle api but couldn't find it.

    Java Code:
    package Calculator;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class calculatorGUI extends JFrame{
    	
    	private static final long serialVersionUID = 1L;
    
    	public calculatorGUI() {
    		
    		super("Calculator");
    		setPreferredSize(new Dimension(600,400));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocation(820, 320);
    		setLayout(new BorderLayout());
    		
    		JPanel panelNorth = new JPanel();
    		panelNorth.setLayout(new GridLayout());
    		add(panelNorth, BorderLayout.NORTH);
    		
    		JTextField numbersText = new JTextField("0", 11);
    		panelNorth.add(numbersText, BorderLayout.SOUTH);
    		
    		
    		pack();
    		setVisible(true);
    	}
    
    }
    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: add a JTextField

    You're adding the numbersText JTextField to the BorderLayout.SOUTH position of the panelNorth JPanel, but this JPanel doesn't use BorderLayout, and so the add method second parameter doesn't make sense. panelNorth uses GridLayout, but without any row or column values given.

    I think that first you will want to decide just what layouts you really want to use, since it's not clear by your posted code, and then get back to us. Note that both BorderLayout and GridLayout may ignore your JTextfield's column size since both will try to have the component being added fill up the available space. You may wish to embed your JTextField in its own FlowLayout using JPanel, and then add that JPanel to the northPanel.

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: add a JTextField

    Yes Fuba, I understood completely what you said. Indeed I had made a mistake there.

    So I followed your suggestion. I used BorderLayout on my JFrame and added at North a BorderLayout JPanel.

    At JPanel South, I added a FlowLayout and put it the JTextField and worked like a charm.

    Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. JTextField
    By tnrh1 in forum AWT / Swing
    Replies: 2
    Last Post: 01-11-2012, 09:54 PM
  2. using only a-z in JTextField
    By JultomteN in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2012, 08:26 PM
  3. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  4. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 PM

Posting Permissions

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