1

I have a really hard time with java and its swing components, i must say this was a lot easier when i programmed with C#, and i am a little bit stuck with this code.

What i am trying to do is to add some panels on another panel. This to show the user the tasks that needs to be done by the project he joined.

On the left you see the panel, and i want to add more panels to it.

I created a panel on the right side of the Gui, and i want to add more panels to it during runtime. I managed to add one panel to it, but it has some strange behaviors.

enter image description here

The blue panel is the newly created panel i added during runtime with this code.

  JPanel pnl = new JPanel();
  lpane.setBackground(Color.red);
  lpane.setLayout(new BorderLayout());
  pnl.setBounds(0, 0, 20, 100);
  pnl.setOpaque(true);
  pnl.setBackground(Color.BLUE);
  lpane.add(pnl);
  lpane.validate();

This is just a test, that's why the code does not contain a for-loop etc for the rest of the panels. As you can see i am using BorderLayout because i found this on the internet, and because without BorderLayout it won't draw anything. Also when i am trying to set the BorderLayout to .NORTH or .STARTPAGE it starts drawing above in the panel but i still can't set any positions of the panel?

Somebody knows why i cannot set any positions or WIDTH and HEIGTH?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
  • The short answer is that you will have to work through the Swing tutorials on the Oracle website. Java layout is different to that of .Net applications, as you can see. I humbly suggest you first visit http://docs.oracle.com/javase/tutorial/uiswing/ and work through a few tutorials so you can understand the different layout managers. Then things like BorderLayout etc. will make so much more sense. – Ewald Jun 01 '12 at 08:16

3 Answers3

3
  • BorderLayout has implemented 5. areas for placing JComponents,

  • last added JComponent to the concrete area is visible by using BorderLayout

  • use (pre_implemented in JPanel) FlowLayout, accepting PreferredSize came from JComponents

  • GridLayout for childs with the same size on the screen

  • a few rules How Swing LayoutManager works

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you for this information, its makes it a little bit more clear. But i must say it is all pretty hard to understand when you come from c# so i have to create the overview for myself again and experiment with it. – Dion Segijn Jun 01 '12 at 11:22
3

Ok, so here's some code that will help you along, but I really suggest you spend some time working through the Swing tutorials at http://docs.oracle.com/javase/tutorial/uiswing/

The Layout Managers exist to make layouts flexible and dynamic, but it takes some time and practice to really understand them. The code below is one way of doing what you want, but it might not be the right code for all use cases.

NOTE: This is NOT production level code, but rather more to illustrate a point.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Simple app to demonstrate how to use basic layout managers.
 * @author ewald
 */
public class LayoutPanels {

    private JFrame frame = new JFrame("The Top Frame");

    public LayoutPanels() {
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }


    /**
     * Sample code - this is not best practice, but it will help
     * you to make some progress.
     */
    public void startUp() {
        JPanel topPanel = new JPanel(new BorderLayout());

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.BLUE);
        eastPanel.setLayout(new FlowLayout());
        eastPanel.setPreferredSize(new Dimension(150,200));

        JPanel onePanel = new JPanel(new FlowLayout());
        JPanel twoPanel = new JPanel(new FlowLayout());
        JPanel threePanel = new JPanel(new FlowLayout());

        onePanel.setPreferredSize(new Dimension(100,50));
        twoPanel.setPreferredSize(new Dimension(100,50));
        threePanel.setPreferredSize(new Dimension(100,50));

        onePanel.setBackground(Color.RED);
        twoPanel.setBackground(Color.GREEN);
        threePanel.setBackground(Color.YELLOW);

        eastPanel.add(onePanel);
        eastPanel.add(twoPanel);
        eastPanel.add(threePanel);

        topPanel.add(eastPanel, BorderLayout.EAST);

        frame.getContentPane().add(topPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        LayoutPanels app = new LayoutPanels();
        app.startUp();
    }

}
Ewald
  • 5,691
  • 2
  • 27
  • 31
  • +1 for the answer, but especially for *"This is NOT production level code, but rather more to illustrate a point"*. You'd think we would not need to state that explicitly, but then again.. ;) – Andrew Thompson Jun 01 '12 at 09:44
  • @AndrewThompson I just KNEW you were going to read it Andrew, and with that in mind, I thought I'd have to add some legal cover there! Seriously though, it's not the best code, but I find it easier to explain it this way, and then later on introduce more advanced concepts and ideas. Thanks for the +1, I appreciate it. – Ewald Jun 01 '12 at 09:59
2

There would be several approaches to this. One way would be to use BoxLayout.Y_AXIS on your parent panel. This will cause your child panels the be added below eachother when they are added. As apposed to BorderLayout which has 5 areas and will not be the right approach for this task.

JPanel pnl = new JPanel();
pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS)); // This will cause your new panels to be added below eachother
pnl.setBackground(Color.BLUE);
//and set sizes etc

JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // set layout on your child panel. FlowLayout is default but you might want to read up on the different layoutmanagers to pick the right one.
newPanel.setSize(50, 50); // you can use pnl.getWidth to make your panels to have the same width as it's parent pane, but you probably want the height to be locked
//and add the contents to this panel
pnl.add(newPanel);
John Snow
  • 5,214
  • 4
  • 37
  • 44
  • FlowLayout and BoxLayout pretty to accepting PrefferedSize that came from JComponents +1, please remove code line `newPanel.setSize(50, 50);` – mKorbel Jun 01 '12 at 11:30