0

I have been stuck in this for a while. The problem is that I want to shift my button to the right side of my screen, but when I use borderlayout.east it removes the other button that I created. Can anyone explain why it does so, and how do I fix this problem?

    public static void main(String args[]){
        GUI();

    }


    public static void GUI(){

        handle handle = new handle();
        JButton buy[] = new JButton[8];


        _panel.setLayout(new BorderLayout());


        for(int i = 0; i < buy.length; i++){

            buy[i] = new JButton("Buy");
            _panel.add(buy[i],BorderLayout.EAST);

        }


        _panel.setPreferredSize(new Dimension(600,600));        

        //_panel.add(buy, BorderLayout.EAST);

        _frame.add(_panel);
        _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _frame.setLocation(500, 100);
        _frame.setResizable(false);
        _frame.getContentPane();
        _frame.pack();
        _frame.setVisible(true);

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
logger
  • 1,983
  • 5
  • 31
  • 57
  • You are adding all button at the same region i.e.`BorderLayout.EAST`, instead you may possibly use [BoxLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html) –  May 23 '14 at 05:31
  • I have read through BoxLayout, BorderLayout and bag....but none of them work the way I wanted it to. – logger May 23 '14 at 05:33

3 Answers3

2

Adding to the layout replaces the other content, you will probably want to add a Panel to the east and then append buttons to that, something like this:

    JPanel subPanel = new JPanel(new FlowLayout());
    _panel.add(supPanel, BorderLayout.EAST);

    for(int i = 0; i < buy.length; i++){

        buy[i] = new JButton("Buy");
        subPanel.add(buy[i]);

    }
imojilover
  • 61
  • 1
  • 1
  • 4
0

According to #JavaDoc.

BORDER LAYOUT

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER.


You are adding all JButtons to east of JPanel.

 for(int i = 0; i < buy.length; i++){
        buy[i] = new JButton("Buy");
        _panel.add(buy[i],BorderLayout.EAST);<-------------
    }

So last one only gets visible at east.


So how should I fix it?

  • Put all buttons to JPanel (say with GridLayout) and add that JPanel to East of the JFrame or other JPanel.

    FOR EXAMPLE

    JPanel myPanel=new JPanel();
    myPanel.setLayout(new GridLayout(Button_array.length-1,1))
    for(int i = 0; i < buy.length; i++){
        buy[i] = new JButton("Buy");
        _myPanel.add(buy[i]);
    }
    _panel.add(myPanel,BorderLayout.EAST)
    
akash
  • 22,664
  • 11
  • 59
  • 87
0

This is the General Layout:

General Layout

Check Nested Layout Example for more information.

Community
  • 1
  • 1
Krishna
  • 673
  • 3
  • 6
  • 21