Java Swing GUI

How to Delete a Row in JTable using JButton​

In this tutorial, we are going to see how to delete a row in JTable using Delete button. JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns.

When a value is selected in a JTable, a TableModelEvent is generated, which is handled by implementing the TableModelListener interface. We can add or insert a JButton​ for each row of a Jtable in a cell by customizing the code in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing the TableCellRenderer interface and we have to redefine the getTableCellRendererComponent() method.


[st_adsense]  

Java Program to Delete a Row in JTable using Delete button:
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.CellEditorListener;
import java.awt.Component;
import java.awt.event.*;
import java.util.EventObject;

public class Main 
{
  public static void main(String[] args) 
  {  
    //JTable Header
    String[] columns = new String[] {"Id","Name","Address","Hourly rate"," "};

    //data for JTable in a 2D table
    Object[][] data = new Object[][] {
        {1, "Thomas", "Alaska", 20.0, " " },
        {2, "Jean", "Arizona", 50.0, " " },
        {3, "Yohan", "California", 30.0, " " },
        {4, "Emily", "Florida", 60.0, " " },
        {5, "Alex", "New York", 10.0, " " },
    };
    //define the model of JTable
    DefaultTableModel model = new DefaultTableModel(data, columns);
    //create a JTable with data
    JTable table = new JTable(model);

    //define our Renderer on the empty " " column
    table.getColumn(" ").setCellRenderer(new MyRendererAndEditor(table));
    table.getColumn(" ").setCellEditor(new MyRendererAndEditor(table));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(table));
    f.setSize(500, 180);
    f.setVisible(true);
  }
}

class MyRendererAndEditor implements TableCellRenderer, TableCellEditor 
{
  private JButton btn;
  private int row;

  MyRendererAndEditor(JTable table) {
    btn = new JButton("Remove");
    btn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.removeRow(row);
      }
    });
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object 
  value, boolean isSelected, boolean hasFocus, int row, int column) 
  {
    return btn;
  }

  @Override
  public Component getTableCellEditorComponent(JTable table, Object 
  value, boolean isSelected, int row, int column) 
  {
    this.row = row;
    return btn;
  }

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

  @Override
  public boolean isCellEditable(EventObject anEvent) { return true; }

  @Override
  public boolean shouldSelectCell(EventObject anEvent) { return true; }

  @Override
  public boolean stopCellEditing() { return true; }

  @Override
  public void cancelCellEditing() {}

  @Override
  public void addCellEditorListener(CellEditorListener l) {}

  @Override
  public void removeCellEditorListener(CellEditorListener l) {}
}

Output:


[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *