Link to home
Start Free TrialLog in
Avatar of David Barman
David BarmanFlag for United States of America

asked on

Java layout issues

New to Java.  Trying to re-create a layout see attached image.  I was trying to use the GridBagLayout.  My issue seems to be to place the buttons in the desired location.   With the JTextArea in place, I can't seem to determine how to place the desired buttons to the right of the JTextArea.

Here is some of my existing code:
        dropBtn = new JButton("Drop");
        eatBtn = new JButton("Eat");
        lookBtn = new JButton("Look");
        listBtn = new JButton("List");
        helpBtn = new JButton("Help");
        pickupBtn = new JButton("Pickup");
        westBtn = new JButton("West");
        eastBtn = new JButton("East");
        northBtn = new JButton("North");
        downBtn = new JButton("Down");
       
        setLayout (new GridBagLayout());
        GridBagConstraints position = new GridBagConstraints();
               
        // create results area to support twenty lines of text
        results = new JTextArea(20,40);
        JScrollPane scrollPane = new JScrollPane(results);
        results.setBorder(new EmptyBorder(20,20,20,20));
        // allow word wrap
        results.setLineWrap(true);
        results.setWrapStyleWord(true);

        // supports auto scrolling within the JTextArea
        DefaultCaret caret = (DefaultCaret) results.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
       
        position.gridx = 0;
        position.gridy = 0;
        add(results,position);        
       
        position.gridx = 1;
        position.gridy = 0;
        position.insets = new Insets(10, 10, 10, 10);
        add(eatBtn,position);
        eatBtn.addActionListener(this);
   
        position.gridy = 0;
        position.gridx = 2;
        add(lookBtn,position);
        lookBtn.addActionListener(this);

        //List button
        //JLabel listLbl = new JLabel("List");
        //listLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 0;
        add(listBtn,position);
        listBtn.addActionListener(this);

        //help button
        //JLabel sizeLbl = new JLabel("help");
        //sizeLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 1;
        add(helpBtn,position);
        helpBtn.addActionListener(this);

        //pick up button
        //JLabel pickupLbl = new JLabel("pick up");
        //sizeLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 2;
        add(pickupBtn,position);
        pickupBtn.addActionListener(this);
       
        position.gridy = 5;
        position.gridx = 1;
        add(westBtn,position);
        westBtn.addActionListener(this);

        position.gridy = 5;
        position.gridx = 3;
        add(eastBtn,position);
        eastBtn.addActionListener(this);
       
        position.gridy = 4;
        position.gridx = 2;
        add(northBtn,position);
        northBtn.addActionListener(this);
       
        position.gridy = 6;
        position.gridx = 2;
        add(downBtn,position);
        downBtn.addActionListener(this);
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Trying to re-create a layout see attached image.  
It's not there
Avatar of David Barman

ASKER

ok.  Let's try this again.  See attached.
It's basically a JTextArea in the upper left.  With a set of buttons directly to the right.  Along with another set of buttons below the JTextArea.
ScreenShot.png
The key to this sort of thing is to build in a modular way from nested layouts, not to try to do it in one layout.
Start by composing a stand-alone panel that contains the rightmost controls
What layout would be most appropriate?
You could actually use a GridBagLayout for that alone
So if I used GridBagLayout for the buttons. How do I incorporate the different layouts as one?
You can use a GBL to house each one
Ok.  I guess what I am missing is, in my JFrame how do I separate them from each other and then lay them out?
Well you've got 3 distinct areas, which imply a 2x2 grid.
Understood. But from what I understand of the layouts. When I create the GridBagLayout.  How do I create "other" layouts to keep them separated?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok.  I think I am getting close.  However, when I add the JTextArea, it pushes the other two panels.  The 0,1 panel is no longer at the top and the 1,0 panel is pushed from the left edge.  Not sure why or how to resolve.

Here is code so far.  I also attached some screen shots.

dropBtn = new JButton("Drop");
        eatBtn = new JButton("Eat");
        lookBtn = new JButton("Look");
        listBtn = new JButton("List");
        helpBtn = new JButton("Help");
        pickupBtn = new JButton("Pickup");
        westBtn = new JButton("West");
        eastBtn = new JButton("East");
        northBtn = new JButton("North");
        downBtn = new JButton("Down"); 
        
        JPanel contentPanel = new JPanel();
        JPanel textPanel = new JPanel();
        JPanel directionPanel = new JPanel();
        JPanel commandPanel = new JPanel();
        
        textPanel.setLayout (new GridLayout(0,1));
        GridBagConstraints position = new GridBagConstraints();
                
        // create results area to support twenty lines of text
        results = new JTextArea(20,40);
        JScrollPane scrollPane = new JScrollPane(results);
        results.setBorder(new EmptyBorder(20,20,20,20));
        // allow word wrap
        results.setLineWrap(true);
        results.setWrapStyleWord(true);

        // supports auto scrolling within the JTextArea
        DefaultCaret caret = (DefaultCaret) results.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        //add ("North", new Label ("Results", Label.CENTER));
        //add ("Center", results);
        //-position.gridx = 3;
        //-position.gridy = 2;
        //position.gridheight = 2; 
        //position.gridwidth = 7;
        //position.insets = new Insets(10, 10, 10, 10);
        textPanel.add(results,position);
               
        commandPanel.setLayout(new GridBagLayout());
        position = new GridBagConstraints();
        //Font myFont = new Font("serif", Font.BOLD, 20);
        //drop button
        //JLabel dropLbl = new JLabel("Drop");
        //dropLbl.setFont(myFont);
        //position.insets = new Insets(5, 5, 5, 5);
        //position.gridy = 3;
        //position.gridx = 1;        
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        //dropBtn.setBounds(50,50,50,50);
        position.gridx = 0;
        position.gridy = 0;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(dropBtn,position);
        dropBtn.addActionListener(this);
        //Eat button
        //JLabel eatLbl = new JLabel("Eat");
        //eatLbl.setFont(myFont);
        //position.gridy = 3;
        //position.gridx = 2;
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        position.insets = new Insets(0, 0, 0, 0);
        position.gridx = 1;
        position.gridy = 0;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(eatBtn,position);
        eatBtn.addActionListener(this);
        //look button
        //JLabel lookLbl = new JLabel("Look");
        //lookLbl.setFont(myFont);
        position.gridy = 0;
        position.gridx = 2;
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(lookBtn,position);
        lookBtn.addActionListener(this);
        //List button
        //JLabel listLbl = new JLabel("List");
        //listLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 0;
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(listBtn,position);
        listBtn.addActionListener(this);
        //help button
        //JLabel sizeLbl = new JLabel("help");
        //sizeLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 1;
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(helpBtn,position);
        helpBtn.addActionListener(this);
        //pick up button
        //JLabel pickupLbl = new JLabel("pick up");
        //sizeLbl.setFont(myFont);
        position.gridy = 1;
        position.gridx = 2;
        //position.gridheight = 1; 
        //position.gridwidth = 1;
        position.insets = new Insets(10, 10, 10, 10);
        commandPanel.add(pickupBtn,position);
        pickupBtn.addActionListener(this);
        
        directionPanel.setLayout(new GridBagLayout());
        position = new GridBagConstraints();
        
        position.gridy = 1;
        position.gridx = 3;
        position.insets = new Insets(10, 10, 10, 10);
        directionPanel.add(westBtn,position);
        westBtn.addActionListener(this);

        position.gridy = 1;
        position.gridx = 0;
        position.insets = new Insets(10, 10, 10, 10);
        directionPanel.add(eastBtn,position);
        eastBtn.addActionListener(this);
        
        position.gridy = 0;
        position.gridx = 2;
        position.insets = new Insets(10, 10, 10, 10);
        directionPanel.add(northBtn,position);
        northBtn.addActionListener(this);
        
        position.gridy = 2;
        position.gridx = 2;
        position.insets = new Insets(10, 10, 10, 10);
        directionPanel.add(downBtn,position);
        downBtn.addActionListener(this);
        
        contentPanel.setLayout(new GridBagLayout());
        this.setLayout(new GridBagLayout());
        position = new GridBagConstraints();
        position.gridy = 1;
        position.gridx = 0;
        position.insets = new Insets(10, 10, 10, 10);
        this.add(commandPanel,position);
        
        position.gridy = 0;
        position.gridx = 1;
        position.insets = new Insets(10, 10, 10, 10);
        this.add(directionPanel,position);
        
        position.gridy = 0;
        position.gridx = 0;
        position.insets = new Insets(10, 10, 10, 10);
        this.add(textPanel,position);

Open in new window

New-Created-Screen-Shot-1.PNG
New-Created-Screen-Shot-2.PNG
Shot-1 looks pretty good to me. What's the problem with it?
I suspect it has something to do with that all for sections of the grid are made of equal size.  Since the JTextArea is much larger than the other JPanels.  Should I be using a different layout for the JFrame???
There are two issues.  the direction buttons on the right, I wanted up at the top.  Which they are until the JTextArea is added.
The other issue I just realized is that as the JTextArea fills with content, it pushes the buttons below off the viewable area of the JFrame.  Do I need to make the JTextArea scroll to prevent that???
Do I need to make the JTextArea scroll to prevent that???

Yes. You should anyway

As for the other issue, your surmises are probably correct about equalizing dimensions. I'm by no means a GBL layout master but you should be able to achieve what you want. Possibly using 'anchor' techniques
I thought I did enable scrolling with:

DefaultCaret caret = (DefaultCaret) results.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

But when I get to the bottom of the JTextArea, the buttons below get pushed down.

Ideas?
I can take a look. It will help if you can post runnable code
I figured that portion out.  I was adding the JTextArea instead of adding the JScrollPane.  Now it scrolls and the buttons don't move when the JTextArea fills.
Thank you. Your guidance was helpful in getting me on the correct course.
:)