2010-07-12 72 views
0

我有一張表格作爲JButton。如何從表格中刪除Jbutton

我設置渲染器如下

TableColumn col = colModel.getColumn(3); 
    col.setCellRenderer(new MyRenderer("Del")); 
    col.setCellEditor(new MultiTradeCellEditor(new JCheckBox())); 

渲染器和cellEditor的類是

class MyRenderer extends JButton implements TableCellRenderer{ 

    public MyRenderer(String text){ 
     super(text); 
    } 
    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) { 
     return this; 
    } 

} 


} 

CellEditor中類

class MultiTradeCellEditor extends DefaultCellEditor{ 

    protected JButton button; 

    public MultiTradeCellEditor(JCheckBox checkBox) { 
     super(checkBox); 
     button = new JButton("Del"); 
     button.setOpaque(true); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) {  
      selectionList.getList().remove(table.getSelectedRow()); 
       table.repaint(); 
           } 
     }); 

    } 

} 

當我從表中刪除該行。我做model.remove(table.getSelectedRow())。它刪除除JButton之外的行。我假設這個按鈕是Renderer組件的一部分,所以它不會被刪除。 我該怎麼做?

回答

0

奇數。 也許是緩存的東西?

嘗試返回空標籤時,沒有值?

class MyRenderer extends JComponent implements TableCellRenderer{ 
    private String text; 
    public MyRenderer(String text){ 
     this.text = text; 
    } 
    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) { 
     if (value) 
      return new JButton(text); 
     else 
      return new JLabel(); 
    } 

} 


} 
+0

是的,但是緩存對JButton和JLabel的引用,並且每次都返回一個引用,而不是分配一個新引用。 – 2010-07-13 20:24:19

+0

@Devon - 編碼時沒有IDE,我很懶惰:) – willcodejavaforfood 2010-07-14 07:30:04