1

I'm making a GUI in Swing, and as a layout I'm using different classes just to adhere to the MVC structure.

In my Main.class I made a layout in Swing, to show the MenuBar and a Toolbar, it runs perfectly fine, but it's not displaying correctly somehow. The only thing I notice is 'the border' from the JToolBar, but none of the buttons I put inside it, which leads me to think that there's an issue while properly outputting it to the correct Panel/Frame.

    JFrame frame = new JFrame("Title");
    JPanel panel = new JPanel();

    frame.setJMenuBar(new MenuBar());

    JToolBar toolbar = new ToolBar();


    GroupLayout panelLayout = new GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 0, Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGap(0, 400, Short.MAX_VALUE)
    );

    GroupLayout layout = new GroupLayout(frame.getContentPane());
    frame.getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(toolbar, GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(toolbar, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(22, Short.MAX_VALUE))
    );

    frame.pack();
    frame.setLocationRelativeTo (null); // Center on screen.
    frame.setVisible(true);
}

}

VilleFTW
  • 55
  • 8

1 Answers1

0

I'm using different classes just to adhere to the MVC structure.

There is no need to use different classes for that reason. That is not the point of MVC.

The only thing I notice is 'the border' from the JToolBar, but none of the buttons I put inside it

Well we can't really help because we have no idea what your ToolBar class does.

My suggestion is to forget about using an IDE to generate your GUI code. The generated code is impossible to read and can't be maintained.

What you want to do is so simple if you manually create the GUI.

To display the toolbar the basic code would be:

frame.add(new ToolBar(), BorderLayout.PAGE_START);

That's it a single line of code compared to all the code generated by the IDE.

And then for the main panel you would have:

JPanel panel = new JPanel();
panel.add( someComponent );
frame.add(panel, BorderLayout.CENTER);

Read the section from the Swing tutorial on How to Use BorderLayout for a complete example.

camickr
  • 321,443
  • 19
  • 166
  • 288