1

I'm creating a Swing GUI that uses a JToolbar. At the moment, I'm using a MigLayout and placing the toolbar inside of that. However, the result is that the toolbar does not span the whole width of the screen, but instead is only as long as it needs to be based on the buttons that I have added. This is my code concerning the initialization of the toolbar:

public class CoolButtons {

private JFrame frame;

public CoolButtons() {
    initialize();
}

private JButton btnAddEmployee, btnSaveChanges, btnRemoveEmployee, btnHome;
Border matte = BorderFactory.createMatteBorder(0, 0, 3, 0, Color.BLACK);
private JSeparator separator;
private JToolBar toolBar;
private ImageIcon save, addNew, removeOne, deleteAll, home;

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    ToolTipManager.sharedInstance().setInitialDelay(200);

    toolBar = new JToolBar("Tools", JToolBar.HORIZONTAL);
    toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));

    btnAddEmployee = new JButton();
    //Configure the button... not important
    toolBar.add(btnAddEmployee);

    btnSaveChanges = new JButton();
    //Configure the button... not important
    toolBar.add(btnSaveChanges)

    btnRemoveEmployee = new JButton();
    //Configure the button... not important
    toolBar.add(btnRemoveEmployee);

    btnHome = new JButton();
    //Configure the button... not important
    toolBar.add(btnHome);

    separator = new JSeparator();
    //Configure the separator... not important
    toolBar.add(separator);

    frame.getContentPane().add(toolBar, "cell 0 0");

    frame.pack();
}

When I run that code, I get this by default: Default

If I drag the JToolBar to the top of the screen, it expands to fill up the whole width of the screen like this: Expanded

My question is, how would I make the JToolBar fill up the whole width of the screen (as it does in the second picture) by default? As in, without having to drag it to the top of the screen.

I looked at this question "How to create Full Screen width jtoolbar", but it unfortunately didn't help. I tried the solution posted there, and nothing changed in my GUI.

Thank you for any advice or ideas :)

Community
  • 1
  • 1
Shannon
  • 100
  • 8
  • 1
    Does using the dock feature of the layout when adding the toolbar, as described in http://stackoverflow.com/a/11370432/616460 solve your problem? E.g. try creating it with "dock north" or something (may not be exactly that, can't test on phone) rather than just putting it in cell 0,0. – Jason C Sep 18 '16 at 14:07
  • See also http://migcalendar.com/forums/viewtopic.php?f=8&t=2358 – Jason C Sep 18 '16 at 14:13
  • Using the dock feature does indeed work perfectly, thank you very much for your help! – Shannon Sep 18 '16 at 14:16

0 Answers0