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 5 of 5

Thread: How to align the items in a form (grid layout)?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to align the items in a form (grid layout)?

    I need to place JLabels and JTextFields in a panel and align them as a form:

    Name: <text field>
    Surname: <text field>
    Address: <textfield>

    etc.

    I'm trying to use GridLayout.

    panel1.setLayout(new GridLayout(1,2));
    It works, but the spaces between labels and text fields are very big. How to make them smaller?
    Or maybe I should us a different layout for that?

    Please help


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to align the items in a form (grid layout)?

    If worse comes to worse, you could always use method setLocation(int width, int height);

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to align the items in a form (grid layout)?

    Thank you for the reply.

    What is the usual way to make a simple form like that:

    Name: <text field>

    Initial: <text field>

    Surname: <text field>

    ???

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to align the items in a form (grid layout)?

    You could try using nested JPanels,

    private void controlHelper(){ //call this in constructor or w/e
     
        JPanel controlPanel = new JPanel(); //control uses default layout manager   
        controlPanel.add(createPanelLabels());
        controlPanel.add(PanelFields());
        add(controlPanel, BorderLayout.SOUTH);
     
     }
     
        public JPanel createPanelLabels() {
            JPanel p = new JPanel();
            p.setLayout(new GridLayout(3, 1));
            name = new JLabel("Name:");
     
            initial = new JLabel("Initial:");
            surname = new JLabel("Surname:");
     
            p.add(name);
            p.add(initial);
            p.add(surname);
     
            return p;
        }
     
           public JPanel panelFields() {
           JPanel p = new JPanel();
            p.setLayout(new GridLayout(3, 1));
     
            final int FIELD_WIDTH = 10;
            nameField = new JTextField("", FIELD_WIDTH);
     
         //etc
    }

    This means that they will be side by side label - field, and squashed in, then you can define where you want it with something like BorderLayout.WEST and so forth.

    That quick code needs to be worked on, as in, making an instance variable of nameField etc.

    I hope thats what you're after, I'm a learner myself, just trying to contribute .

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to align the items in a form (grid layout)?

    I recommend nesting panels in panels, like newbie suggested. The trick is to figure out what layout managers to put in each.

    Check out this: A Visual Guide to Layout Managers

    If you are having trouble doing this in code, you might want to try a GUI based builder. NetBeans is a free IDE written by Sun for writing java and it has a GUI tool to help you layout components, like Visual Basic sort of. Eclipse may have something similar now - I am not sure.

    Sometimes I cheat and use NetBeans, and look at the resulting code to see how they did it, and then write my own using that info.

Similar Threads

  1. System.out.print -align
    By juwan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 17th, 2010, 04:36 PM
  2. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM
  3. My Custom Layout (VERY EASY TO USE)
    By aussiemcgr in forum AWT / Java Swing
    Replies: 10
    Last Post: August 5th, 2010, 01:37 PM
  4. How do you layout these components centered
    By robertbob in forum AWT / Java Swing
    Replies: 2
    Last Post: May 24th, 2010, 11:52 PM
  5. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM