WHAT'S NEW?

Editable JTable

EditableTable.java

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;
import java.awt.*;

public class EditableTable extends JFrame

{
EditableTable()
{
super("Editable Table");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();

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

        Object[][] dataEntries = {
        { "Rahul", "Jain", new Integer(50), "B-tech", new Boolean(false) },
        { "Ruturaj", "Kokate", new Integer(80), "PG", new Boolean(true) },
        { "Akshay ", "Shinde", new Integer(80), "Msc", new Boolean(true) },
        { "Gaurav", "Deshpande", new Integer(80), "Bsc", new Boolean(true) },
        { "Amey", "Barve", new Integer(80), "PG", new Boolean(true) },
        { "Rahil", "Arai", new Integer(70), "Gratuate", new Boolean(false) },
        { "Manik", "Pawar", 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));


        c.add(new JScrollPane(table));



setVisible(true);

}
    public static void main(String[] a)
    {
        new EditableTable();
        }
    }
     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-----------------------------------------------------



0 comments:

Post a comment