2

I have create a GUI using Java Swing (Windows Builder Pro) for a personal project and it has a JToolBar. I have added separators between the buttons in the tool bar.

JButton btnSave = new JButton("Save");
    btnSave.setToolTipText("Save");
    btnSave.setMnemonic('S');
    btnSave.setIcon(new ImageIcon(Main.class.getResource("/org/dbhaskaran/resources/Save32.png")));
    toolBar.add(btnSave);
    toolBar.add(new JSeparator(SwingConstants.VERTICAL));

    JButton btnDesign = new JButton("Design");
    btnDesign.setIcon(new ImageIcon(Main.class.getResource("/org/dbhaskaran/resources/wizard32.png")));
    toolBar.add(btnDesign);
    toolBar.add(new JSeparator(SwingConstants.VERTICAL));
  1. I don't understand why my JButtons are getting aligned to the right after adding the separator? And how do I fix it?
  2. When I click on one of the buttons on the tool bar, it leaves behind an ugly border - is there any way to get rid of it? Please see screenshot below:

enter image description here

SethO
  • 2,703
  • 5
  • 28
  • 38
Deepak B
  • 2,245
  • 5
  • 26
  • 28

2 Answers2

8

Figured out the answer to my first question:

Replace

toolBar.add(new JSeparator(SwingConstants.VERTICAL));

With

toolBar.addSeparator();

Now all the Icons in the tool bar are tightly packed. Only need to figure out if there is a way to get rid of the ugly button border that is left behind on the most recently used Jbutton in the JToolBar.

Deepak B
  • 2,245
  • 5
  • 26
  • 28
2

Only need to figure out if there is a way to get rid of the ugly button border that is left behind on the most recently used Jbutton in the JToolBar.

button.setFocusPainted(false)

repeat for all JButtons

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Michael Dunn
  • 818
  • 5
  • 3