Swing - How to imitate the why my look and feel draws disabled icons?

若如初见. 提交于 2021-01-28 20:59:06

问题


When you set the icon for a Swing control (say a JButton), a disabled icon with grayed-darker colors automatically generates. I want to be able to imitate the way the icon colors are changed for the disabled state, so I can create my own disabled icon with a little twist, and set it as the disabled button.

Is there a way to achieve this (other then first instantiating the control and retrieving it's icon, I'm looking for more straight-forward less hacky way)?


回答1:


Is there a way to achieve this (other then first instantiating the control and retrieving it's icon

Well the LAF is responsible for creating the disabled Icon. The getDisabledIcon() method from the AbstractButton button class looks something like this:

Icon disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, getIcon());

So in theory the LAF getDisabledIcon() method expects the component to be passed in as a parameter. So the answer to your question would be: "yes, the button needs to be created first".

However in practice it appears (for Metal and Windows at least) that the component is not actually used in the creation of the icon so you could do something like:

ImageIcon original = new ImageIcon( ... );
Icon disabled = UIManager.getLookAndFeel().getDisabledIcon(null, original);

Using this approach is risky because other LAF's may indeed need the component to create the disabled Icon.

However, instead of passing null, I guess you could create a single component and then call this method with multiple different icons, so it would still be a little better than creating a unique component for each Icon.

Here is the SSCCE I used to test this approach:

import javax.swing.*;
import javax.swing.plaf.metal.*;
import java.awt.*;
import java.awt.image.*;

public class ButtonDisabledIcon extends JPanel
{
    public ButtonDisabledIcon()
    {
        ImageIcon original = new ImageIcon( "dukewavered.gif" );

        JButton button1 = new JButton( "Original" );
        button1.setIcon( original );
        add(button1);

        JButton button2 = new JButton( "Disabled" );
        button2.setIcon(original);
        button2.setEnabled(false);
        add(button2);

        JButton button3 = new JButton( "LAF Disabled" );
        button3.setIcon( UIManager.getLookAndFeel().getDisabledIcon(null, original) );
        add(button3);
  }
    private static void createAndShowUI()
    {
/*
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }
*/
        JFrame frame = new JFrame("Button Disabled Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ButtonDisabledIcon() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}



回答2:


Amplifying on @camickr's answer, you can experiment with getGray() and this L&F toolbar.

gray icons

JToggleButton jtb = new JToggleButton("Plain", icon);
jtb.setEnabled(true);
this.add(jtb);

jtb = new JToggleButton("Disabled", icon);
jtb.setEnabled(false);
this.add(jtb);

jtb = new JToggleButton("Gray enabled", getGray(icon));
jtb.setEnabled(true);
this.add(jtb);

jtb = new JToggleButton("Gray disabled", getGray(icon));
jtb.setDisabledIcon(getGray(icon));
jtb.setEnabled(false);
this.add(jtb);


来源:https://stackoverflow.com/questions/18848329/swing-how-to-imitate-the-why-my-look-and-feel-draws-disabled-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!