Pavan Nihal
Pavan Nihal

Reputation: 1

Java Swing JPanel BorderLayout doesn't work as I expected

First of all I have to say that I started programming in Java only 3 days ago.

So please be patient and try to give me a detailed explanation.

So I am trying to create this demo GUI using Swing. I just want to initially test the layout of different components before coding the complete design. So I wrote this small code to add 3 buttons to a JPanel.

import javax.swing.*;
import java.awt.*;

public class demoGUI_v1{
    JButton button1,button2,button3;
    JFrame frame;
    JPanel panel,panel2;    
    public static void main(String[] args){
        demoGUI_v1 gui = new demoGUI_v1();
        gui.framework();
    }

    public void framework(){
        frame = new JFrame();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel2 = new JPanel();
        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        panel2.add(BorderLayout.CENTER,panel);
        frame.getContentPane().add(panel, BorderLayout.WEST);
        frame.setVisible(true);
    }
}

enter image description here

Upvotes: 0

Views: 364

Answers (3)

Emiliano Schiano
Emiliano Schiano

Reputation: 1922

Here you have a simpler way to achieve what you need:

public void framework(){
    frame = new JFrame();
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    panel2 = new JPanel();
    button1 = new JButton("Button1");
    button2 = new JButton("Button2");
    button3 = new JButton("Button3");
    panel.add(Box.createVerticalGlue());
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(Box.createVerticalGlue());
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(panel, BorderLayout.LINE_START);
    frame.setVisible(true);
}

Upvotes: 0

camickr
camickr

Reputation: 324197

Not directly related to your question but:

frame.getContentPane().add(BorderLayout.WEST,panel);

Don't use that format of the add(...) method. As the API says:

This method is obsolete as of 1.1. Please use the method add(Component, Object) instead.

Also, since JDK4 you don't need to get the content pane. So you can use:

frame.add(panel, BorderLayout.LINE_START); // preferred over "WEST"

Class names should start with an upper case character and should not use "_" in the class name. Again, just look at the API to see the class names used. Don't make up your own conventions.

I would suggest the tutorial you got your original code from is very old. I would suggest you start by using the Swing tutorial for examples and explanations. The tutorial covers all the layout managers and should help explain why Eric's suggestion works.

Upvotes: 0

Eric Leibenguth
Eric Leibenguth

Reputation: 4277

Add some vertical glue, to center the components vertically:

    panel.add(Box.createVerticalGlue());
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(Box.createVerticalGlue());

Upvotes: 1

Related Questions