I am developing a Java application that uses a JTable. I want to allow the user to enter data in a JTable that can later be printed or saved as PDF file. The issues is: Suppose the user has entered some data and the cursor is on the last cell of the row in a table (I have uploaded an image-suppose the cursor is on the highlighted cell).How do I make it in such a way that when a user presses the Enter Button(Keyboard button) the application will add/append a new row that is empty to the table so that the user can fill in other data.

Recommended Answers

All 9 Replies

Which part of that is the problem? Responding to the enter button, adding a blank row to a table, or something else?

Hie James,
The problem is adding a new row to the table. The row should be part of the table not a stand alone. What should I include to make it work that way?

Here's the trick:
JTable is just a view, the actual structure and data is in its TableModel.
Get your JTable's TableModel (DefaultTableModel) myTable.getModel(), and add a new row to that addRow(Object[] data)

Hie James,

I have created a listener that will listen to table changes as follows:

    public void createTable(){
    table= new JTable(data,colNames);
    sp= new JScrollPane(table);
    table.getModel().addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent tme) {
            //my code for adding a new row has to go here
            table.getModel().//this line is giving me an error.
        }
    });

i have tried the following and it is giving me an error - i mean i cant see the getTableModel method.

    model = JTableObj.getTableModel();
    model.insertRow(table.getRowCount(),new Object[] {"","","","",""});

How do I resolve this? thanks in advance for yor help.

As in my previous post: It's getModel, not getTableModel, and you have to cast the returned value to (DefaultTableModel) because thats where the addRow method is. (Unless, of course, you have defined your own TableModel.)

Hie James,

Thanks for the idea.I casted it as follows:

  TableModel tm=table.getModel();
           DefaultTableModel dtm=(DefaultTableModel)tm;
           dtm.addRow(newRow);

The problem is it is giving this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel
    at JTableDemo$1.tableChanged(JTableDemo.java:48)
    at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
    at javax.swing.table.AbstractTableModel.fireTableCellUpdated(AbstractTableModel.java:275)
    at javax.swing.JTable$1.setValueAt(JTable.java:694)
    at javax.swing.JTable.setValueAt(JTable.java:2741)
    at javax.swing.JTable.editingStopped(JTable.java:4723)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141)
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368)
    at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:233)
    at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5480)
    at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(DefaultCellEditor.java:385)
    at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
    at javax.swing.JTextField.postActionEvent(JTextField.java:721)
    at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2878)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2925)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2841)
    at java.awt.Component.processEvent(Component.java:6282)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
    at java.awt.Component.dispatchEventImpl(Component.java:4731)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Is it the right listener that i used?

Here is my whole code if it can help:

public final class JTableDemo extends JFrame {

    String[] colNames = {"Name", "Surname"};
    String[][] data = {{"Nation", "Chirara"}, {"Bill", "Thompson"}};
    String [][] newRow={{"",""},{"",""}};
    JTable table;
    JScrollPane sp;

    JTableDemo(String title){
    super(title);
    this.setSize(300, 300);
    this.setVisible(true);

    Container con= this.getContentPane();
    this.createTable();
    con.add(sp);


    }
    public void createTable(){
    table= new JTable(data,colNames);
    sp= new JScrollPane(table);
    table.getModel().addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent tme) {

           TableModel tm=table.getModel();
           DefaultTableModel dtm=(DefaultTableModel)tm;
           dtm.addRow(newRow);
        }
    });


    }
    public static void main(String [] args){
    JTableDemo jtd= new JTableDemo("My Table");

    }

}

Please advise me where i have gone wrong. I want it to work in such a way that when i hit the Keyboard Enter button, it appends a new row that is empty to the existing table. thanks for ideas once again.

i have added an image of my output. i want it to work in such that when i press the enter button with the focus on the last cell as shown in the output image....it should append a new empty row.

Capture2.PNG

You're getting that error because the constructor you used to create your JTable is a simple one that doesn't build a TabelModel that supports adding rows.

DefaultTableModel is the obvious first choice. To get a DefaultTableModel you simply ask for one, ie

DefaultTableModel tm = new DefaultTableModel(data, colNames);
table= new JTable(tm);

now you have a JTable with a TableModel that supports adding rows.

Setting up the event handler to add the row at the right time is a different problem, but ... one step at a time

Hie James,

Thanks very much for your help. I really appreciate your patience with me.
I did what you suggested and it worked perfect.

Now I have a different problem. let me close this thread and start a new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.