JComboBox自定義單元格渲染器-如何獲取組合的isEnabled()狀態?

我基于DefaultListCellRenderer(我的一個基本上顯示了一種顏色),為jComboxes創建了一個自定義單元渲染器。到目前為止,它工作得很好。

唯一的問題是,當JComboBox被禁用時:在這種情況下,我還應該以一種不那么閃亮的方式繪制顏色(以顯示它處于非活動狀態)。但是如何在ListCellRenderer中獲取狀態?

我嘗試了isEnabled()或component.isEnabled(),但這似乎不可訪問/給我組合的實際狀態。在DefaultListCellRenderer中,有一個對list.isEnabled()的查詢,但這對我來說沒有意義(而且也不起作用)。

Any ideas?

? 最佳回答:

有點棘手,但可能;)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.util.function.Predicate;

import javax.annotation.Nullable;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
 * <code>ComboTest</code>.
 */
public class ComboTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ComboTest()::startUp);
    }

    private void startUp() {
        JFrame frm = new JFrame("Combo test");
        JComboBox<String> combo = new JComboBox<>(new String[] {"One", "Two", "Three"});
        combo.setRenderer(new EnablementCellRenderer());
        JButton b = new JButton("Toggle Enabled");
        b.addActionListener(l -> {
            combo.setEnabled(!combo.isEnabled());
            combo.repaint();
        });
        frm.add(combo);
        frm.add(b, BorderLayout.EAST);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private class EnablementCellRenderer extends BasicComboBoxRenderer {

        @Override
        protected void paintComponent(Graphics g) {
            JComboBox<?> combo = getFirstAncestorOfClass(this, JComboBox.class);
            setForeground(combo.isEnabled() ? Color.BLUE : Color.GREEN);
            super.paintComponent(g);
        }
    }

    /**
     * Searches for the first ancestor of the given component which is the instance of the given class.
     *
     * @param aStart start component to search. If the component is instance of the class - it will be returned.
     * @param condition condition used to determine the component.
     * @return first ancestor of the given component which is the instance of the given class. Null if no such component found.
     */
    @Nullable
    public static Container getFirstAncestor(Component aStart, Predicate<Component> condition) {
        Container result = null;
        Component base = aStart;
        while ((result == null) && (base.getParent() != null || getInvoker(base) != null)) {
            base = getInvoker(base) == null ? base.getParent() : getInvoker(base);

            result = condition.test(base) ? (Container) base : null;
        }
        return result;
    }

    /**
     * Searches for the first ancestor of the given component which is the instance of the given class.
     *
     * @param aStart start component to search. If the component is instance of the class - it will be returned.
     * @param aClass class of component.
     * @return first ancestor of the given component which is the instance of the given class. Null if no such component found.
     * @param <E> class of component.
     */
    @Nullable
    public static <E> E getFirstAncestorOfClass(Component aStart, Class<E> aClass) {
        return aClass.cast(getFirstAncestor(aStart, aClass::isInstance));
    }

    /**
     * Gets the invoker of the given component when it's a pop-up menu.
     *
     * @param c component which invoker must be found.
     * @return the invoker when the given component is a pop-up menu or null otherwise.
     */
    private static Component getInvoker(Component c) {
        return c instanceof JPopupMenu ? ((JPopupMenu) c).getInvoker() : null;
    }

}
主站蜘蛛池模板: 亚洲一区二区三区无码影院| 国产精品视频一区二区三区经| 日本片免费观看一区二区| 国产成人综合亚洲一区| 老熟女五十路乱子交尾中出一区| 丰满人妻一区二区三区视频| 一区二区三区在线|欧| 亚洲av区一区二区三| 精品国产一区二区三区香蕉事| 国产乱码一区二区三区爽爽爽| 日韩精品无码久久一区二区三| 国产麻豆精品一区二区三区| 亚洲一区欧洲一区| 国内精品一区二区三区东京| 亚洲国产情侣一区二区三区| 精品无人区一区二区三区 | 国产一区二区三区在线观看免费| 中文字幕在线精品视频入口一区| 中文乱码精品一区二区三区| 国产美女口爆吞精一区二区| 午夜无码一区二区三区在线观看| 国产SUV精品一区二区88L | 国产综合一区二区在线观看| 一区二区三区www| 国产亚洲欧洲Aⅴ综合一区| 久久国产精品免费一区| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 97久久精品午夜一区二区| 国产亚洲欧洲Aⅴ综合一区| 亚洲一区二区三区影院| 久久国产视频一区| 国产精品亚洲午夜一区二区三区 | 国产婷婷一区二区三区| 国产在线一区二区在线视频| 亚洲av综合av一区| 亚洲欧洲无码一区二区三区| 国产在线一区二区视频| 国产午夜精品一区二区| 日韩精品中文字幕无码一区| 亚洲日本va一区二区三区| 国产福利一区二区|