Java 7 JColorChooser: Disable Transparency Slider

后端 未结 5 937
说谎
说谎 2020-12-19 04:35

JDK 7 added a new transparency slider to the JColorChooser:

\"enter

The proble

5条回答
  •  轮回少年
    2020-12-19 05:13

    Here is a slight modification of aymeric's answer that hides them altogether instead of disabling.

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

提交回复
热议问题