简体   繁体   English

如何仍然查看 JTable 中选定行的单元格颜色?

[英]How to still view Cell color of selected row in a JTable?

Lets say a JTable has rows with various cell colors like this.假设一个 JTable 具有像这样的各种单元格颜色的行。

在此处输入图像描述

If the first 2 rows are highlighted, it looks like the following and now the colors seen for the first two rows are lost.如果前 2 行突出显示,则如下所示,现在前两行看到的颜色丢失了。

在此处输入图像描述

Is it possible to make the highlighted rows more transparent so that the underlying colors are still visible?是否可以使突出显示的行更加透明,以便底层颜色仍然可见? Something similar to what Excel does.类似于 Excel 所做的事情。 Notice the same 2 rows are highlighted and yet the colors are still visible while its still obvious the rows are highlighted/selected.请注意,相同的 2 行被突出显示,但颜色仍然可见,而行被突出显示/选择仍然很明显。

在此处输入图像描述

Here is a modified sample I pulled from the Java2 page and modified it for the above example.这是我从 Java2 页面中提取的修改后的示例,并针对上述示例进行了修改。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

class EvenOddRenderer implements TableCellRenderer 
{

  public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) 
  {
      Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(table, value,
      isSelected, hasFocus, row, column);
      Color foreground = Color.red, background = Color.WHITE;
      
      if (column == 1 && row == 0) 
      {
          background = Color.GREEN;
      }
      else if (column == 1 && row == 1) 
      {
          background = Color.CYAN;
      }
      else if (column == 1 && row == 2) 
      {
          background = Color.ORANGE;
      }
      else if (column == 2)
      {
          background = Color.YELLOW;
      }

      if (isSelected)
          background = Color.LIGHT_GRAY;

    renderer.setForeground(foreground);
    renderer.setBackground(background);
    return renderer;
  }
}
public class CellColorTransparency {
  public static void main(String args[]) {

    final Object rowData[][] = { 
        { "1", "one",  "I" },
        { "2", "two",  "II" },
        { "3", "three", "III" }};
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    table.setDefaultRenderer(Object.class,new EvenOddRenderer());    
    
    JFrame frame = new JFrame("Color Test Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

  }
}

It was actually easier than expected.这实际上比预期的要容易。

Simply use the darker() call within the Renderer.只需在渲染器中使用 darker() 调用。

      if (isSelected)
      background = background.darker();//Color.LIGHT_GRAY;

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

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