2011-05-04 69 views
0

我的jtable加载了数据,这是我在jTable上调用我的弹出功能的地方。所以基本上,如果我右击一行,弹出式(信用检查)出现,如果我点击它是设置一个值到该行中的最后一个单元格。现在,基于这个列单元格值,我必须定义一行的颜色。比方说,如果单元格值失败,那么将该行从红色变为绿色。我已经尝试customCellRenderer并定义了我的条件,但行颜色没有变化。但是,自定义单元格渲染器非常适合我必须编写的按钮功能。下面的代码使用准备cellRenderer,我觉得很容易,但我没有看到行颜色的任何变化。基于列值的JTable行颜色更改 - 弹出单击

我错过了一些连接,PLZ提供给我帮助。

在此先感谢。

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; 
       } 
      }; 

     } 

    } 
} 
+0

编辑是怪诞的不知何故 - 并编辑忘记/重置东西?代码在预览中看起来完全没问题(在从我的IDE中完成一个副本之后),现在再次丢失了第一部分... – kleopatra 2011-05-04 09:14:33

+0

对不起,这是我的第一篇文章。 – aditya86c 2011-05-04 15:51:01

回答

2

一些格式化后(相信我,对于代码可读性是很重要的;-)好像你实例化你的弹出菜单中的新表,只说表有自定义呈现。你可以做什么,但对你的真实表格没有任何影响。

将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

+0

你告诉我的东西非常有意义。但是,如何在不编写Jtable中的内部类的情况下调用预先准备好的渲染器。你能告诉我一些样本链接还是一些结构。 – aditya86c 2011-05-04 20:15:39

+0

下面是我如何调用我的弹出窗口...... jTable.addMouseListener(new TablePopupListener(jTable));它位于不同的java文件中,名为FileChooser.java ............ ........所以调用进入TablePopupListener类。TablePopupListener类的代码在第一篇文章中给出。 – aditya86c 2011-05-05 19:58:53

+0

没有调用table = new JTable(model){// prepareRenderer logic}我无法调用prepareRenderer。我在TablePopupListener类的actionPerformed()方法中编写了此逻辑。 – aditya86c 2011-05-05 20:01:44