定义单元格更改时的Java jTable颜色行

问题描述 投票:0回答:1

大家好,我没有发现下面的代码有什么不对。它填充表和标题就好了。但是,当我更改第1行时,colum 1进行测试并单击它不会像我期望的那样为行着色。然后点击我点击的那个变成绿色,然后我点击桌子上的任何地方只是改变绿色。

该列不会从1(公司)更改,因为这将是将进行更改的默认列。该行是此处唯一的动态数字。

要测试的流程:

  • 双击IBM。
  • 输入测试。
  • 单击任何其他单元格以保存该单元格值。
  • 行没有改变(第1行)。
  • 再次单击测试单元格。
  • 所有行都将变为绿色。

预期流程:

  • 双击IBM。
  • 输入测试。
  • 单击任何其他单元格以保存该单元格值。
  • 更改为测试的单元格变为行绿色。
  • 单击“单元格的共享”(3,3)。
  • 双击并将4000更改为1000。
  • 单击任何其他单元格以保存该单元格值。
  • 更改为1000的单元格将该行更改为红色。

填充表格和标题:

enter image description here

单击并更改第1行,第1列值以进行测试:

enter image description here

编辑后单击该单元格到另一个单元格:

enter image description here

现在,单击任何其他单元格(注意测试行不是绿色应该是):

enter image description here

现在点击我编辑的单元格进行测试:

enter image description here

而且你看到上面它只是着色我点击的每一行,不管我说的逻辑

if(“test”.equals(type)){....

java代码:

@SuppressWarnings("serial")
public class TableRowRenderingTip extends JPanel {
    public TableRowRenderingTip() {
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data =
        {
            {"Buy", "IBM", new Integer(1000), new Double(80.5), Boolean.TRUE},
            {"Sell", "Dell", new Integer(2000), new Double(6.25), Boolean.FALSE},
            {"Short Sell", "Apple", new Integer(3000), new Double(7.35), Boolean.TRUE},
            {"Buy", "MicroSoft", new Integer(4000), new Double(27.50), Boolean.FALSE},
            {"Short Sell", "Cisco", new Integer(5000), new Double(20), Boolean.TRUE}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public Class getColumnClass(int column) {
                return getValueAt(1, column).getClass();
            }
        };

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Border", createBorder(model));
        add(tabbedPane);
    }

    private JComponent createBorder(DefaultTableModel model) {
        JTable table = new JTable(model) {
            private Border outside      = new MatteBorder(1, 0, 1, 0, Color.RED);
            private Border _outside     = new MatteBorder(1, 0, 1, 0, Color.GREEN);
            private Border inside       = new EmptyBorder(0, 1, 0, 1);
            private Border highlight    = new CompoundBorder(outside, inside);
            private Border _highlight   = new CompoundBorder(_outside, inside);

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                JComponent jc = (JComponent)c;
                String type = (String)getModel().getValueAt(convertRowIndexToModel(row), 1);

                if (isRowSelected(row)) {           
                    if ("test".equals(type)) {
                        jc.setBorder( _highlight ); // Green color
                        jc.setBackground(Color.GREEN);
                    } else {
                        jc.setBorder( highlight ); //Red color
                    }
                }

                return c;
            }
        };

        //table.setPreferredScrollableViewportSize(table.getPreferredSize());
        //table.changeSelection(0, 0, false, false);        
        return new JScrollPane( table );
    }

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

    public static void createAndShowGUI()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Table Row Rendering");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableRowRenderingTip() );
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

所以不用说我至少可以说有点沮丧,因为我已经花了几个小时研究这个问题,现在试图找出可能出现的问题。

我相信我会看到一些简单的东西......

java swing jtable renderer
1个回答
1
投票

如果测试条件不正确,您将忘记设置边框并突出显示为默认值。例如

if (isRowSelected(row)) {
    if ("test".equals(type)) {
        jc.setBorder(_highlight); // Green color
        jc.setBackground(Color.GREEN);
    } else {
        jc.setBorder(highlight); // Red color
    }
} else {
    jc.setBorder(null);
    jc.setBackground(null);
}
© www.soinside.com 2019 - 2024. All rights reserved.