Creating a Swing Composite Component for JToolbar

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 { // 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:

Toolbar on Nimbus LF:

The toolbar on Nimbus LF

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

Covered nimbus

same toolbar in Windows LF:

The same toolbar on Windows LF

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

+4
source share
2 answers

1) I suggest installing JMenuBar as a container , not JToolbar ,

Disadvantages:

  • not movable and removable, neither of the Container

  • can be placed everywhere, but only inside the container, for example, like another JComponent , using the LayoutManager


2) for JToolbar it would be better to place there one JPanel nested by another JComponents , as shown in the code example


3) in your code example, you define one time JButton time, in Java you need to define as separate Objects

+1
source

Using another JToolbar instead of JPanel.

But: then I could (or maybe not?) Have a problem if I wanted to include a composite component in a dialog or something else than a toolbar. (This would be like two types of buttons: one for toolbars and one for the rest)

this is part of the code from the question by adding a panel 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); 
+1
source

Source: https://habr.com/ru/post/1401356/


All Articles