-1

I'm using WindowBuilder in Eclipse Indigo to get a handle on the different layouts. Basically I'm just trying to gain a thorough understanding of how each one works and which are best for different scenarios. The below code works, but there are a few things I would like to tweak but I'm not sure how to get my desired effect.

public class Dashboard extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Dashboard frame = new Dashboard();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Dashboard() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 602, 372);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel drawingPanel = new JPanel();
    drawingPanel.setBounds(6, 6, 487, 251);
    contentPane.add(drawingPanel);
    drawingPanel.setLayout(null);

    JScrollPane propertiesScrollPane = new JScrollPane();
    propertiesScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    propertiesScrollPane.setBounds(6, 264, 487, 80);
    contentPane.add(propertiesScrollPane);

    JPanel selectionPanel = new JPanel();
    propertiesScrollPane.setViewportView(selectionPanel);
    selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS));

    JPanel surfacesPanel = new JPanel();
    selectionPanel.add(surfacesPanel);
    surfacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
    surfacesPanel.setLayout(new BoxLayout(surfacesPanel, BoxLayout.Y_AXIS));

    JCheckBox surfaceCheck = new JCheckBox("Visible");
    surfaceCheck.setHorizontalAlignment(SwingConstants.CENTER);
    surfacesPanel.add(surfaceCheck);

    JButton surfaceButton = new JButton("Surfaces");
    surfacesPanel.add(surfaceButton);

    JPanel spacesPanel = new JPanel();
    selectionPanel.add(spacesPanel);
    spacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
    spacesPanel.setLayout(new BoxLayout(spacesPanel, BoxLayout.Y_AXIS));

    JCheckBox spacesCheck = new JCheckBox("Visible");
    spacesCheck.setHorizontalAlignment(SwingConstants.CENTER);
    spacesPanel.add(spacesCheck);

    JButton spacesButton = new JButton("Spaces");
    spacesPanel.add(spacesButton);

    JScrollPane scrollPane_1 = new JScrollPane();
    scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane_1.setBounds(504, 6, 92, 251);
    contentPane.add(scrollPane_1);

    JLabel logoLabel = new JLabel("Logo Here");
    LogoLabel.setHorizontalAlignment(SwingConstants.CENTER);
    LogoLabel.setBounds(505, 264, 91, 80);
    contentPane.add(logoLabel);
}
}

Specifically:

I would like the selectionPanel anchored to the bottom left, the propertiesPanel anchored to the top right, the logoLabel anchored to the bottom right and the drawingPanel anchored to the top left. the idea is that when the main form is resized, the scroll panels and label should stay exactly where they are relative to the edge of the parent form, while the drawingPanel should be resizable horizontally and vertically so that it takes up all the remaining space no matter the size of the top frame. The logo shouldn't be resizable in any dimension, while the selectionPanel should be resizable in the x dimension, and the properties window should be resizable in the y dimension. Lastly, I would like the buttons inside the surfaces and spaces panels to take up the maximum amount of space within the panels. That is to say, without having to do trial and error or specify magic number values, if the panel is 50x50 pixels, and the checkbox is 10 pixels high, then the button should be 40 pixels high.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NickAbbey
  • 1,201
  • 2
  • 10
  • 17
  • 1
    Your problem is in your use of null layout rather than try to use the layout managers. This is where they shine. It's like you are choosing to throw out one of the more powerful features of the Swing library and are then stuck because you're doing this. The solution is to avoid null layouts and absolute positioning like the plague. I suggest that you ditch the window builder for now and read the layout manager tutorials for a better understanding on how they work. – Hovercraft Full Of Eels Jan 16 '13 at 00:47
  • I switched to gridbag but am having the exact same issues... What do I need to look at to get a better understanding of this stuff? – NickAbbey Jan 16 '13 at 00:50
  • Looks like the constraints are what I want to be learning here...? – NickAbbey Jan 16 '13 at 00:51
  • There are so many posts on layout managers here, many answered by me, why not have a look at some. But firstly read the tutorials as it's described pretty well there. Learn to nest JPanels, each using its own layout, avoid using GridBagLayout unless absolutely necessary, consider even downloading some layouts such as MigLayout, but mostly experiment, try, test. – Hovercraft Full Of Eels Jan 16 '13 at 00:53
  • I've been looking through lots of posts on layout managers here (and noticed many of your answers). It seems that most of what I'm finding are questions for specific issues. I've already read the tutorials and am in the process of trying to apply what I've read. I'll just keep at it I suppose and when I get stuck on something more specific I'll just post a question. Thanks for the responses! – NickAbbey Jan 16 '13 at 00:58

1 Answers1

2

See this answer which show two ways to stretch a button to the size of the parent container.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433