Custom ListCellRenderer will not change background color

*爱你&永不变心* 提交于 2020-01-21 05:17:08

问题


I have this class:

 @SuppressWarnings("serial")
  private class DataCellRenderer extends JLabel implements ListCellRenderer 
  {
    public DataCellRenderer()
    {
      setHorizontalAlignment(SwingConstants.RIGHT); 
    }

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

      if(isSelected)
        setBackground(Color.red);

      setText("  " + value.toString());

      return this;
    }
  }

The problem is that my Background will not turn red when I select a cell in my JList. The setText part works but I can't figure out why it will not change my background color of the cell. Anyone have any ideas, Thanks!


回答1:


The main problem is that labels are non-opaque by default, so you need to make the label opaque in order for the background to be painted.

But you don't need to create a custom renderer for this. The default renderer is opaque. All you need to do is set the selection background property of the list:

list.setSelectionBackground(Color.RED);

If you are trying to create a renderer to right align the text then you can just set a property on the default renderer:

DefaultListCellRenderer renderer = (DefaultListCellRenderer)list.getCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.RIGHT);



回答2:


for example

import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

public class FilesInTheJList {

    private static final int COLUMNS = 5;
    private Dimension size;

    public FilesInTheJList() {
        final JList list = new JList(new File("C:\\").listFiles()) {

            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredScrollableViewportSize() {
                if (size != null) {
                    return new Dimension(size);
                }
                return super.getPreferredScrollableViewportSize();
            }
        };
        list.setFixedCellHeight(50);
        list.setFixedCellWidth(150);
        size = list.getPreferredScrollableViewportSize();
        size.width *= COLUMNS;
        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list.setCellRenderer(new MyCellRenderer());
        list.setVisibleRowCount(0);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

        JFrame f = new JFrame("Files In the JList");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(list));
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FilesInTheJList fITJL = new FilesInTheJList();
            }
        });
    }

    private static class MyCellRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof File) {
                File file = (File) value;
                setText(file.getName());
                setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
                if (isSelected) {
                    setBackground(Color.red);
                    setForeground(Color.blue);
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
                //setPreferredSize(new Dimension(250, 25));
                //setEnabled(list.isEnabled());
                setFont(list.getFont());
                setOpaque(true);
            }
            return this;
        }
    }
}



回答3:


By default JLabel is transparent. If you want it to show a background, you need:

label.setOpaque(true);

Also, you often want to use the default background colour of the parent list, which will fit with the UI theme or customisation of the list itself:

if (isSelected) {
    label.setForeground(list.getSelectionForeground());
    label.setBackground(list.getSelectionBackground());
} else {
    label.setForeground(list.getForeground());
    label.setBackground(list.getBackground());
}

Putting it all together:

import javax.swing.*;
import java.awt.*;

public class CustomListCellRenderer implements ListCellRenderer<MyObject>
{
    @Override
    public Component getListCellRendererComponent(JList<? extends MyObject> list,
                                                  MyObject value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus)
    {
        JLabel label = new JLabel();
        label.setOpaque(true);
        label.setText(value.getName());
        if (isSelected) {
            label.setForeground(list.getSelectionForeground());
            label.setBackground(list.getSelectionBackground());
        } else {
            label.setForeground(list.getForeground());
            label.setBackground(list.getBackground());
        }
        return label;
    }
}


来源:https://stackoverflow.com/questions/7881925/custom-listcellrenderer-will-not-change-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!