2

我一直对JLists他们的渲染器有问题。我有Result一个有字段的类:private double sum; 我还创建了一个JList包含Result类的实例:

model = new DefaultListModel<Result>();
list = new JList<>(model);

我想为列表中满足此声明的那些元素设置前景或背景(无论哪个)为红色:result.sum > 10.

我试图编写一个扩展的类,ListCellRenderer但它以不值得一提的灾难告终。请帮忙。

import java.awt.Component;

import javax.swing.JList;
import javax.swing.ListCellRenderer;

    public class MyCellRenderer implements ListCellRenderer<Result> {

        @Override
        public Component getListCellRendererComponent(JList<? extends Result> arg0, Result arg1, int arg2, boolean arg3, boolean arg4) {
            if(result.getSuma() > 10)
                setForeground(Color.red);
            return arg0;
        }        
    }
4

2 回答 2

5

我建议您为您的 porpuses使用DefaultListCellRenderer并覆盖它的方法,在您的自定义中返回。我给你一个渲染器的例子,为你的 porpuses 修改它:getListCellRendererComponentsuper.getListCellRendererComponent()String

private static ListCellRenderer<? super String> getCellRenderer() {
    return new DefaultListCellRenderer(){
        @Override
        public Component getListCellRendererComponent(JList<?> list,Object value, int index, boolean isSelected,boolean cellHasFocus) {
            Component listCellRendererComponent = super.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus);
            if(value.toString().length()>1){
                listCellRendererComponent.setBackground(Color.RED);
            } else {
                listCellRendererComponent.setBackground(list.getBackground());
            }
            return listCellRendererComponent;
        }
    };
}

此方法设置文本的背景颜色,长度大于 1。

在此处输入图像描述

于 2013-11-24T01:23:24.907 回答
4

建议:

  • 让您的渲染器扩展 DefaultListCellRenderer。
  • 覆盖getListCellRendererComponent方法。
  • super.getListCellRendererComponent(...)进行更改后返回。

例如,在我的一个项目中,我有:

@SuppressWarnings("serial")
class MyLabPanelListCellRenderer extends DefaultListCellRenderer {
   @Override
   public Component getListCellRendererComponent(JList list, Object value,
         int index, boolean isSelected, boolean cellHasFocus) {
      if (value == null) {
         value = "";
      } else {
         value = ((LabPanel) value).getLabPanelDisplayName();
      }
      return super.getListCellRendererComponent(list, value, index, isSelected,
            cellHasFocus);
   }
}

或者更完整的例子:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestListRenderer extends JPanel {
   private static final int TOTAL_ITEMS = 100;
   private static final int MAX_VALUE = 15;
   private static final int VISIBLE_ROW_COUNT = 15;
   private DefaultListModel<Integer> listModel = new DefaultListModel<>();
   private JList<Integer> myList = new JList<>(listModel);
   private Random random = new Random();

   public TestListRenderer() {
      for (int i = 0; i < TOTAL_ITEMS; i++) {
         listModel.addElement(random.nextInt(MAX_VALUE));
      }

      myList.setCellRenderer(new MyListCellRenderer());

      myList.setVisibleRowCount(VISIBLE_ROW_COUNT);
      add(new JScrollPane(myList));
   }

   private class MyListCellRenderer extends DefaultListCellRenderer {
      private static final int PREF_W = 50;
      private static final int MAX_INT_VALUE = 10;
      private final Color HIGH_VALUE_FG = Color.white;
      private final Color HIGH_VALUE_BG = Color.red;

      @Override
      public Dimension getPreferredSize() {
         Dimension superSize = super.getPreferredSize();
         return new Dimension(PREF_W, superSize.height);
      }

      @Override
      public Component getListCellRendererComponent(JList<?> list,
            Object value, int index, boolean isSelected, boolean cellHasFocus) {
         Component superRenderer = super.getListCellRendererComponent(list, value, index, isSelected,
               cellHasFocus);

         setBackground(null);
         setForeground(null);
         if (value != null && ((Integer) value).intValue() > MAX_INT_VALUE) {
            setBackground(HIGH_VALUE_BG);
            setForeground(HIGH_VALUE_FG);
         }

         return superRenderer;
      }
   }

   private static void createAndShowGui() {
      TestListRenderer mainPanel = new TestListRenderer();

      JFrame frame = new JFrame("TestListRenderer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

如果 > 10,它将在具有红色背景的 JList 中显示数字。

在此处输入图像描述

于 2013-11-24T01:14:37.353 回答