tags:

views:

46

answers:

2
    JPanel panel = new JPanel();
    bigbutton = new JButton("Big Button");
    clearbutton = new JButton("Clear Page");
    resetbutton = new JButton("Start Over");
    finishbutton = new JButton("Finish");


    panel.add(sometable);
    return panel; 

right now what happens is they sprawl horizontally. I want the four buttons horizontally and above the table.

+2  A: 

Set you panel's layout to BorderLayout, add your buttons to a box, add the box to your panel with BorderLayout.PAGE_START constraint, add your table with Borderlayout.CENTER constraint.

tulskiy
The OP might also consider adding the JButtons to a JToolBar instead of the 'box'.
Andrew Thompson
+1  A: 

right now what happens is they sprawl horizontally.

In addition to @tulskiy's and @Andrew Thompson's suggestions, it may be useful to note that the default layout of JPanel is FlowLayout, which arranges components in a horizontal row.

Addendum:

change JPanel's FlowLayout to BorderLayout or some other layout?

Choosing a layout is partly a matter of taste, but I'd combine the suggested approaches, as shown in How to Use Tool Bars. Note how ToolBarDemo extends JPanel and then invokes the superclass constructor to specify BorderLayout():

public class ToolBarDemo extends JPanel implements ActionListener {
    ...
    public ToolBarDemo() {
        super(new BorderLayout());
        ...
        JToolBar toolBar = new JToolBar("Still draggable");
        addButtons(toolBar);
        ...
        setPreferredSize(new Dimension(450, 130));
        add(toolBar, BorderLayout.PAGE_START);
        add(sometable, BorderLayout.CENTER);
    }
    ...
}

Absent extension, just use the constructor directly:

JPanel panel = new JPanel(new BorderLayout());

Alternative, use the setLayout() method inherited by JPanel.

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
trashgod
anyway to change Jpanel's FlowLayout to borderlayout or some other layout ?
Kim Jong Woo