Question

When you add a JButton to a JToolbar, the button takes a specific look (not the same that if you add it to a Jpanel). I created some component similar to a JToolbar and I would like the same behavior. Problem: I checked the JToolbar class to find some specific code in charge to change the appearence of added components (change component specific paint method or UI delegates, etc). I didn't find anything! I don't understand how JToolbar works. Could anyone explain me how it works?

Thanks a lot,

Herve Guillaume

Was it helpful?

Solution

This appears to be handled by the update() method of the MetalButtonUI class. Here is the code from JDK5_07:

public void update(Graphics g, JComponent c) {
    AbstractButton button = (AbstractButton)c;
    if ((c.getBackground() instanceof UIResource) &&
              button.isContentAreaFilled() && c.isEnabled()) {
        ButtonModel model = button.getModel();
        if (!MetalUtils.isToolBarButton(c)) {
            if (!model.isArmed() && !model.isPressed() &&
                    MetalUtils.drawGradient(
                    c, g, "Button.gradient", 0, 0, c.getWidth(),
                    c.getHeight(), true)) {
                paint(g, c);
                return;
            }
        }
        else if (model.isRollover() && MetalUtils.drawGradient(
                    c, g, "Button.gradient", 0, 0, c.getWidth(),
                    c.getHeight(), true)) {
            paint(g, c);
            return;
        }
    }
    super.update(g, c);
}

The isToolBarButton() method just checks if the parent Container is a JToolBar, so I guess one solution is to always add your JButton to a JToolBar and then add the toolbar to your real Container.

Otherwise, I guess, you would need to write your own custom UI and override the update() method.

OTHER TIPS

I think it's nothing but a disable button. if you make you make your button disable it will look similar to one in toolBar (except the black text color). To change text color of disabled button you can override UIManager's default property. And to make button work more likely as in toolBar add mouseListener to it and change its enable status in mouseEnter and Exit method.

Example:

    JFrame frame = new JFrame("tool bar button demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(150, 150);
    frame.setLayout(new FlowLayout());

    JToolBar bar = new JToolBar();
    bar.add(new JButton("A button"));
    frame.add(bar);

    // to make text black in disabled button.
    UIManager.getDefaults().put("Button.disabledText",Color.BLACK);

    JButton button = new JButton("A button");
    button.setEnabled(false);
    // if you are setting true or not changing the roll over property 
    // of toolBar then following listerner help to give similar effect
    button.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent me) {
        }
        @Override
        public void mousePressed(MouseEvent me) {
        }
        @Override
        public void mouseExited(MouseEvent me) {
            ((JButton)me.getSource()).setEnabled(false);
        }
        @Override
        public void mouseEntered(MouseEvent me) {
            ((JButton)me.getSource()).setEnabled(true);
        }
        @Override
        public void mouseClicked(MouseEvent me) {
        }
    });
    frame.add(button);

    frame.setVisible(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top