0

I have a JToolbar that contains multiple JPanels (needed as I would like to have specific borders for each of them). Unfortunately the Look&Feel manager does not recognize the JPanels as belonging to a toolbar and the JButtons are thus renderer as normal buttons (i.e. without the special mouse-over effect you have on a toolbar).

Replacing the JPanels by JToolbars are not an option as the LAF renderer gives it a special background.

Any other options / hints?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tom
  • 1,375
  • 3
  • 24
  • 45

1 Answers1

1

As shown below, you can change a toolbar's layout and add components as desired. You can also have an arbitrary number of toolbars. The L&F combo is shown here. Note that the addSeparator() method of JToolBar supplies a L&F-specific JToolBar.Separator.

test image

import component.Laf;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 * @see https://stackoverflow.com/a/16121288/230513
 */
public class JToolBarTest {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
        // https://stackoverflow.com/a/11949899/230513
        f.add(Laf.createToolBar(f));
        f.add(createBar());
        f.add(createBar());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JToolBar createBar() {
        JToolBar toolBar = new JToolBar();
        toolBar.add(createPanel());
        toolBar.addSeparator();
        toolBar.add(createPanel());
        return toolBar;
    }

    private JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("Panel"));
        Action buttonAction = new AbstractAction("Button"){

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getActionCommand()
                    + " " + e.getSource().hashCode());
            }
        };
        panel.add(new JButton(buttonAction));
        panel.add(new JButton(buttonAction));
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JToolBarTest().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thanks a lot for the example. I see that also here the buttons are painted with a regular look&feel and not with the look&feel of a toolbar (i.e. typically button borders are visible only when the mouse is over it). The way I solved it is to replace all sub-panels by JToolbars but this is far from being ideal. – Tom Apr 20 '13 at 15:27
  • Aqua buttons render differently, but I see most L&Fs are the same. You might look at [size variants](http://stackoverflow.com/a/2900157/230513). Please don't hesitate to edit your question with an [sscce](http://sscce.org/), such as this one, that illustrates any problems you encounter. You can up-vote a useful answer by clicking the up-arrow at the left. – trashgod Apr 20 '13 at 19:14