I can't set the size of my JTextField

From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.

Announcements Posted on
Please change your TSR password 23-05-2013
Sign in to Reply
  1. Dr Ben's Avatar
    • Respected Member
    • Posts: 225
    I can't set the size of my JTextField
    Hi, here's the code (I think it's only necessary to see this one panel where the problem is happening, but if you think I ought to post the whole 96 lines of code, please say):

    Code:
            
    
     JTextField grossIncTxt = new JTextField(5);
    ...
    
            JPanel one = new JPanel();
            one.setLayout(new GridLayout(1,2));
            one.add(new JLabel("Gross Income"));
            grossIncTxt.setMinimumSize(new Dimension(150,20));
            grossIncTxt.setMaximumSize(new Dimension(150,20));
            one.add(grossIncTxt);
            add(one);
    The problem is that when the program is run, the JTextField object takes shape of the entire box on the right of the 2x1 grid of the panel object, rather than following my rules of what the size should be

    halp plox!
  2. RipperRoo's Avatar
    • Benevolent Member
    • Location: Newcastle
    • Posts: 657
    Re: I can't set the size of my JTextField
    Look into the different layouts you can use.

    I think if you use FlowLayout that would solve it, but it can be a little awkward sometimes.

    http://download.oracle.com/javase/tu...yout/flow.html
  3. Dr Ben's Avatar
    • Respected Member
    • Posts: 225
    Re: I can't set the size of my JTextField
    nah i need to use gridlayout for this, there must be some way to do this in gridlayout?
  4. RipperRoo's Avatar
    • Benevolent Member
    • Location: Newcastle
    • Posts: 657
    Re: I can't set the size of my JTextField
    (Original post by Dr Ben)
    nah i need to use gridlayout for this, there must be some way to do this in gridlayout?
    ah right, I'm not too good at positioning and stuff in Java (had a lot of problems with scrollboxes).

    Have you tried using BoxLayout or a layout within a layout? I just play around with it for a while and it usually works. xD
  5. xXedixXx's Avatar
    • Vengeful, Imperial Overlord of The Student Room
    • Location: Hertfordshire
    • Posts: 4,130
    Re: I can't set the size of my JTextField
    What IDE are you using? Because if you use NetBean's swing editor it's really easy to set the size of text fields, buttons etc.. both graphically and via a properties dialog.
  6. Dr Ben's Avatar
    • Respected Member
    • Posts: 225
    Re: I can't set the size of my JTextField
    (Original post by xXedixXx)
    What IDE are you using? Because if you use NetBean's swing editor it's really easy to set the size of text fields, buttons etc.. both graphically and via a properties dialog.
    Yeah I use netbeans, I love netbeans, but doing that will just hide the gap in my java knowledge, I'd like to learn where I'm going wrong
  7. xXedixXx's Avatar
    • Vengeful, Imperial Overlord of The Student Room
    • Location: Hertfordshire
    • Posts: 4,130
    Re: I can't set the size of my JTextField
    (Original post by Dr Ben)
    Yeah I use netbeans, I love netbeans, but doing that will just hide the gap in my java knowledge, I'd like to learn where I'm going wrong
    Fair enough; my advice would be to google some tutorials.
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.