When using setRollover (true), the buttons on the Swing panels are flat without borders, and the frame is drawn only when you hover / click the button. However, if the buttons are first added to the panel, and then the panel is added to the toolbar, this does not work. Is there an easy way to achieve this?
I want the buttons in JPanel to make them act as a single component (imagine a paging component with buttons first / prev / next / last page). I also want it to work independently of L & F (as if JPanel was not between the toolbar and buttons).
EDIT:
Compare the One and Two buttons (added directly) with the Three and Four buttons (added via 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 {
Here are the screenshots:
Toolbar on Nimbus LF:

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

same toolbar in Windows LF:

I would like the Three and Four buttons to work the same as the One and Two buttons.
Jirka source share