0

jTable加载了数据,这就是我调用弹出功能的地方jTable

jTable.addMouseListener(new TablePopupListener(jTable));
displayTable();

所以基本上,如果我右键单击一行,就会出现一个弹出窗口(信用检查),如果我单击它,则会为该行中的最后一个单元格设置一个值。现在,基于此列单元格值,我必须定义行的颜色。假设如果单元格值失败,则将该行变为红色,否则变为绿色。我已经尝试customCellRenderer并定义了我的情况,但行颜色没有变化。不过,自定义单元格渲染器非常适合我必须编写的按钮功能。下面的代码使用cellRenderer了我觉得很容易的准备,但我没有看到行颜色有任何变化。

我缺少一些联系,请为我提供帮助。

提前致谢。

    class TablePopupListener extends MouseAdapter implements ActionListener { 
        JPopupMenu popup; 
        JTable table; 
        int[] selRows; 
        TableModel model; 
        ArrayList rowValueList = new ArrayList(); 
        JMenuItem creditCheck = new JMenuItem("Credit Check");

        public TablePopupListener(JTable jTable) {
            this.table = jTable;
            model = table.getModel();
            popup = new JPopupMenu();
            JMenuItem creditCheck = new JMenuItem("Credit Check");
            creditCheck.addActionListener(this);
            popup.add(creditCheck);

        }

        public void mousePressed(MouseEvent me) {
           firePopup(me);
        }

        public void mouseReleased(MouseEvent me) {
           firePopup(me);
        }

        public void firePopup(MouseEvent me) {

            /*
             * The popup menu will be shown only if there is a row selection in the
             * table
             */

            // popup.show(table, me.getX(), me.getY());
            if (me.isPopupTrigger() && table.getModel().getRowCount() != 0
               && table.getSelectedRow() != -1) {
              // popup.show(table,me.getX(),me.getY());
              if (me.isPopupTrigger()) {
                   JTable source = (JTable) me.getSource();
                   int row = source.rowAtPoint(me.getPoint());
                   int column = source.columnAtPoint(me.getPoint());

                   if (!source.isRowSelected(row))
                      source.changeSelection(row, column, false, false);

                   popup.show(table, me.getX(), me.getY());

              }
            }
         }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("Credit Check")) {
            System.out.println("you have clicked creditCheckpopup");
            selRows = table.getSelectedRows();
            if (selRows.length > 0) {
                for (int i = 0; i < selRows.length; i++) {
                  // get Table data
                    for (int j = 1; j < (table.getColumnCount()) - 1; j++) {
                        rowValueList.add(model.getValueAt(selRows[i], j));
                    }
                    System.out.println("Selection : " + rowValueList);
                }
            } else {
                System.out.println("you have clicked something idiot");
            }

            int result = new COpxDeal(rowValueList).CheckCredit();
            if (result == 1)
                rowValueList.add("pass");
            else
                rowValueList.add("fail");
            String aValue = (String) rowValueList.get(14);
            for (int i = 0; i < selRows.length; i++) {
                model.setValueAt(aValue, selRows[i], 15);
            }

     // inserted comment (Kleopatra): where are we? that's outside of the TablePopup?
     // okay, nothing like copying the code into an IDE and let that do the formatting, silly me ;-)
     // this is indeed _inside_ the popup, that is the table is recreated
            table = new JTable(model) {
                public Component prepareRenderer(TableCellRenderer renderer,
                        int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    JComponent jc = (JComponent) c;

                    // if (!isRowSelected(row)){
                    // c.setBackground(getBackground());
                    // System.out.println(isRowSelected(row));
                    // }
                    int modelRow = convertRowIndexToModel(row);
                    String strTestValue = "fail";
                    String strTblValue = (String) getModel().getValueAt(
                            modelRow, 15);
                    System.out.println("result :" + strTblValue);
                    if (strTblValue == null || strTblValue.equals(""))
                        System.out.println("there is nothing in strTblValue");
                    else if (strTestValue.equals(strTblValue)) {
                        jc.setBackground(Color.RED);
                    } else {
                        jc.setBackground(Color.green);
                    }

                    return c;
                }
            };

        }

    }
}
4

1 回答 1

2

经过一些格式化后(相信我,代码的可读性很重要;-) 似乎您在 popupMenu 中实例化了一个新表,并且只有该表具有自定义渲染器。您可以这样做,但对您的真实桌子没有任何影响。

将 prepareRenderer 移动到您的真实表中(作为参数传递到弹出窗口的表),您应该会看到颜色。注意:由于 DefaultTableCellRenderer 中的错误,您必须始终设置颜色,即

 if (nothingToDo) {
     setBackground(normal)
 } else if ... {
     setBackground(one)
 } else {
     setBackground(other)
 }

编辑:试图解释代码结构的变化,伪代码片段

当前状态,这就是您正在做的事情:

JTable table = new JTable();
table.addMouseListener(new TablePopupListener(table));
     // keep listener-local reference to table
    JTable table = table;
    ....
    // in the listener guts, the reference is replaced
    table = new JTable() {
         @Override
         Component prepareRenderer(... 
    }

更改为,这就是你应该做的:

 JTable table = new JTable() {
      @Override
      Component prepareRenderer(...
 };
 table.addMouseListener(new TablePopupListener(table));
     // keep listener-local reference to table
     JTable table = table;
     // don't replace ever, it's for reading only

编辑2:-更改伪代码以实际注册侦听器)-在 addMouseListener 下方缩进的代码表示 TablePopupListener内代码的轮廓

于 2011-05-04T09:03:06.823 回答