保持JList选择未聚焦的单元格

时间:2016-11-05 02:04:27

标签: java swing jlist renderer

在我的自定义渲染器中,每个单元格都被选中,获得浅灰色背景颜色,但当这些单元格失去焦点时,所做的选择将会消失。我该如何防止这种情况?

package window;

import javax.swing.BorderFactory;
import javax.swing.ListCellRenderer;

import main.Trade;

public class TradeRenderer extends JPanel implements ListCellRenderer<Object>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus){


        if(isSelected)
            this.setBackground(Color.LIGHT_GRAY);
        else
            this.setBackground(Color.WHITE);

        this.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.LIGHT_GRAY));

        setEnabled(true);

        return this;
    }

}

0 个答案:

没有答案