在选择时突出显示发际线交叉模式中的相关单元格

时间:2018-05-16 15:44:34

标签: java swing jtable highlight jxtable

我想在JXTable添加细线十字突出显示。但我可以突出显示当前行或当前列;从来没有。

我目前拥有的是红色部分,黄色部分是我想要添加的部分。

enter image description here

在SSCCE 1 之下。

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

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

import org.jdesktop.swingx.JXTable;

public class Application
{

  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        JFrame frame = new JFrame();
        JXTable table = new JXTable(new CustomTableModel());

        boolean highlightRow = false;
        table.setRowSelectionAllowed(highlightRow);
        table.setColumnSelectionAllowed(!highlightRow);

        table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());

        frame.add(new JScrollPane(table));
        frame.setVisible(true);
        frame.pack();
      }
    });
  }

  public static class CustomTableCellRenderer extends DefaultTableCellRenderer
  {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
      Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
      if (row == table.getSelectedRow()) { component.setBackground(Color.YELLOW); }
      if (column == table.getSelectedColumn()) { component.setBackground(Color.RED); }
      return component;
    }
  }

  static class CustomTableModel extends AbstractTableModel
  {
    private final List<Object[]> data = new ArrayList<>();

    public CustomTableModel()
    {
      data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
      data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
      data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
      data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
    }

    @Override
    public int getRowCount() { return data.size(); }

    @Override
    public int getColumnCount() { return 10; }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex)[columnIndex]; }
  }

}

更新

使用@camickr提供的代码和建议可以得到:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import org.jdesktop.swingx.JXTable;

public class Application
{

  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        JFrame frame = new JFrame();

        DefaultTableModel model = new DefaultTableModel(5, 10);
        JXTable table = new JXTable(model)
        {
            @Override
            public Component prepareRenderer(
                TableCellRenderer renderer, int row, int column)
            {
                Component c = super.prepareRenderer(renderer, row, column);

                if (column == getSelectedColumn()) { c.setBackground(Color.RED); }
                else if (row == getSelectedRow()) { c.setBackground(Color.YELLOW); }
                else c.setBackground(getBackground());

                return c;
            }
        };

        frame.add(new JScrollPane(table));
        frame.setVisible(true);
        frame.pack();
      }
    });
  }

}

但不幸的是我现在有文物问题。类似于下面的图片(取自仅用红色着色的版本)。

enter image description here

我在Windows 10.0.16299 Build 16299上使用Java 1.8.0_74。JXTable来自

        <dependency>
            <groupId>org.swinglabs.swingx</groupId>
            <artifactId>swingx-all</artifactId>
            <version>1.6.5-1</version>
        </dependency>

1 个答案:

答案 0 :(得分:1)

使用JTable时似乎有效(尽管如果希望列具有优先级,则需要更改if语句的顺序)。所以似乎JXTable做了一些导致问题的额外渲染。

所以这是基于Table Row Rendering的替代方法,似乎适用于JXTable:

JXTable table = new JXTable(new CustomTableModel())
{
    public Component prepareRenderer(
        TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        if (column == getSelectedColumn()) { c.setBackground(Color.RED); }
        else if (row == getSelectedRow()) { c.setBackground(Color.YELLOW); }
        else c.setBackground(getBackground());

        return c;
    }
};

上面的代码替换了自定义渲染器:

//table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());

注意:在你的&#34; MCVE&#34;我的建议你删除了:

boolean highlightRow = false;
table.setRowSelectionAllowed(highlightRow);
table.setColumnSelectionAllowed(!highlightRow);

为什么呢?我只给了你上面两个改动。

这就是为什么你需要发布一个合适的&#34; MCVE&#34;所以我们可以确定你正确地实施了这个建议。