Creating an Editable Table in Java

Introduction

 
In this article, we are going to making a table. This table is not a static table; it is editable. And in my previous article The Use of JTable in Swing we describe how to create a table and insert data in to table. Today we describe how changes can be made in a table Dynamically. Editable is nothing; it just is a simple table with some flexible options within a table.
 
In this example, we are going to use a java class DefualtCellEditor. With the help of this class we make cells of a table editable.
 

DefaultCellEditor class API Description

 
Constructors
  • DefaultCellEditor(JCheckBox checkBox) : Constructs a DefaultCellEditor object that uses a check box.
  • DefaultCellEditor(JComboBox comboBox) : Constructs a DefaultCellEditor object that uses a combo box.
  • DefaultCellEditor(JTextField textField) : Constructs a DefaultCellEditor that uses a text field.
General use methods
  • void cancelCellEditing() : Forwards the message from the CellEditor to the delegate.
  • Object getCellEditorValue() : Forwards the message from the CellEditor to the delegate.
  • int getClickCountToStart() : Returns the number of clicks needed to start editing.
  • Component getComponent() : Returns a reference to the editor component.
  • boolean isCellEditable(EventObject anEvent) : Forwards the message from the CellEditor to the delegate.
  • void setClickCountToStart(int count) : Specifies the number of clicks needed to start editing.
  • boolean shouldSelectCell(EventObject anEvent) : Forwards the message from the CellEditor to the delegate.
  • boolean stopCellEditing() : Forwards the message from the CellEditor to the delegate.
Code Explanation
 
Step 1:  Importing necessary packages because we use the following classes of appropriate packages:
  1. import javax.swing.DefaultCellEditor;  
  2. import javax.swing.JComboBox;  
  3. import javax.swing.JFrame;  
  4. import javax.swing.JScrollPane;  
  5. import javax.swing.JTable;  
  6. import javax.swing.table.AbstractTableModel;  
  7. import javax.swing.table.TableModel;  
Step 2: Creating a class and main methods with a frame:
  1. public class EditableTable  
  2. {  
  3.     public static void main(String[] a)  
  4.     {  
  5.         JFrame frame = new JFrame();  
  6.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
Step 3: Creating a two dimensional array of object types containing data information and a single dimensional array of title and education String type.
  1. String[] Education = { "PG","Msc""B-Tech,","Bsc""12th""10th" };  
  2. String[] columnTitles = { "First Name""Last Name""weight""Qualification""age(18+)" };  
  3. Object[][] dataEntries = {  
  4. "ABHISHEK""DUBEY"new Integer(50), "B-tech"new Boolean(false) },  
  5. "MANISH""TIWARI"new Integer(80), "PG"new Boolean(true) },  
  6. "ANURUDDHA ""PANDEY"new Integer(80), "Msc"new Boolean(true) },  
  7. "Bindresh""AGINHOTRI"new Integer(80), "Bsc"new Boolean(true) },  
  8. "SOURABH""TRIPATHI"new Integer(80), "PG"new Boolean(true) },  
  9. "AMIT""GUPTA"new Integer(70), "Gratuate"new Boolean(false) },  
  10. "AMIT""VERMA"new Integer(55), "12TH"new Boolean(true) }, };  
Step 4: Creating another component object.
  1. JComboBox comboBox = new JComboBox(Education);  
  2. table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));  
  3. frame.add(new JScrollPane(table));  
  4. frame.setSize(300200);  
  5. frame.setVisible(true);  
Step 5: Creating another a  class which contains some helper methods.
  1. class EditableTableModel extends AbstractTableModel  
  2. {  
  3.      String[] columnTitles;  
  4.      Object[][] dataEntries;  
  5.      int rowCount;  
  6.     public EditableTableModel(String[] columnTitles, Object[][] dataEntries)  
  7.     {  
  8.           this.columnTitles = columnTitles;  
  9.           this.dataEntries = dataEntries;  
  10.     }  
  11.     public int getRowCount()  
  12.     {  
  13.           return dataEntries.length;  
  14.     }  
  15.     public int getColumnCount()  
  16.     {  
  17.          return columnTitles.length;  
  18.     }  
  19.     public Object getValueAt(int row, int column)  
  20.     {  
  21.          return dataEntries[row][column];  
  22.     }  
  23.     public String getColumnName(int column)  
  24.     {  
  25.          return columnTitles[column];  
  26.     }  
  27.     public Class getColumnClass(int column)  
  28.     {  
  29.          return getValueAt(0, column).getClass();  
  30.     }  
  31.     public boolean isCellEditable(int row, int column)  
  32.     {  
  33.          return true;  
  34.      }  
  35.     public void setValueAt(Object value, int row, int column)  
  36.     {  
  37.         dataEntries[row][column] = value;  
  38.     }  
  39. }  
Complete code
  1. import javax.swing.DefaultCellEditor;  
  2. import javax.swing.JComboBox;  
  3. import javax.swing.JFrame;  
  4. import javax.swing.JScrollPane;  
  5. import javax.swing.JTable;  
  6. import javax.swing.table.AbstractTableModel;  
  7. import javax.swing.table.TableModel;  
  8. public class EditableTable  
  9. {  
  10.     public static void main(String[] a)  
  11.     {  
  12.         JFrame frame = new JFrame();  
  13.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  14.         String[] columnTitles = { "First Name""Last Name""weight""Qualification""age(18+)" };  
  15.         Object[][] dataEntries = {  
  16.         { "ABHISHEK""DUBEY"new Integer(50), "B-tech"new Boolean(false) },  
  17.         { "MANISH""TIWARI"new Integer(80), "PG"new Boolean(true) },  
  18.         { "ANURUDDHA ""PANDEY"new Integer(80), "Msc"new Boolean(true) },  
  19.         { "Bindresh""AGINHOTRI"new Integer(80), "Bsc"new Boolean(true) },  
  20.         { "SOURABH""TRIPATHI"new Integer(80), "PG"new Boolean(true) },  
  21.         { "AMIT""GUPTA"new Integer(70), "Gratuate"new Boolean(false) },  
  22.         { "AMIT""VERMA"new Integer(55), "12TH"new Boolean(true) }, };  
  23.         TableModel model = new EditableTableModel(columnTitles, dataEntries);  
  24.         JTable table = new JTable(model);  
  25.         table.createDefaultColumnsFromModel();  
  26.         String[] Education = { "PG","Msc""B-Tech,","Bsc""12th""10th" };  
  27.         JComboBox comboBox = new JComboBox(Education);  
  28.         table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));  
  29.         rame.add(new JScrollPane(table));  
  30.         frame.setSize(300200);  
  31.         frame.setVisible(true);  
  32.         }  
  33.     }  
  34.      class EditableTableModel extends AbstractTableModel  
  35.     {  
  36.         String[] columnTitles;  
  37.         Object[][] dataEntries;  
  38.         int rowCount;  
  39.         public EditableTableModel(String[] columnTitles, Object[][] dataEntries)  
  40.         {  
  41.             this.columnTitles = columnTitles;  
  42.             this.dataEntries = dataEntries;  
  43.         }  
  44.         public int getRowCount()  
  45.         {  
  46.             return dataEntries.length;  
  47.         }  
  48.         public int getColumnCount()  
  49.         {  
  50.             return columnTitles.length;  
  51.         }  
  52.         public Object getValueAt(int row, int column)  
  53.         {  
  54.             return dataEntries[row][column];  
  55.         }  
  56.         public String getColumnName(int column)  
  57.         {  
  58.             return columnTitles[column];  
  59.         }  
  60.         public Class getColumnClass(int column)  
  61.         {  
  62.             return getValueAt(0, column).getClass();  
  63.         }  
  64.         public boolean isCellEditable(int row, int column)  
  65.         {  
  66.             return true;  
  67.         }  
  68.         public void setValueAt(Object value, int row, int column)  
  69.         {  
  70.              dataEntries[row][column] = value;  
  71.         }  
  72.     }  
  73. }  
Output
 
The initial output of cmd.
 
p00.gif
 
Basic output(first look):
 
p0.gif 
 
The following output shows the modified option:
 
p1.gif 
 
By using the mouse pointer you can edit the age column:
 
p2.gif 
 
Resources