Question

So I want the button Select to be above button Back and I don't want them overlapping each other. But when I set them both the PAGE_END, they overlap. How do I get around this?

Here is the code for the problem:

public void methodName() {
    JPanel controls = new JPanel(new BorderLayout(5,5));
    final CardLayout cl = new CardLayout();
    final JPanel panel = new JPanel(cl);
    controls.add(panel);
    this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));        
    list = new JList<Object>(objectName);
        list.setVisibleRowCount(7);
        select = new JButton("Select");
        back = new JButton("Back");
        select.addActionListener(this);
        controls.add(new JScrollPane(list));
        controls.add(select, BorderLayout.PAGE_END);
        controls.add(back, BorderLayout.PAGE_END);
        controls.setBorder(new EmptyBorder(25,25,0,0));
        add(controls);
        refreshFrame();
}

Here is what it looks like when they are both added in but are overlapping:

The problem (Overlapping)

This is what I want it to look like:

This is what it should look like

Any ideas?

Thanks in advance!

Was it helpful?

Solution

Place the 2 JButtons on a new JPanel using GridLayout like this

JPanel basePanel = new JPanel(new GridLayout(0, 1, 0, 3));
basePanel.add(select);
basePanel.add(back);
controls.add(basePanel, BorderLayout.PAGE_END);

GridLayout can provide a vertical gap between buttons in its constructor as shown in your question

OTHER TIPS

create a new jpanel with gridLayout(2,1) then add the two buttons to the jpanel then add the Jpanel to the Jframe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top