Take the 2-minute tour ×

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?

share|improve this question
Post an SSCCE as soon as possible. Because everything works fine in my case. –  Branislav Lazic Apr 20 '13 at 12:44

1 Answer 1

up vote 1 down vote accepted

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 http://stackoverflow.com/a/16121288/230513
public class JToolBarTest {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
        // http://stackoverflow.com/a/11949899/230513

    private JToolBar createBar() {
        JToolBar toolBar = new JToolBar();
        return toolBar;

    private JPanel createPanel() {
        JPanel panel = new JPanel();
        Action buttonAction = new AbstractAction("Button"){

            public void actionPerformed(ActionEvent e) {
                    + " " + 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() {
            public void run() {
                new JToolBarTest().display();
share|improve this answer
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. Please don't hesitate to edit your question with an sscce, 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

Your Answer


