Java 7 JColorChooser: 禁用透明度滑块

10
JDK 7在JColorChooser中添加了一个新的透明度滑块:

enter image description here

问题在于我不想让用户选择透明颜色。不幸的是,禁用滑块似乎没有简单的方法。
一个去除透明度的方法是基于所选颜色创建新颜色,但这会给用户留下错误印象,因为滑块现在实际上无效,我不希望有一个无用的UI元素。
所以我的问题是,如何最好地摆脱透明度滑块?
附言:在我看来,他们只是添加了滑块并使其成为默认行为,这可能会在JDK 6程序中引起许多错误,这些程序不希望颜色选择器返回具有alpha值的颜色。
5个回答

5
根据文档,可以直接修改/配置现有的类。因此,建议的做法是创建自己的 ChooserPanels(它们需要扩展 AbstractColorChooserPanel),然后调用。
JColorChooser jc = new JColorChooser();
jc.setChooserPanels(new AbstractColorChooserPanel[]{yourChooserPanel});

或者,如果你正在寻找一种更快/更丑/更简单的方法来完成它,我为你编写了以下代码:

private static void removeTransparencySlider(JColorChooser jc) throws Exception {

    AbstractColorChooserPanel[] colorPanels = jc.getChooserPanels();
    for (int i = 1; i < colorPanels.length; i++) {
        AbstractColorChooserPanel cp = colorPanels[i];

        Field f = cp.getClass().getDeclaredField("panel");
        f.setAccessible(true);

        Object colorPanel = f.get(cp);
        Field f2 = colorPanel.getClass().getDeclaredField("spinners");
        f2.setAccessible(true);
        Object spinners = f2.get(colorPanel);

        Object transpSlispinner = Array.get(spinners, 3);
        if (i == colorPanels.length - 1) {
            transpSlispinner = Array.get(spinners, 4);
        }
        Field f3 = transpSlispinner.getClass().getDeclaredField("slider");
        f3.setAccessible(true);
        JSlider slider = (JSlider) f3.get(transpSlispinner);
        slider.setEnabled(false);
        Field f4 = transpSlispinner.getClass().getDeclaredField("spinner");
        f4.setAccessible(true);
        JSpinner spinner = (JSpinner) f4.get(transpSlispinner);
        spinner.setEnabled(false);
    }
}

祝你好运:)

3

虽然这取决于实现方式,但您可以根据名称删除AbstractColorChooserPanel的具体子类。

以下示例将删除除RGB面板之外的所有面板:

AbstractColorChooserPanel[] ccPanels = chooser.getChooserPanels();
for (AbstractColorChooserPanel ccPanel : ccPanels) {
    System.out.println(ccPanel.getDisplayName());
    String name = ccPanel.getClass().getSimpleName();
    if (!"DefaultRGBChooserPanel".equals(name))
        tcc.removeChooserPanel(ccPanel);
}

这个例子恢复了HSB面板:

for (AbstractColorChooserPanel ccPanel : ccPanels) {
    String name = ccPanel.getClass().getSimpleName();
    if ("DefaultHSBChooserPanel".equals(name))
        tcc.addChooserPanel(ccPanel);
}

你需要通过实践确定所需的名称。


2
这里的其他答案展示了如何从JColorChooser实例中删除透明度滑块,但使用JColorChooser的主要方法是静态的showDialog方法,在这种情况下,您无法访问实例。因此,我提供了两种方法,一种可以隐藏JColorChooser实例中的控件,另一种是showDialog的替代方法,它具有额外的参数showTransparencyControls。请注意保留HTML标签。
import java.awt.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;

class SwingUtil {
    /**
     * Hides controls for configuring color transparency on the specified
     * color chooser.
     */
    public static void hideTransparencyControls(JColorChooser cc) {
        AbstractColorChooserPanel[] colorPanels = cc.getChooserPanels();
        for (int i = 0; i < colorPanels.length; i++) {
            AbstractColorChooserPanel cp = colorPanels[i];
            try {
                Field f = cp.getClass().getDeclaredField("panel");
                f.setAccessible(true);
                Object colorPanel = f.get(cp);

                Field f2 = colorPanel.getClass().getDeclaredField("spinners");
                f2.setAccessible(true);
                Object sliders = f2.get(colorPanel);

                Object transparencySlider = java.lang.reflect.Array.get(sliders, 3);
                if (i == colorPanels.length - 1)
                    transparencySlider = java.lang.reflect.Array.get(sliders, 4);

                Method setVisible = transparencySlider.getClass().getDeclaredMethod(
                    "setVisible", boolean.class);
                setVisible.setAccessible(true);
                setVisible.invoke(transparencySlider, false);
            } catch (Throwable t) {}
        }
    }


    /**
     * Shows a modal color chooser dialog and blocks until the dialog is
     * closed.
     * 
     * @param component the parent component for the dialog; may be null
     * @param title the dialog's title
     * @param initialColor the initial color set when the dialog is shown
     * @param showTransparencyControls whether to show controls for
     *        configuring the color's transparency
     * @return the chosen color or null if the user canceled the dialog
     */
    public static Color showColorChooserDialog(Component component,
            String title, Color initialColor, boolean showTransparencyControls) {
        JColorChooser pane = new JColorChooser(
            initialColor != null ? initialColor : Color.white);
        if (!showTransparencyControls) hideTransparencyControls(pane);
        Color[] result = new Color[1];
        ActionListener okListener = e -> result[0] = pane.getColor();
        JDialog dialog = pane.createDialog(component, title, true, pane, okListener, null);
        dialog.setVisible(true);
        dialog.dispose();
        return result[0];
    }
}

1

Java 9 添加了一个新的属性到AbstractColorChooserPanel,用于控制这些滑块:

public void setColorTransparencySelectionEnabled(boolean b);
public boolean isColorTransparencySelectionEnabled();

还有一个新的静态JColorChooser.showDialog方法的重载,用于指定相同的属性:

public static Color showDialog(Component component, String title, Color initialColor,
    boolean colorTransparencySelectionEnabled);

Java 9 预计将于2017年3月发布。


1
这是对aymeric答案的微小修改,它不是禁用它们,而是完全隐藏它们。
private static void removeTransparencySlider(JColorChooser jc) throws Exception {

    AbstractColorChooserPanel[] colorPanels = jc.getChooserPanels();
    for (int i = 1; i < colorPanels.length; i++) {
        AbstractColorChooserPanel cp = colorPanels[i];

        Field f = cp.getClass().getDeclaredField("panel");
        f.setAccessible(true);

        Object colorPanel = f.get(cp);
        Field f2 = colorPanel.getClass().getDeclaredField("spinners");
        f2.setAccessible(true);
        Object spinners = f2.get(colorPanel);

        Object transpSlispinner = Array.get(spinners, 3);
        if (i == colorPanels.length - 1) {
            transpSlispinner = Array.get(spinners, 4);
        }
        Field f3 = transpSlispinner.getClass().getDeclaredField("slider");
        f3.setAccessible(true);
        JSlider slider = (JSlider) f3.get(transpSlispinner);
        slider.setEnabled(false);
        slider.setVisible(false);
        Field f4 = transpSlispinner.getClass().getDeclaredField("spinner");
        f4.setAccessible(true);
        JSpinner spinner = (JSpinner) f4.get(transpSlispinner);
        spinner.setEnabled(false);
        spinner.setVisible(false);

        Field f5 = transpSlispinner.getClass().getDeclaredField("label");
        f5.setAccessible(true);
        JLabel label = (JLabel) f5.get(transpSlispinner);
        label.setVisible(false);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接