Java7JColorChooser:禁用透明滑块

Java7JColorChooser:禁用透明滑块,java,swing,transparency,java-7,jcolorchooser,Java,Swing,Transparency,Java 7,Jcolorchooser,JDK 7将新的透明度滑块添加到: 问题是我不想让我的用户选择透明的颜色。不幸的是,似乎没有一个简单的方法来禁用滑块 消除透明度的一种方法是仅基于选定颜色创建新颜色,但删除alpha值。然而,这给用户留下了一个错误的印象,因为滑块现在实际上什么都不做,我不希望有一个无用的UI元素 所以我的问题是,去除透明度滑块的最佳方法是什么 附言:依我看,很奇怪,他们只是添加了滑块,并将其作为默认行为。这可能会导致JDK 6程序中出现许多错误,这些错误不希望颜色选择器返回带有alpha值的颜色。根据文档,只

JDK 7将新的透明度滑块添加到:

问题是我不想让我的用户选择透明的颜色。不幸的是,似乎没有一个简单的方法来禁用滑块

消除透明度的一种方法是仅基于选定颜色创建新颜色,但删除alpha值。然而,这给用户留下了一个错误的印象,因为滑块现在实际上什么都不做,我不希望有一个无用的UI元素

所以我的问题是,去除透明度滑块的最佳方法是什么


附言:依我看,很奇怪,他们只是添加了滑块,并将其作为默认行为。这可能会导致JDK 6程序中出现许多错误,这些错误不希望颜色选择器返回带有alpha值的颜色。

根据文档,只需修改/配置现有类即可。因此,建议您创建自己的ChooserPanel(它们需要扩展
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);
    }
}
private static void removetTransparencySlider(JColorChooser jc)引发异常{
AbstractColorChooserPanel[]colorPanels=jc.getChooserPanels();
对于(int i=1;i

祝您好运:)

尽管它依赖于实现,但您可以按名称删除
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);
}

您需要根据经验确定所需的名称。

这里是对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);
    }
}
private static void removetTransparencySlider(JColorChooser jc)引发异常{
AbstractColorChooserPanel[]colorPanels=jc.getChooserPanels();
对于(int i=1;i
这里的其他答案显示了如何从JColorChooser实例中删除透明滑块,但使用JColorChooser的主要方法是静态方法,在这种情况下,您无法访问实例。因此,我介绍了两种方法,一种是对JColorChooser实例隐藏控件,另一种是将
showDialog
作为额外参数的showTransparencyControls
替换方法:

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];
    }
}
import java.awt.*;
导入java.awt.event.ActionListener;
导入java.lang.reflect.Field;
导入java.lang.reflect.Method;
导入javax.swing.*;
导入javax.swing.colorchooser.AbstractColorChooserPanel;
斯温古提类{
/**
*隐藏用于在指定区域上配置颜色透明度的控件
*颜色选择器。
*/
公共静态无效hideTransparencyControls(JColorChooser cc){
AbstractColorChooserPanel[]colorPanels=cc.getChooserPanels();
对于(int i=0;ipublic void setColorTransparencySelectionEnabled(boolean b);
public boolean isColorTransparencySelectionEnabled();
public static Color showDialog(Component component, String title, Color initialColor,
    boolean colorTransparencySelectionEnabled);