Question

I'm trying to accomplish this layout: http://www.domizai.ch/tmp/targetLayout.jpg

But what i get now is this: http://www.domizai.ch/tmp/layoutNow.jpg

I Simply want panel2 to y-align it in the Center and not to stretch it to the height of panel1.

My code looks like this right now:

JPanel panel1 = new JPanel();
panel1.add(new JLabel(icon,JLabel.CENTER));
panel1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(new JLabel("Copy files to Desktop..."),BorderLayout.NORTH);
panel2.add(bar,BorderLayout.CENTER);
panel2.add(label = new JLabel("1 minute remaining");,BorderLayout.SOUTH);

JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(panel1,BorderLayout.WEST);
panel3.add(panel2,BorderLayout.EAST);

JPanel panel4 = new JPanel();
panel4.setLayout(new BorderLayout());
panel4.add(new JLabel(),BorderLayout.LINE_START);
panel4.add(button,BorderLayout.LINE_END);
panel4.setBorder(BorderFactory.createEmptyBorder(10,0, 0, 0));

JPanel panel5 = new JPanel();
panel5.setLayout(new BorderLayout());
panel5.add(panel3, BorderLayout.PAGE_START);
panel5.add(panel4, BorderLayout.PAGE_END);
panel5.setBorder(BorderFactory.createEmptyBorder(20,20, 10, 20)); // top, left bottom, right

frame.setContentPane(panel5);

What's the best way? Thx!

Was it helpful?

Solution

You can try this:

  • The content pane is has BorderLayout. It contains:
    • A label with the icon at WEST
    • A panel at CENTER (or EAST). The panel has a GridLayout with 1 column and 5 rows. The panel contains:
      • An empty panel
      • The label of "Copy file to desktop"
      • The progress bar
      • The label of "1 minute remainding"
      • An empty panel

OTHER TIPS

For your own sanity, I recommend looking into a different layout manager - MigLayout is pretty good. You could also try using the standard BoxLayout manager, which IIRC lets you add expanding gaps ("glue") to your layout.

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