2

I'm developing a Swing based application in which I want to add JToolBar with images in JButton but it's not looking good. JToolBar is having some dots at the starting part.

How can I get rid of the dots?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mukthi
  • 561
  • 4
  • 13
  • 35

1 Answers1

3

Two things:

  1. The "dots" you describe are probably due to the JToolbar being floatable by default. If you wish to disable this you can call setFloatable(false).
  2. Here's a utility method I use to decorate JButtons prior to adding them to JToolBars (or JPanels, etc):

decorateButton(AbstractButton)

public static void decorateButton(final AbstractButton button) {
    button.putClientProperty("hideActionText", Boolean.TRUE);
    button.setBorder(BorderFactory.createEmptyBorder());
    button.setBackground(null);
    button.setOpaque(true);
    button.setPreferredSize(BUTTON_SIZE);
    button.setMaximumSize(BUTTON_SIZE);
    button.setMinimumSize(BUTTON_SIZE);
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            button.setBackground(COLOR_BUTTON_MOUSEOVER);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            button.setBackground(COLOR_BUTTON_PRESSED);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            button.setBorder(button.isEnabled() ? BORDER_BUTTON_MOUSEOVER_ENABLED : BORDER_BUTTON_MOUSEOVER_DISABLED);
            button.setBackground(COLOR_BUTTON_MOUSEOVER);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            button.setBorder(BorderFactory.createEmptyBorder());
            button.setBackground(null);
        }
    });
}
Community
  • 1
  • 1
Adamski
  • 54,009
  • 15
  • 113
  • 152
  • +1 for point 1. I think point 2. is just muddying the waters. – Andrew Thompson Oct 21 '11 at 09:17
  • 1
    @Andrew Thompson agreed with point 1, but would go a lot further than "muddying" on point 2 - as in "don't without an extremely good reason" – kleopatra Oct 21 '11 at 09:27
  • Would be interested to know the correct approach as realise (2) is a bit of a hack. For example, I'm using the Alloy L&F which is great but the JToolBar buttons are too wide. However, IDEA also uses Alloy but the toolbar buttons are smaller and have the same rollover effect as achieved by my util method above. This isn't out-of-the-box functionality. – Adamski Oct 21 '11 at 11:35