Results 1 to 7 of 7
  1. #1
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default How to set layout to have correct output

    I have to create a frame with tabs. So I have lots of labels and fields.
    I tried many layouts(like Grid, GridBag, Boarder, Flow) for setting them in different tabs. But I couldn't get satisfactory results.
    Below is the code for one of the tab :-
    Java Code:
    class SCGUI extends JPanel {
    
        public SCGUI() {
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
            JPanel leftPanel = new JPanel();
            JPanel rightPanel = new JPanel();
            JPanel shortRightPanel = new JPanel();
            JPanel cboxPanel = new JPanel();
    
            leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
            rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
            shortRightPanel.setLayout(new BoxLayout(shortRightPanel, BoxLayout.Y_AXIS));
            cboxPanel.setLayout(new BoxLayout(cboxPanel, BoxLayout.Y_AXIS));
    
            JLabel shortCodeLabel = new JLabel("Short Code : - ");
            JLabel shortCodeOwnerLabel = new JLabel("Short Code Owner : - ");
            JLabel connectionTypeLabel = new JLabel("Connection Type : - ");
            JTextField shortCodeTextField = new JTextField(10);
            shortCodeTextField.setSize(100, 30);
            JComboBox comboboxConnectionType = new JComboBox();
            JTextField shortCodeOwnerTextField = new JTextField(50);
            shortCodeOwnerTextField.setSize(200, 30);
    
            leftPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
            leftPanel.add(Box.createRigidArea(new Dimension(5, 35)));
            leftPanel.add(shortCodeLabel);
            leftPanel.add(Box.createRigidArea(new Dimension(5, 25)));
            leftPanel.add(shortCodeOwnerLabel);
            leftPanel.add(Box.createRigidArea(new Dimension(5, 25)));
            leftPanel.add(connectionTypeLabel);
            
            shortRightPanel.setMinimumSize(new Dimension(100,20));
            shortRightPanel.setMaximumSize(new Dimension(100,20));
            shortRightPanel.setPreferredSize(new Dimension(100,20));
            cboxPanel.setMinimumSize(new Dimension(100,30));
            cboxPanel.setMaximumSize(new Dimension(100,30));
            cboxPanel.setPreferredSize(new Dimension(100,30));
            
            shortCodeTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
            shortRightPanel.add(shortCodeTextField);
            
            cboxPanel.add(comboboxConnectionType);
    
            rightPanel.add(Box.createRigidArea(new Dimension(5, 40)));
            rightPanel.add(shortRightPanel);
            rightPanel.add(Box.createRigidArea(new Dimension(5,20)));
            rightPanel.add(shortCodeOwnerTextField);
            rightPanel.add(Box.createRigidArea(new Dimension(5,10)));
            rightPanel.add(cboxPanel);
            mainPanel.add(leftPanel);
            mainPanel.add(rightPanel);
    
            add(mainPanel);
        }
    }
    and Output looks like : -
    How can I make it look correct.
    Attached Thumbnails Attached Thumbnails How to set layout to have correct output-1.jpg  
    Last edited by Prajin; 07-06-2010 at 01:29 PM.

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

    Default

    Does your current image represent what you want it to look like or what it looks like now? Can you show us the other?

  3. #3
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Current image represent what it looks now. That's not what I wanted. Textfield, Combox aren't align properly.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Using SpringLayout or GridBagLayout would make this easier.

    db

  5. #5
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    I tried GridBagLayout but it didn't gave good results. Probably I wasn't using it properly and SpringLayout concept I didn't understood, which I read from sun tutorial.
    In gridbagLayout, all TextField width became same. Whereas I was trying to set it to different width. I created panels for each textFields and set there(TextField & panel) sizes different also.
    While adding all panels to main panel I used GridLayout. Could it be that Gridlayout did all the wrong?

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    I tried GridBagLayout but it didn't gave good results. Probably I wasn't using it properly and SpringLayout concept I didn't understood, which I read from sun tutorial.
    In gridbagLayout, all TextField width became same. Whereas I was trying to set it to different width. I created panels for each textFields and set there(TextField & panel) sizes different also.
    While adding all panels to main panel I used GridLayout. Could it be that Gridlayout did all the wrong?

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

    Default

    GridLayout is great for creating grids where each cell is the exact same size, such as a chessboard, but while this design will work for your GUI, it won't look pleasing as you're finding out.

    I suggest that you post an image of how you want this to look and also let's see your GridBagLayout attempt. These two bits of information will likely help us figure out better what's wrong. Also you'll want to review the GridBagLayout tutorials and may wish to search this forum on keywords GridBagLayout and GridBagConstraints.

    Much luck!

Similar Threads

  1. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  2. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:41 PM
  3. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM
  4. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11: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
  •