简体   繁体   中英

Swing JList SetCellRenderer BackGround Color Not Working

Good morning:

I have a JList in Swing with some data. 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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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