如何将删除操作添加到采用值id的每一行

时间:2014-11-26 16:45:16

标签: java swing jtable row jbutton

所以,我在向每一行添加一个操作以删除从Values列中获取id的所选行时遇到问题,因为Delete列在所有单元格中都有 - 。

enter image description here

我有ButtonRenderer和ButtonEditor来向JTable添加按钮

ButtonRenderer

import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
/**
*
* @author joepa37
*/
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}

 @Override
public Component getTableCellRendererComponent(JTable table, Object value,
  boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
  setForeground(table.getSelectionForeground());
  setBackground(table.getSelectionBackground());
} else {
  setForeground(table.getForeground());
  setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());
return this;
 }
}

ButtonEditor

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
/**
 *
 * @author joepa37
 */
public class ButtonEditor extends DefaultCellEditor {
  protected JButton button;
  private String label;
  private boolean isPushed;
  public ButtonEditor(JCheckBox checkBox) {
  super(checkBox);
  button = new JButton();
  button.setOpaque(true);
  button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
      fireEditingStopped();
    }
  });
}
 @Override
public Component getTableCellEditorComponent(JTable table, Object value,
  boolean isSelected, int row, int column) {
if (isSelected) {
  button.setForeground(table.getSelectionForeground());
  button.setBackground(table.getSelectionBackground());
} else {
  button.setForeground(table.getForeground());
  button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
 }
 @Override
public Object getCellEditorValue() {
if (isPushed) {
  // 
  // 
  JOptionPane.showMessageDialog(button, label + ": Ouch!");
  // System.out.println(label + ": Ouch!");
}
isPushed = false;
return new String(label);
 }
@Override
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
@Override
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}

我的桌子在这里

import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
/**
 *
 * @author joepa37
 */
public class deleteRowTable extends JFrame{
public deleteRowTable(){
    super();

    this.add(getPreview());

    setSize(500,500);
    setVisible(true);
}

public static void main(String args[]){
    new deleteRowTable();
}

public Component getPreview ()
{
    // Table
    JTable table = new JTable ( new ExampleTableModel () );
    JScrollPane scrollPane = new JScrollPane ( table );

    // Custom column
    TableColumn buttonColumn = table.getColumnModel ().getColumn ( 0 );

    // Custom renderer
    buttonColumn.setCellRenderer(new ButtonRenderer());

    // Custom editor
    buttonColumn.setCellEditor(new ButtonEditor(new JCheckBox()));

    return scrollPane;
}

public class ExampleTableModel extends AbstractTableModel
{
    private String[] columnNames = { "Delete", "Values"};
    private Object[][] data = { { "-", "id1"}, 
                                { "-", "id2"}, 
                                { "-", "id3"},
                                { "-", "id4"}, 
                                { "-", "id"} 
                            };

    public final Object[] longValues = { "-", "Value"};

    @Override
    public int getColumnCount ()
    {
        return columnNames.length;
    }

    @Override
    public int getRowCount ()
    {
        return data.length;
    }

    @Override
    public String getColumnName ( int col )
    {
        return columnNames[ col ];
    }

    @Override
    public Object getValueAt ( int row, int col )
    {
        return data[ row ][ col ];
    }

    @Override
    public Class getColumnClass ( int c )
    {
        return longValues[ c ].getClass ();
    }

    @Override
    public boolean isCellEditable ( int row, int col )
    {
        return true;
    }

    @Override
    public void setValueAt ( Object value, int row, int col )
    {
        data[ row ][ col ] = value;
        fireTableCellUpdated ( row, col );
    }
}
}

1 个答案:

答案 0 :(得分:3)

您可以在编辑器中获取行号。一种方法是通过:

class ButtonEditor extends DefaultCellEditor {
   protected JButton button;
   private String label;
   private boolean isPushed;
   private int myRow;  // *******added **********

   public ButtonEditor(JCheckBox checkBox) {
      // ..............
   }

   @Override
   public Component getTableCellEditorComponent(JTable table, Object value,
         boolean isSelected, int row, int column) {
      this.myRow = row;  // *******added **********
      if (isSelected) {
         button.setForeground(table.getSelectionForeground());
         button.setBackground(table.getSelectionBackground());
      } else {
         button.setForeground(table.getForeground());
         button.setBackground(table.getBackground());
      }
      label = (value == null) ? "" : value.toString();
      button.setText(label);
      isPushed = true;
      return button;
   }

   @Override
   public Object getCellEditorValue() {
      if (isPushed) {
         // can get the row number here
         JOptionPane.showMessageDialog(button, label + ": Ouch! " + myRow);
      }
      isPushed = false;
      return new String(label);
   }

获得行号后,即可访问模型所持行的任何项目。如果行已经排序,您可能需要从视图转换为模型。


例如:

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;


@SuppressWarnings("serial")
public class deleteRowTable extends JFrame {
   public deleteRowTable() {
      super();
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      this.add(getPreview());

      setSize(500, 500);
      setVisible(true);
   }

   public static void main(String args[]) {
      new deleteRowTable();
   }

   public Component getPreview() {
      // Table
      JTable table = new JTable(new ExampleTableModel());
      JScrollPane scrollPane = new JScrollPane(table);

      // Custom column
      TableColumn buttonColumn = table.getColumnModel().getColumn(0);

      // Custom renderer
      buttonColumn.setCellRenderer(new ButtonRenderer());

      // Custom editor
      buttonColumn.setCellEditor(new ButtonEditor(new JCheckBox()));

      return scrollPane;
   }

   public class ExampleTableModel extends AbstractTableModel {
      private String[] columnNames = { "Delete", "Values" };
      private Object[][] data = { { "-", "id1" }, { "-", "id2" },
            { "-", "id3" }, { "-", "id4" }, { "-", "id" } };

      public final Object[] longValues = { "-", "Value" };

      @Override
      public int getColumnCount() {
         return columnNames.length;
      }

      @Override
      public int getRowCount() {
         return data.length;
      }

      @Override
      public String getColumnName(int col) {
         return columnNames[col];
      }

      @Override
      public Object getValueAt(int row, int col) {
         return data[row][col];
      }

      @Override
      public Class<?> getColumnClass(int c) {
         return longValues[c].getClass();
      }

      @Override
      public boolean isCellEditable(int row, int col) {
         return true;
      }

      @Override
      public void setValueAt(Object value, int row, int col) {
         data[row][col] = value;
         fireTableCellUpdated(row, col);
      }
   }
}

@SuppressWarnings("serial")
class ButtonEditor extends DefaultCellEditor {
   protected JButton button;
   private String label;
   private boolean isPushed;
   private int myRow;
   private JTable table;

   public ButtonEditor(JCheckBox checkBox) {
      super(checkBox);
      button = new JButton();
      button.setOpaque(true);
      button.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
         }
      });
   }

   @Override
   public Component getTableCellEditorComponent(JTable table, Object value,
         boolean isSelected, int row, int column) {
      this.myRow = row;
      this.table = table;
      if (isSelected) {
         button.setForeground(table.getSelectionForeground());
         button.setBackground(table.getSelectionBackground());
      } else {
         button.setForeground(table.getForeground());
         button.setBackground(table.getBackground());
      }
      label = (value == null) ? "" : value.toString();
      button.setText(label);
      isPushed = true;
      return button;
   }

   @Override
   public Object getCellEditorValue() {
      if (isPushed) {

         deleteRowTable.ExampleTableModel model = (deleteRowTable.ExampleTableModel) table.getModel();
         String label = (String) model.getValueAt(myRow, 1);
         if (label != null) {
            JOptionPane.showMessageDialog(button, "This is your label: " + label);
            // delete the model's row here if desired.
         }
      }
      isPushed = false;
      return new String(label);
   }

   @Override
   public boolean stopCellEditing() {
      isPushed = false;
      return super.stopCellEditing();
   }

   @Override
   protected void fireEditingStopped() {
      super.fireEditingStopped();
   }
}

@SuppressWarnings("serial")
class ButtonRenderer extends JButton implements TableCellRenderer {
   public ButtonRenderer() {
      setOpaque(true);
   }

   @Override
   public Component getTableCellRendererComponent(JTable table, Object value,
         boolean isSelected, boolean hasFocus, int row, int column) {
      if (isSelected) {
         setForeground(table.getSelectionForeground());
         setBackground(table.getSelectionBackground());
      } else {
         setForeground(table.getForeground());
         setBackground(UIManager.getColor("Button.background"));
      }
      setText((value == null) ? "" : value.toString());
      return this;
   }
}