2012-02-17 266 views
3

我搜索了在jtable中添加按鈕的教程,並從http://tips4java.wordpress.com/2009/07/12/table-button-column/找到了一個類文件在哪裏設置按鈕的標籤?將按鈕添加到jtable

 [code] 
    private void createTable(){ 
     model = new DefaultTableModel(); 
     editorTable.setModel(model); 
     model.addColumn("COL1"); 
     model.addColumn("COL2"); 
     model.addColumn("ADD"); 
     model.addColumn("DELETE"); 
     model.addRow(new Object[]{"DATA1", "DATA2"}); 

     Action delete = new AbstractAction() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      editorTable = (JTable) e.getSource(); 
      int modelRow = Integer.valueOf(e.getActionCommand()); 
      ((DefaultTableModel) editorTable.getModel()).removeRow(modelRow); 
     } 
    }; 

     ButtonColumn bc = new ButtonColumn(editorTable, delete, 3); 
     bc.setMnemonic(KeyEvent.VK_D); 
    } 

    [/code] 
+0

看看這裏:http://stackoverflow.com/questions/1475543/how-to-add-button-in-a-row-of-jtable-in-swing- java – MByD 2012-02-17 01:21:29

+0

你檢查了關於JTable的oracle java教程:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html檢查Renderer和編輯器 – asela38 2012-02-17 01:23:39

+0

Binyamin,謝謝你的迴應,我實現了這個類,它工作正常,但我沒有找到在哪裏添加按鈕的標籤。 – FirmView 2012-02-17 01:28:18

回答

4

它在表格渲染器和編輯器中根據DefaultTableModel中的數據自動設置。例如,對於表編輯器,代碼:

public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) { 
    ... 
    editButton.setText(value.toString()); 
    editButton.setIcon(null); 
    ... 
} 

其中value是從你的表模型中的價值。詳情請參閱ButtonColumn.java

編輯:既然你要添加4列,你應該你行數據更改爲

model.addRow(new Object[]{"DATA1", "DATA2", "DATA3", "DELETE"}); 

才能看到的第4列的刪除按鈕。

+0

謝謝,卡卡,我看到了代碼,它是editorValue。如果我想將標籤的名稱刪除,應該在哪裏進行更改? – FirmView 2012-02-17 01:46:06

+1

+1瞭解博客條目:)由於博客說'你在TableModel中存儲文本(或圖標),並且ButtonColumn將呈現按鈕上的文本(或圖標)。因此,您將一個文本字符串添加到TableModel,就像您將其他文本字符串添加到模型一樣,只有渲染是不同的。 – camickr 2012-02-17 02:17:28

+0

我明白了。謝謝 – FirmView 2012-02-17 02:18:14

-1
MyClass myClass = new MyClass(); 
    jTable1.getColumnModel().getColumn(0).setCellEditor(myClass); 
    jTable1.getColumnModel().getColumn(0).setCellRenderer(myClass); 
class MyClass extends AbstractCellEditor implements TableCellEditor, TableCellRenderer 
{ 

    @Override 
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 
    { 
    JPanel panel=(JPanel)jTable1.getCellRenderer(row, column).getTableCellRendererComponent(table, value, isSelected, isSelected, row, column); 
    panel.setBackground(table.getSelectionBackground());  
    return panel; 
    } 

    @Override 
    public Object getCellEditorValue() 
    { 
     return null; 
    } 

    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    { 
     AbstractAction action = new AbstractAction() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       JOptionPane.showMessageDialog(rootPane,"Row :"+jTable1.getSelectedRow()+" "+ e.getActionCommand() + " clicked"); 
      } 
     }; 
     JButton button1 = new JButton(action); 
     JButton button2 = new JButton(action); 
     button1.setText("Button1"); 
     button2.setText("Button2"); 

     JPanel panel = new JPanel(); 
     panel.add(button1); 
     panel.add(button2); 
     panel.setBackground(table.getBackground()); 
     return panel; 
    } 
} 

}

+0

與此有關的多個問題,f.i. a)cellEditor無效實現:它_must_在終止時通知其監聽器b)始終創建新組件c)假定默認渲染器是JPanel類型d)... – kleopatra 2013-08-12 09:42:12