4

When using setRollover(true), buttons on Swing toolbars are flat without border and the border is drawn only when hovering/pushing the button. However, if the buttons are first added to a panel, and then the panel is added to the toolbar, this does not work. Is there some easy way how to achieve it?

I want the buttons to be in a JPanel to make them act as a single component (imagine a paging component with first/prev/next/last page buttons). I also want it to work regardless of L&F (as it would if the JPanel was not between the toolbar and the buttons).

EDIT:

Compare the buttons One & Two (added directly) with buttons Three & Four (added via a JPanel) in the following example:

import javax.swing.*;

public class ToolbarTest extends JFrame {
    ToolbarTest() {
        JToolBar toolbar = new JToolBar();
        toolbar.setRollover(true);

        JButton button = new JButton("One");
        button.setFocusable(false);
        toolbar.add(button);

        button = new JButton("Two");
        button.setFocusable(false);
        toolbar.add(button);

        JPanel panel = new JPanel();
        button = new JButton("Three");
        button.setFocusable(false);
        panel.add(button);

        button = new JButton("Four");
        button.setFocusable(false);
        panel.add(button);

        toolbar.add(panel);

        add(toolbar);
        pack();
    }

    public static void main(String[] args) throws Throwable {
        // optional: set look and feel (some lf might ignore the rollover property)
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {      // or "Windows", "Motif"
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

        ToolbarTest frame = new ToolbarTest();
        frame.setVisible(true);
    }
}

Here are the screenshots:

The toolbar on Nimbus LF:

The toolbar on Nimbus LF

The same toolbar when mouse hovers over the second button (the mouse cursor is not shown):

Hovered Nimbus

The same toolbar on Windows LF:

The same toolbar on Windows LF

I would like the Three and Four buttons to work the same way as the One and Two buttons.

Jirka
  • 4,184
  • 30
  • 40
  • 1
    never saw that. please edit question with your [SSCCE](http://sscce.org/) – mKorbel Mar 14 '12 at 11:30
  • I have added an example. – Jirka Mar 14 '12 at 16:05
  • must it be JToolBar ???, are you needed to moving with JToolBar on the screen???, – mKorbel Mar 14 '12 at 16:08
  • I prefer the standard JToolbar if possible, but if there is some other toolbar implementation that can handle this then I am not against using it. (If you question is whether I need to use a toolbar in general, then yes - the app has a menu, toolbar, etc. the LF makes sure that the toolbar acts as a toolbar should on a given platform, it is detachable, etc) – Jirka Mar 14 '12 at 16:39
  • good question :-) Looking into the code reveals that BasicToolBarUI handles only the direct children. Not much to do about that ... – kleopatra Mar 14 '12 at 16:50
  • kleopatra: Thanks. I should have looked at that. – Jirka Mar 14 '12 at 19:54

2 Answers2

1

1) I'd suggesting to set JMenuBar as container rather than JToolbar,

disadvantages:

  • isn't moveable and detachable, nor out of Container

  • could by placed everywhere but only inside Container, like as another JComponent by using LayoutManager


2) for JToolBar would be better to place there one JPanel nested another JComponents, as shows from your code example


3) in your code example you define one JButton fouth times, in Java is required define as separate Objects

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Ad 3): I do define 4 separate JButton objects. Only the variable is reused. The objects are not. – Jirka Mar 14 '12 at 18:20
  • I am not sure I understand. I want to get the roll-over effect for buttons embedded in a panel, not just directly added button. But if I use JMenuBar as you suggest then I do not get the effect for any buttons. – Jirka Mar 14 '12 at 19:18
1

Using another JToolbar instead of JPanel works.

But: then I would probably (or maybe not?) have a problem, if I wanted to include the composite component into a dialog or something else than a toolbar. (This would be similar to having two types of buttons, one for toolbars and one for the rest)

this is the portion of the code from the question adding a panel changed to add a toolbar.

JToolBar component = new JToolBar();
component.setRollover(true);
component.setBorder(null);
component.setFloatable(false);

button = new JButton("Three");
button.setFocusable(false);
component.add(button);

button = new JButton("Four");
button.setFocusable(false);
component.add(button);

toolbar.add(component);
Jirka
  • 4,184
  • 30
  • 40