简体   繁体   中英

Java Nimbus L&F issue Toolbar JButton

I have a problem with Nimbus Look and Feel when I use JToolbar and JButton in the Toolbar. I have the Problem just with Nimbus if I use Metal, the Button are shown correctly.

The Buttons in the ToolBar are just Visible if they are clicked or mouseover and in Metal they are by standard visible. My Problem or Question, is there a solve for this problem ?

Example Code:

Button: test, test1 are in the Toolbar and not Visible and Button test2 isn't in the Toolbar an looks normal and I want that the Button test, test1 looks like test2 just that they are in the Toolbar.

import java.awt.FlowLayout;

import javax.swing.*;

public class nimbus extends JFrame{

    public nimbus()
    {
        //setLook("javax.swing.plaf.metal.MetalLookAndFeel");
        setLook("javax.swing.plaf.nimbus.NimbusLookAndFeel");

        FlowLayout fl = new FlowLayout();
        fl.setAlignment(fl.LEFT); 

        this.setLayout(fl);
        this.setSize(100, 200);

        JToolBar tbar = new JToolBar();
        tbar.setLayout(fl);
        tbar.setSize(800, 40);
        tbar.setFloatable(false);

        JButton btn = new JButton("test");
        btn.setSize(50, 23);
        tbar.add(btn);

        btn = new JButton("test1");
        btn.setSize(50, 23);
        tbar.add(btn);

        this.add(tbar);

        btn = new JButton("test2");
        btn.setSize(50, 23);
        this.add(btn);
    }

    public static void main(String[] args){
        nimbus n = new nimbus();
        n.setVisible(true);
    }
    public void setLook(String look)
    {

        try {
            UIManager.setLookAndFeel(look);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Your problem seems to be the different LookAndFeel behaviour.
Means in special, NimbusLookAndFeel-ToolbarUI has an different implementation than MetalLookAndFeel-ToolbarUI.

As i posted in an different Thread, the MetalLookAndFeel-ToolbarUI impl looks like:

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;
           }
       }
  ...

Take a look at: MetalUtils.isToolBarButton(c)

To change this behaviour, i am afraid you have to create your own ToolbarUI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM