0

Greeting of the day, I have a JFrame and a Jpanel named panelMain. all my set-up for both the frame and panel are perfect. I have set the Layout of panelMain to BorderLayout. I have now created another panel named panel1. panel1 has a null layout. I added some buttons to panel1 (which has a null layout). then I added panel1 to WEST of panelMain. when I ran the program, panelMain showed up, but panel1 didn't. then I changed the Layout of panel1 from null to FlowLayout, then it appeared on the WEST side of panelMain. I don't know what problem is null layout facing when put in BorderLayout, can someone please help me out.

JPanel panelMain = new JPanel();
JPanel panelWEST = new JPanel();
JButton button = new JButton();
JButton button2 = new JButton();
Constructor(){
    setContentPane(panelMain);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(Frame.MAXIMIZED_BOTH);

    panelMain.setLayout(new BorderLayout());
    panelMain.setBackground(Color.CYAN);

    panelWEST.setLayout(null);
   

    button.setBounds(50,50,200,200);
    button.setText("West Button1");
    button2.setBounds(50,300,200,200);
    button2.setText("West Button2");
    panelWEST.setBackground(Color.PINK);
    panelWEST.add(button);
    panelWEST.add(button2);

    panelMain.add(panelWEST,BorderLayout.WEST);
    
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

Don't use null layouts!!! Swing was designed to be used with layout managers.

It is never a good idea to use random numbers and that is exactly what you are doing when you try to guess the size of a component.

It is the responsibility of each Swing component to determine its own preferred size. The layout manager then uses this information to set the size/location of components added to the panel. When you use a null layout you lose this functionality.

As a solution you might do something like:

JPanel buttonPanel = new JPanel( new GridLyaout( 0, 1) );
buttonPanel.add(button1);
buttonPanel.add(button2);

JPanel west = new JPanel( new BorderLayout() );
west.add(buttonPanel, BorderLayout.PAGE_START);

panelMain.add(west, BorderLayout.LINE_START);

If you want space on the left of the buttonPanel, then you can use an EmptyBorder. Read the Swing Tutorial for working examples. There area sections on:

  1. How to Use Borders
  2. A Visual Guide to Layout Managers
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Wow, stack overflow is getting me very confused. Before a week a guy toled me NEVER USE LAYOUT MANAGERS, he told me that using layout managers is always a bad practice, you should always use null layout, and now I am getting opposite feedback, can someone please help me out on should I use a null layout or not. –  Nov 05 '20 at 03:09
  • @meetprakashshah The answer in your last question stated: **This is bad practice. A very bad one when it comes to UI.** in reference to using a null layout and the setBounds() method. .And then goes on to say: **By using layout managers, you are solving this kind of problems easily,** The answer was stating that you should NOT be trying to position components freely. You should be using layout managers to logically position components on the frame. Please reread the entire answer again. – camickr Nov 05 '20 at 03:13
  • Camickr, can you please help me how can I arrange components at a specific location using layout managers? Thank you for all your help. –  Nov 05 '20 at 03:22
  • You don't use specific locations. The was the point in this answer and in the previous answer. Your original code was attempting to display two buttons vertically on the left side of the frame. I gave you an example of how to do this with layout managers in 6 lines of code without using any magic numbers. The layout manager set the location so you don't need to. How does the code I provided not solve your problem? – camickr Nov 05 '20 at 03:29
  • The code provided by you is perfect, but I think I need to study more about Layout Managers, Thank you –  Nov 05 '20 at 03:55