Reputation: 4213
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 same toolbar when mouse hovers over the second button (the mouse cursor is not shown):
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.
Upvotes: 4
Views: 2306
Reputation: 4213
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);
Upvotes: 1
Reputation: 109815
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
Upvotes: 1