Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a editable table, In cell of table when i put value then it not get value through the method getValueAt() when the cell editing still on....
it only get value through getValueAt() when the cell editing is disable..

For that i want automatically disable editing of the JTable cell...

Thanks regards..
Vishal!
Posted

You can use a TableModel.

Like this
Java
public class MyModel extends AbstractTableModel{

}

Then set the model into your JTable.
Java
JTable myTable = new JTable();
myTable.setModel(new MyModel());
 
Share this answer
 
Comments
vishal.v.patil 6-Mar-13 5:11am    
but it will lost my data..
i want my data also from cells...,,
Shubhashish_Mandal 6-Mar-13 8:20am    
I have used it in many application and it works fine for me.
How it will lost your data, I can not understand. Post your code if possible.
vishal.v.patil 6-Mar-13 8:23am    
if we have already my data in table then after i apply tablemodel at runtime can its work fine!!??
it may lost data or not??
Shubhashish_Mandal 6-Mar-13 8:36am    
you have to add the data through model into your table.
vishal.v.patil 6-Mar-13 8:39am    
but i want to fill data from user and after that i want to disable the editing from table on button click event!!
Here is another way..
Java
JTable table = new JTable(){  
       public boolean isCellEditable(int row,int column){  
         Object o = getValueAt(row,column);  
         if() return false;  
         return true;  
       }  
     }; 
 
Share this answer
 
Comments
vishal.v.patil 6-Mar-13 8:54am    
it initialize the table object then it will fails...
this will create new table object..
Its work as you want. After click the disable button cell editing will stop.
Java
public class TestTable {

    boolean flag = false;

    public void create() {
        JTable table = new JTable(2, 2) {

            public boolean isCellEditable(int row, int column) {
                if (flag) {
                    return false;
                }
                return true;
            }
        };
        JFrame f = new JFrame();
        JButton button = new JButton("Disable");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                flag = true;
            }
        });
        f.setLayout(new FlowLayout());
        f.add(new JScrollPane(table));
        f.add(button);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new TestTable().create();
            }
        });
    }
 
Share this answer
 
Comments
vishal.v.patil 7-Mar-13 0:31am    
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import javax.swing.*;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.EmptyBorder;

import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
public class TestTable {

boolean flag = false;

public void create() {
final JTable table = new JTable(2, 2) {

public boolean isCellEditable(int row, int column) {
if (flag) {
return false;
}
return true;
}
};
JFrame f = new JFrame();
JButton button = new JButton("Disable");
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
flag = true;
}
});
JButton button1 = new JButton("Show");
button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
System.out.println("Value At TB("+i+","+j+")"+table.getModel().getValueAt(i,j));
}
});
f.setLayout(new FlowLayout());
f.add(new JScrollPane(table));
f.add(button); f.add(button1);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
new TestTable().create();
}
});
}
}

Now see the problm...!!
Shubhashish_Mandal 7-Mar-13 6:54am    
what is the problem ?
vishal.v.patil 7-Mar-13 8:35am    
when we focus on specific cell that's value not prints!!
this is the problm....
vishal.v.patil 9-Mar-13 1:25am    
if i want to these only one button it not works..!
How i do..
Here is the solution...
Java
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
 
Share this answer
 
Comments
vishal.v.patil 7-Mar-13 8:51am    
m very very thankful 2 u bro...!!!

such a lot of thanks..!!
>>>>Shubhashish Mandal
Write blow code in your table constructor
public boolean isCellEditable(int row, int column){

return false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900