Java 7 JColorChooser: Disable Transparency Slider

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

JDK 7 added a new transparency slider to the JColorChooser:

\"enter

The proble

5条回答
  •  自闭症患者
    2020-12-19 05:28

    According to the documentation, it's possible to just modify/configure the existing classes. So recommended way to go is therefore to create your own ChooserPanels (they need to extend AbstractColorChooserPanel) and then invoke

    JColorChooser jc = new JColorChooser();
    jc.setChooserPanels(new AbstractColorChooserPanel[]{yourChooserPanel});
    

    Alternatively, if yor aree looking for a faster/nastier/uglier way to do it, wrote this for you:

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

    Good luck with it :)

提交回复
热议问题