简体   繁体   English

Swing JList SetCellRenderer BackGround颜色不起作用

[英]Swing JList SetCellRenderer BackGround Color Not Working

Good morning: 早上好:

I have a JList in Swing with some data. 我在Swing中有一些数据的JList。 I select the data from the DataBase and I am trying that the data that matches same in the JList be selected and in another colour, but I have tried all posibilities I could and gave me a lot of error. 我从数据库中选择了数据,我试图选择与JList中相同的数据并使用另一种颜色,但是我尝试了所有可能的方法并给了我很多错误。 My code. 我的代码。

This array saves the tags i would like to highlight. 该数组保存我要突出显示的标签。

 final String[] segmentacion2 = suscriptor.getSegmentacion2().split(";");

This for is to pre-select the tags. 这是为了预先选择标签。

 for (int j = 0; j < segmentacion2.length; j++)
     {
         listaSegmentacion2.setSelectedValue(segmentacion2[j], true);
     }


     listaSegmentacion2.setCellRenderer(new DefaultListCellRenderer() {

         @Override
         public Component getListCellRendererComponent(JList list,
                 Object value, int index, boolean isSelected,
                 boolean cellHasFocus) {

             super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
         for (int j = 0; j < segmentacion2.length; j++)
             {
                 listaSegmentacion2.setSelectedValue(segmentacion2[j], true);
             }

             System.out.println(isSelected);
             if(isSelected)
             {
                 setBackground(Color.green);
             }
             else
             {
                 setBackground(null);
             }
           return this;
         }
     });

And the error I got is: 我得到的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at javax.swing.UIDefaults.getFromHashtable(Unknown Source)
at javax.swing.UIDefaults.get(Unknown Source)
at javax.swing.MultiUIDefaults.get(Unknown Source)
at javax.swing.UIManager.get(Unknown Source)
at sun.swing.DefaultLookup.get(Unknown Source)
at sun.swing.DefaultLookup.getBorder(Unknown Source)
at sun.swing.DefaultLookup.getBorder(Unknown Source)
at javax.swing.DefaultListCellRenderer.getNoFocusBorder(Unknown Source)
at javax.swing.DefaultListCellRenderer.getListCellRendererComponent(Unknown Source)
at com.mypackage.main.Principal$2.getListCellRendererComponent(Principal.java:509)
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source)
at javax.swing.JList.setSelectedIndex(Unknown Source)
at javax.swing.JList.setSelectedValue(Unknown Source)

Any suggestions/ help? 有什么建议/帮助吗? Would be apreciated. 会很感激。 Thanks. 谢谢。

This array saves the tags i would like to highlight. 该数组保存我要突出显示的标签。

Then you need to compare the data in the Array with the data being rendered. 然后,您需要将数组中的数据与要呈现的数据进行比较。

The easiest way to do this is to copy the data from the Array to a Set: 最简单的方法是将数据从Array复制到Set:

HashSet<String> values = new HashSet<String>();

for (String value: segmentation2)
    values.add( value );

Now in the renderer the basic logic would be: 现在,在渲染器中,基本逻辑将是:

if (!isSelected)
    if (values.contains(value.toString())
        setBackground( Color.GREEN );
    else
        setBackground( null );

The idea is the highlighting is based on the value, not on the selection. 这个想法是突出显示是基于值,而不是选择。

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

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