Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add column Name dynamically in Jtable?

Tags:

java

jtable

I am using Jtable in my project where when tab key is pressed new column is added but the name of that column is not there. It is displaying a blank in that place I want that column name should be of excel format ie. like excel column name should be A,B,C and so on. So what should I check for that.

private void datatypetableKeyPressed(java.awt.event.KeyEvent evt) {                                         

        if (evt.getKeyCode() == KeyEvent.VK_TAB) {
            addColumn();
        }
    } 

private void addColumn() {
        DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
        JTableHeader th = datatypetable.getTableHeader();
        TableColumnModel tcm = th.getColumnModel();
        TableColumn tc = tcm.getColumn(0);

        if (model != null) {
            Vector v = new Vector(1);
            for (int j = 0; j < datatypetable.getRowCount(); j++) {
                tc.setHeaderValue("???");
                v.add("");


            }
            model.addColumn(v);
            th.repaint();
        }
    }

This is my code for adding new columns to jtable.

like image 584
Tanisha Agarwal Avatar asked Sep 16 '25 06:09

Tanisha Agarwal


1 Answers

I have done in this way and its working....Hope my answer help anyone...

private void datatypetableKeyPressed(java.awt.event.KeyEvent evt) {                                         
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                int col = datatypetable.getSelectedColumn();
                int row = datatypetable.getSelectedRow();
                int colCount = datatypetable.getColumnCount();
                int rowCount = datatypetable.getRowCount();
                col++;
                if (col >= colCount) {
                    col = 0;
                    row++;
                }
                if (row >= rowCount) {
                    row = 0;
                }
    //            datatypetable.getSelectionModel().setSelectionInterval(row, row);
    //            datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
    //            datatypetable.editCellAt(row, col);
                if (row == rowCount - 1) {
                    addRow();
                    datatypetable.scrollRectToVisible(datatypetable.getCellRect(rowCount, 0, true));
                }
            }

            if (evt.getKeyCode() == KeyEvent.VK_TAB) {
                int col = datatypetable.getSelectedColumn();
                int row = datatypetable.getSelectedRow();
                int colCount = datatypetable.getColumnCount();
                int rowCount = datatypetable.getRowCount();
                col++;
                if (col >= colCount) {
                    col = 0;
                    row++;
                }
                if (row >= rowCount) {
                    row = 0;
                }
    //            datatypetable.getSelectionModel().setSelectionInterval(row, row);
    //            datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
    //            datatypetable.editCellAt(row, col);
                if (col == colCount - 1) {
                    addColumn();
                    int selectedRow = 0;
                    datatypetable.editCellAt(selectedRow, 0);
                    datatypetable.setSurrendersFocusOnKeystroke(true);
                    datatypetable.getEditorComponent().requestFocus();
                    datatypetable.scrollRectToVisible(datatypetable.getCellRect(colCount, 0, true));
                }
            }

        }  


    private void addRow() {
            DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();

            if (model != null) {
                Vector v = new Vector(1);
    //            for (int j = 0; j <= datatypetable.getColumnCount(); j++) {
    //                v.add("");
    //            }
                v.add("");
                model.addRow(v);
            }
        }

        private void addColumn() {
            DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();

            if (model != null) {
                Vector v = new Vector(1);
    //            for (int j = 0; j < datatypetable.getRowCount(); j++) {
    //                v.add("");
    //            }
                v.add("");
                model.addColumn(Character.toString((char) ('A' + datatypetable.getColumnCount())), v);
            }
        }
like image 171
Tanisha Agarwal Avatar answered Sep 19 '25 15:09

Tanisha Agarwal