Question

I want to post a picture, but I don't have reputation on this site, but I described my problem below :

| Name   | Grade  |
__________________
|  Febri |    60| <---- if this is a cell (row0,column1), 
                    I can't retrieve data on this cell if cursor is 
                    still pointing inside that cell.

take a look :

System.out.println(mytable.getValueAt(0,0)); --> output : Febri
System.out.println(mytable.getValueAt(0,1)); -- > output :    

that's because my mouse's cursor is still pointing inside that cell ..

Any suggestions? Is this problem related to mouse listener?

Please help , thx. 
import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

public class myTable extends JFrame {


String heads[] = { "Name", "Grade" };
Object[][] data = { { "Febri", "60" } };

JTable table;
JButton button;
JScrollPane scroll;

public myTable() {
    setLayout(new FlowLayout());
    table = new JTable(data, heads);


    scroll =new JScrollPane(table);

    button = new JButton("Retrieve Data");
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button) {
                System.out.println(table.getValueAt(0, 0));
                System.out.println(table.getValueAt(0, 1));
            }
        }
    });

    add(scroll);
}

public static void main(String args[])

{

    myTable tbl = new myTable();
    tbl.setVisible(true);
    tbl.setSize(500, 400);

} }
Was it helpful?

Solution

Your example works (mostly) fine.

I assume you mean while the cell is editable it won't return the value which the editor is showing?

This makes sense, as the value contained by the editor hasn't yet been committed to the model.

What you can do is stop the current editing process if the table is in edit model, this will commit the value from the editor to the model, which you can then read...

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button) {
        // Is the table in edit mode?
        if (table.isEditing()) {
            // Get the current editor
            TableCellEditor editor = table.getCellEditor();
            if (editor != null) {
                // Try and stop the cell editing process
                if (!editor.stopCellEditing()) {
                    // Cancel the editing if can't be stopped...
                    // You could handle an error state here instead...
                    editor.cancelCellEditing();
                }
            }
        }
        System.out.println(table.getValueAt(0, 0));
        System.out.println(table.getValueAt(0, 1));
    }
}

Of course, this all comes down to what it is you are trying to achieve...

OTHER TIPS

You need to stop editing of the cell. Table Stop Editing shows two ways to do this. One as demonstrated by MadProgrammer and the other allows you so set a property on the JTable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top