2011-05-04 52 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