2013-11-04 85 views
0

我是Java Swing中的新成員。我有jtable,在第2列中,當右鍵單擊一個jpopmenu時顯示兩個選項(進行中/已解決),並且我想在單擊「已解決」選項時將單元格顏色修改爲綠色。我知道我需要一個單元渲染器。如何把它?jitem單擊後更改jtable單元格顏色

有我的源代碼:

public class logaff { 

static File font_file = new File("font/MYRIADPRO-REGULAR.ttf"); 
static JPopupMenu pm; 
static JMenuItem one = new JMenuItem(); 
static JMenuItem two = new JMenuItem(); 
static JTable table; 

public static void main(String[] args) throws FontFormatException, 
     IOException { 
    Font fontt = Font.createFont(Font.TRUETYPE_FONT, font_file); 
    Font sizedFont = fontt.deriveFont(Font.PLAIN, 15); 
    JPanel logaffich = new JPanel(); 
    logaffich.setBackground(Color.black); 
    logaffich.setLayout(null); 

    JPanel tableau = new JPanel(); 
    tableau.setLocation(5, 15); 
    tableau.setSize(790, 520); 
    tableau.setBackground(Color.white); 

    JButton a = new JButton("fg"); 
    String[] columnNames = { "ID", "Description", "Status", "Cam", 
      "Elapsed Time" }; 
    String[][] data = new String[10][5]; 

    for (int i = 0; i < data.length; i++) { 
     for (int j = 0; j < data[i].length; j++) { 
      data[i][j] = "Table " + i + ", " + j; 

     } 
    } 

    int[] columnsWidth = { 158, 450, 60, 40, 80 }; 

    table = new JTable(data, columnNames); 
    int k = 0; 
    for (int width : columnsWidth) { 
     TableColumn column = table.getColumnModel().getColumn(k++); 
     column.setMinWidth(width); 
     column.setMaxWidth(width); 
     column.setPreferredWidth(width); 
     table.setRowHeight(55); 
    } 

    JScrollPane scrollPane = new JScrollPane(table); 
    table.setFillsViewportHeight(true); 
    tableau.setLayout(new BorderLayout()); 
    tableau.add(scrollPane, BorderLayout.CENTER); 

    pm = new JPopupMenu(); 
    pm.add(one); 
    pm.add(two); 

    table.addMouseListener(new MouseAdapter() { 

     public void mouseReleased(MouseEvent me) { 
      try { 
       showPopup(me); 
      } catch (FontFormatException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

    logaffich.add(tableau); 
    TitledBorder title1; 

    title1 = BorderFactory.createTitledBorder(null, "New Log Alert", 
      TitledBorder.LEFT, TitledBorder.TOP, sizedFont, Color.white); 
    logaffich.setBorder(title1); 

    // panel pour le formulaire 
    JFrame frame = new JFrame(); 
    frame.setContentPane(logaffich); 
    // Create and set up the content pane. 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(808, 565); 
    frame.setResizable(false); 
    frame.setVisible(true); 

} 

private static void showPopup(MouseEvent me) throws FontFormatException, 
     IOException { 
    // is this event a popup trigger? 
    if (pm.isPopupTrigger(me)) { 
     Point p = me.getPoint(); 
     final int row = table.rowAtPoint(p); 
     final int col = table.columnAtPoint(p); 
     // if we've clicked on a row in the second column 
     if (row != -1 && col == 1) { 
      File font_file = new File("font/MYRIADPRO-REGULAR.ttf"); 

      Font fontt = Font.createFont(Font.TRUETYPE_FONT,font_file); 
      Font sizedFont = fontt.deriveFont(Font.BOLD, 17); 
      final ImageIcon progress = new ImageIcon("images/progress.png"); 
      one.setIcon(progress); 
      one.setFont(sizedFont); 
      final ImageIcon ok = new ImageIcon("images/ok.png"); 
      two.setIcon(ok); 
      two.setFont(sizedFont); 

      one.setText("In progress " + row + "."); 
      two.setText("Solved " + row + "."); 
      pm.show(table, p.x, p.y); 

     } 
     one.addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent me) { 
       System.out.println("sfsdf" + row); 

      } 

     }); 
    } 
} 
} 

回答

0

您需要了解Renderers文檔。正確使用它。

接下來的代碼添加自定義單元格渲染你的專欄:

TableColumn column = table.getColumnModel().getColumn(1); 
column.setCellRenderer(getRenderer()); 

示例代碼getRenderer方法:

private static TableCellRenderer getRenderer() { 
return new DefaultTableCellRenderer(){ 
    @Override 
    public Component getTableCellRendererComponent(JTable table, 
      Object value, boolean isSelected, boolean hasFocus, int row, 
      int column) { 
      boolean solved = false; 
      if (value.toString().endsWith("Solved")) { 
       solved = true; 
       value = value.toString().replace("Solved", ""); 
      } 
      Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column); 
      if (solved) { 
       tableCellRendererComponent.setBackground(Color.GREEN); 
      } else { 
       tableCellRendererComponent.setBackground(table.getBackground()); 
      } 
      return tableCellRendererComponent; 
    } 
}; 
} 

該方法返回DefaultTableCellRenderer。在這裏你可以在if-else聲明中確定單元格的背景。

編輯: 1)變更getRenderer方法。

2)建立在明年方式您彈出:

pm = new JPopupMenu(); 
pm.add(one); 
pm.add(two); 
one.addMouseListener(new MouseAdapter() { 
    public void mousePressed(MouseEvent me) { 
     if(row != -1) 
     System.out.println("sfsdf" + row); 

    } 

}); 

two.addMouseListener(new MouseAdapter() { 
    public void mousePressed(MouseEvent me) { 
     if(row != -1){ 
      Object valueAt = table.getValueAt(row,1); 
      table.setValueAt(valueAt+"Solved", row,1); 
      ((DefaultTableModel)table.getModel()).fireTableDataChanged(); 
     } 
    } 

}); 

,因爲你每次調用showPopup方法,而不是你一定要一次添加它作爲在我的例子時添加監聽器。 row字段我設置爲靜態字段。

3)你的方法showPopup改變:

private static void showPopup(MouseEvent me) throws FontFormatException, 
    IOException { 

// is this event a popup trigger? 
if (pm.isPopupTrigger(me)) { 
    Point p = me.getPoint(); 
    row = table.rowAtPoint(p); 
    final int col = table.columnAtPoint(p); 
    // if we've clicked on a row in the second column 
    if (row != -1 && col == 1) { 
     final ImageIcon progress = new ImageIcon("images/progress.png"); 
     one.setIcon(progress); 
     final ImageIcon ok = new ImageIcon("images/ok.png"); 
     two.setIcon(ok); 

     one.setText("In progress " + row + "."); 
     two.setText("Solved " + row + "."); 
     pm.show(table, p.x, p.y); 

    } 

} 
} 

4)創建您的餐桌旁路table = new JTable(new DefaultTableModel(data, columnNames));

如果你改變你的代碼,我recomended,當你推解決在彈出的然後細胞會與綠色背景。

+0

我想單擊選項「已解決」時將單元格顏色修改爲綠色。 == [TableCellRenderer和如何刷新單元格背景,而不使用JTable.repaint](http://stackoverflow.com/questions/16814512/tablecellrenderer-and-how-to-refresh-cell-background-without-using-jtable-repain ) – mKorbel

+0

非常感謝你把它添加上beut顏色的單元「解決」工作不 –

+0

如果您提交的所有變化將是工作 – alex2410