简体   繁体   English

在getListCellRendererComponent中双击时更改颜色

[英]Changing color when double clicked in the getListCellRendererComponent

How can I change color when I double click the cell of JList ? 当我双击JList的单元格时,如何更改颜色? Is there a way adding a MouseListener within DefaultListCellRenderer ? 有没有办法在DefaultListCellRenderer添加MouseListener

public class Renderer extends DefaultListCellRenderer{

 @Override
    public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) 
    {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if(isSelected){
            c.setBackground(Color.ORANGE);
            c.setForeground(Color.BLACK);
        }
        return c;
    }
}

I has a similar situation recently where I wanted to double click table entries and have them added in a list. 我最近有类似的情况,我想双击表条目并将它们添加到列表中。 The trick was to: 诀窍是:

  • Add a boolean in the table to denote 'has been added to list'. 在表中添加一个布尔值来表示“已添加到列表中”。
  • On double click (add the row to list &) set that value to true. 双击(将行添加到列表&)将该值设置为true。
  • When rendering, check the boolean and decide how to render it. 渲染时,检查布尔值并决定如何渲染它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM