Creating an Editable Table in Java

Introduction

In this article we are going to making a table. This table is not a static table; its 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:

import javax.swing.DefaultCellEditor;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableModel;

Step 2: Creating a class and main methods with a frame:

public class EditableTable

{

     public static void main(String[] a)

    {

         JFrame frame = new JFrame();

         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.

String[] Education = { "PG","Msc", "B-Tech,","Bsc", "12th", "10th" };

String[] columnTitles = { "First Name", "Last Name", "weight", "Qualification", "age(18+)" };

Object[][] dataEntries = {

{ "ABHISHEK", "DUBEY", new Integer(50), "B-tech", new Boolean(false) },

{ "MANISH", "TIWARI", new Integer(80), "PG", new Boolean(true) },

{ "ANURUDDHA ", "PANDEY", new Integer(80), "Msc", new Boolean(true) },

{ "Bindresh", "AGINHOTRI", new Integer(80), "Bsc", new Boolean(true) },

{ "SOURABH", "TRIPATHI", new Integer(80), "PG", new Boolean(true) },

{ "AMIT", "GUPTA", new Integer(70), "Gratuate", new Boolean(false) },

{ "AMIT", "VERMA", new Integer(55), "12TH", new Boolean(true) }, };

Step 4: Creating another component object.

JComboBox comboBox = new JComboBox(Education);

table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));

frame.add(new JScrollPane(table));

frame.setSize(300, 200);

frame.setVisible(true);

Step 5: Creating another a  class which contains some helper methods.

class EditableTableModel extends AbstractTableModel

{

     String[] columnTitles;

     Object[][] dataEntries;

      int rowCount;

     public EditableTableModel(String[] columnTitles, Object[][] dataEntries)

    {

          this.columnTitles = columnTitles;

          this.dataEntries = dataEntries;

    }

     public int getRowCount()

    {

          return dataEntries.length;

    }

     public int getColumnCount()

    {

         return columnTitles.length;

    }

     public Object getValueAt(int row, int column)

    {

         return dataEntries[row][column];

    }

     public String getColumnName(int column)

    {

         return columnTitles[column];

    }

     public Class getColumnClass(int column)

    {

         return getValueAt(0, column).getClass();

    }

     public boolean isCellEditable(int row, int column)

    {

         return true;

     }

     public void setValueAt(Object value, int row, int column)

    {

        dataEntries[row][column] = value;

    }

}

Complete code


import
 javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class EditableTable
{
    public static void main(String[] a)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] columnTitles = { "First Name", "Last Name", "weight", "Qualification", "age(18+)" };
        Object[][] dataEntries = {
        { "ABHISHEK", "DUBEY", new Integer(50), "B-tech", new Boolean(false) },
        { "MANISH", "TIWARI", new Integer(80), "PG", new Boolean(true) },
        { "ANURUDDHA ", "PANDEY", new Integer(80), "Msc", new Boolean(true) },
        { "Bindresh", "AGINHOTRI", new Integer(80), "Bsc", new Boolean(true) },
        { "SOURABH", "TRIPATHI", new Integer(80), "PG", new Boolean(true) },
        { "AMIT", "GUPTA", new Integer(70), "Gratuate", new Boolean(false) },
        { "AMIT", "VERMA", new Integer(55), "12TH", new Boolean(true) }, };
        TableModel model = new EditableTableModel(columnTitles, dataEntries);
        JTable table = new JTable(model);
        table.createDefaultColumnsFromModel();
        String[] Education = { "PG","Msc", "B-Tech,","Bsc", "12th", "10th" };
        JComboBox comboBox = new JComboBox(Education);
        table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));
        rame.add(new JScrollPane(table));
        frame.setSize(300, 200);
        frame.setVisible(true);
        }
    }
     class EditableTableModel extends AbstractTableModel
    {
        String[] columnTitles;
        Object[][] dataEntries;
        int rowCount;
        public EditableTableModel(String[] columnTitles, Object[][] dataEntries)
        {
            this.columnTitles = columnTitles;
            this.dataEntries = dataEntries;
        }
        public int getRowCount()
        {
            return dataEntries.length;
        }
        public int getColumnCount()
        {
            return columnTitles.length;
        }
        public Object getValueAt(int row, int column)
        {
            return dataEntries[row][column];
        }
        public String getColumnName(int column)
        {
            return columnTitles[column];
        }
        public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
        public boolean isCellEditable(int row, int column)
        {
            return true;
        }
        public void setValueAt(Object value, int row, int column)
        {
             dataEntries[row][column] = value;
        }
    }
}

Output 

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

Populating and Editing a DataGrid in ASP.NET
How to Create and Use the Editable Grid Screen in LightSwitch Beta 2
Fully editable grid Using The Repeater Control

Data Screens in LightSwitch 2011: Part 3
Editable ListBox in C#

Up Next
    Ebook Download
    View all
    Learn
    View all