Java Layout Managers

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Right, have just been reading about layout managers and all seems to make sense and follow on. However, there's a "challeneg" question after the chapter saying make a simple layout of buttons in a compass style layout (North, South, East, West and Center) without using multiple JPanels (says I don't even need one panel).

My initial thought was to use x5 JPanels, but as they say don't do that I must be mssing something. This isn't really involved stuff either, as I'm just reading an introduction chapter on layout managers - border, flow and grid layouts.

Any ideas to help a java nub?
 
Not got my Java hat on this month, but can't you do something like
JFrame myframe = new JFrame();
myframe.layout(new compasslayout()); // Not sure of the syntax here but should be simple to see in an IDE like Eclipse
JButton button1 = new JButton;
myframe.add(button1);
myframe.pack();
 
The bit I don't understand is how I'm supposed to add x5 JButtons to the layout without using panels - further on down it says I only need a single panel and should think how the default layout works). These are buttons away from the border E.g (but north and south would be centered)

PHP:
          btnNorth

btnWest        btnCenter                      btnEast

           btnSouth
 
Last edited:
Something like this?

Code:
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
jp.add(new Button("North"), BorderLayout.NORTH);
jp.add(new Button("South"), BorderLayout.SOUTH);
jp.add(new Button("East"), BorderLayout.EAST);
jp.add(new Button("West"), BorderLayout.WEST);
jp.add(new Button("Center"), BorderLayout.CENTER);
 
Something like this?

Code:
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
jp.add(new Button("North"), BorderLayout.NORTH);
jp.add(new Button("South"), BorderLayout.SOUTH);
jp.add(new Button("East"), BorderLayout.EAST);
jp.add(new Button("West"), BorderLayout.WEST);
jp.add(new Button("Center"), BorderLayout.CENTER);

Yup exactly what I was going to say, hardly "challenging" ;)
 
Something like this?

Code:
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
jp.add(new Button("North"), BorderLayout.NORTH);
jp.add(new Button("South"), BorderLayout.SOUTH);
jp.add(new Button("East"), BorderLayout.EAST);
jp.add(new Button("West"), BorderLayout.WEST);
jp.add(new Button("Center"), BorderLayout.CENTER);

That's just the bog standard borderlayout though. What they want is a JButton in the middle of each 'border' - that's why my first thought was to use x5 panels - but its says I don't need them.

Here's a pro ms paint example :)

java.jpg
 
Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Buttons extends JFrame{
    
  public Buttons(){
    super("Buttons");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridBagLayout());
        
    GridBagConstraints GBConst = new GridBagConstraints();
    GBConst.gridx = 1;
    GBConst.gridy = 0;
    GBConst.gridwidth = 1;
    GBConst.gridheight = 1;
    GBConst.anchor = GridBagConstraints.CENTER;
    GBConst.fill = GridBagConstraints.NONE;
    GBConst.insets = new Insets(0,0,0,0);
    GBConst.weightx = 1;
    GBConst.weighty = 1;
    
    getContentPane().add(new JButton("North"), GBConst);
    
    GBConst.gridx = 1;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("Centre"), GBConst);
    
    GBConst.gridx = 1;
    GBConst.gridy = 2;
    getContentPane().add(new JButton("South"), GBConst);
    
    GBConst.gridx = 0;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("West"), GBConst);
    
    GBConst.gridx = 2;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("East"), GBConst);   
    
    pack();
    setVisible(true);    
  }
  
  public static void main(String[] args){
    new Buttons();
  }
}

buttons.png


Learn GridBag, it is your friend.
 
Last edited:
Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
 
public class Buttons extends JFrame{
 
  public Buttons(){
    super("Buttons");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridBagLayout());
 
    GridBagConstraints GBConst = new GridBagConstraints();
    GBConst.gridx = 1;
    GBConst.gridy = 0;
    GBConst.gridwidth = 1;
    GBConst.gridheight = 1;
    GBConst.anchor = GridBagConstraints.CENTER;
    GBConst.fill = GridBagConstraints.NONE;
    GBConst.insets = new Insets(0,0,0,0);
    GBConst.weightx = 1;
    GBConst.weighty = 1;
 
    getContentPane().add(new JButton("North"), GBConst);
 
    GBConst.gridx = 1;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("Centre"), GBConst);
 
    GBConst.gridx = 1;
    GBConst.gridy = 2;
    getContentPane().add(new JButton("South"), GBConst);
 
    GBConst.gridx = 0;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("West"), GBConst);
 
    GBConst.gridx = 2;
    GBConst.gridy = 1;
    getContentPane().add(new JButton("East"), GBConst);   
 
    pack();
    setVisible(true);    
  }
 
  public static void main(String[] args){
    new Buttons();
  }
}

buttons.png


Learn GridBag, it is your friend.

Thanks for that, will have a read, although the book hasn't actually covered it yet - even if this way does seem A LOT simplier.

Thanks
 
Back
Top Bottom