Hy again ,


I have another problem now ,

I have an application with a jTabel , you can double click a cell and you can change the value that is inside it , after you changed the value inside the cell and hit enter the value i enterd remains but i need to hit again enter key to return me the row and column that i need to save later in the database.

My question :

How can i save the data from the cell after editing was done without needing to hit ENTER key again?

My code is this but it does not work :

private void jTable2KeyPressed(java.awt.event.KeyEvent evt) {                                   
        // TODO add your handling code here:
        if(evt.getKeyCode() == evt.VK_ENTER){
            
            DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();
            int row = jTable2.getSelectedRow();
            int column = jTable2.getSelectedColumn();
            Object value2 =  model.getValueAt(row , column);
            System.out.println(value2);
        }
}

I tried that but still does not work ,

I made a new private void edit(){//code} and nothing retuned

I would like just to System.out.println("edited") something after i edited the cell without needing to press ENTER key again

"Does not work" is pretty vague. Using the TableModelListener as mKorbel linked to should work just fine.

Post the code that you used for your listener.

private void edit(){
        DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();
        int row = jTable2.getSelectedRow();
        int col = jTable2.getSelectedColumn();
        final Object rowData = model.getValueAt(row,col);
        //final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
        final String columnNames[] = { "ID", "nume", "prenume","prenume" };
        JScrollPane scrollPane = new JScrollPane(jTable2);
        jTable2.getModel().addTableModelListener(new TableModelListener() {

          public void tableChanged(TableModelEvent e) {
             System.out.println(e+"  cacat");
             //jTable2.setValueAt("",0,0);
          }
        }); 
        jTable2.setValueAt("",0,0);
    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
    }

I do not know how to use it , i read about it but i don`t realy know

The import part of that example code is the listener being added to the JTable

jTable2.getModel().addTableModelListener(new TableModelListener() {

          public void tableChanged(TableModelEvent e) {
             System.out.println(e+"  cacat");
             //jTable2.setValueAt("",0,0);
          }
        });

You need to put that in your code somewhere after you have initialized 'jTable2'. It only needs to be done once when you are setting up the table.

Ok , i will read those documents.For now i will mark this thread as solved.

Thank you for the fast repleys.

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.