编程语言
首页 > 编程语言> > java-Swing ListCellRenderer中的背景颜色错误

java-Swing ListCellRenderer中的背景颜色错误

作者:互联网

我目前正在尝试为JList编写自定义ListCellRenderer.不幸的是,几乎所有示例都仅将DefaultListCellRenderer用作JLabel并完成此操作.但是,我需要一个JPanel(因为我需要显示的信息不仅仅是图标和一行文本).

现在我的背景色有问题,尤其是Nimbus PLAF.似乎我从list.getBackground()获得的背景颜色是白色,但被绘制为灰色阴影(或蓝灰色).输出我得到的颜色会产生以下结果:

Background color: DerivedColor(color=255,255,255 parent=nimbusLightBackground offsets=0.0,0.0,0.0,0 pColor=255,255,255

但是,可以看出,这不是绘画的内容.

alt text http://hypftier.de/dump/jlist_renderer_background.png

对于选定的项目,它显然可以正常工作.目前,我什至将我放入JPanel的每个组件的单元格渲染器返回的颜色都设置为不透明,并且具有正确的前景色和背景色-无济于事.

有任何想法我在这里做错了吗?

ETA:有望运行的示例代码.

public class ParameterListCellRenderer implements ListCellRenderer {
  @Override
  public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
    // some values we need
    Border border = null;
    Color foreground, background;
    if (isSelected) {
      background = list.getSelectionBackground();
      foreground = list.getSelectionForeground();
    } else {
      background = list.getBackground();
      foreground = list.getForeground();
    }
    if (cellHasFocus) {
      if (isSelected) {
        border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
      }
      if (border == null) {
        border = UIManager.getBorder("List.focusCellHighlightBorder");
      }
    } else {
      border = UIManager.getBorder("List.cellNoFocusBorder");
    }

    System.out.println("Background color: " + background.toString());

    JPanel outerPanel = new JPanel(new BorderLayout());
    setProperties(outerPanel, foreground, background);
    outerPanel.setBorder(border);

    JLabel nameLabel = new JLabel("Factory name here");
    setProperties(nameLabel, foreground, background);
    outerPanel.add(nameLabel, BorderLayout.PAGE_START);

    Box innerPanel = new Box(BoxLayout.PAGE_AXIS);
    setProperties(innerPanel, foreground, background);
    innerPanel.setAlignmentX(Box.LEFT_ALIGNMENT);
    innerPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));

    JLabel label = new JLabel("param: value");

    label.setFont(label.getFont().deriveFont(
        AffineTransform.getScaleInstance(0.95, 0.95)));
    setProperties(label, foreground, background);

    innerPanel.add(label);

    outerPanel.add(innerPanel, BorderLayout.CENTER);

    return outerPanel;
  }

  private void setProperties(JComponent component, Color foreground,
      Color background) {
    component.setOpaque(true);
    component.setForeground(foreground);
    component.setBackground(background);
  }
}

奇怪的是,如果我这样做

if (isSelected) {
  background = new Color(list.getSelectionBackground().getRGB());
  foreground = new Color(list.getSelectionForeground().getRGB());
} else {
  background = new Color(list.getBackground().getRGB());
  foreground = new Color(list.getForeground().getRGB());
}

它神奇地工作.所以也许我到达那里的带nimbusLightBackground的DerivedColor可能有麻烦?

解决方法:

JPanels默认为opaque = true,而jLabels默认为opaque = false.

因此,当DefaultCellRender使用JLabel时,它将获取其父级的背景.

尝试在面板上将opaque设置为false.

更新:

Nimbus使用其自己的自定义ListCellRenderer.我发现有2处解决此问题的方法,one here on SO涉及Google’s code repository(请参阅UpdateUI和NimbusCellRenderer).

标签:look-and-feel,swing,nimbus,listcellrenderer,java
来源: https://codeday.me/bug/20191210/2099048.html