how to make the JTable.editCellAt(row,col) work

P

poorichard

hi,all
I'd to start editing the cell at row and column programatically,
the method JTable.editCellAt(row,col) is for that,
but there is something wrong in my code ,I think,

the whole code is as follows,
public class FrameTest {

Logger logger = Logger.getLogger(FrameTest.class.getName());
static int width = 500;
static int height = 300;
JFrame frame;
public FrameTest(){
logger.setLevel(Level.ALL);

createUI();
}

JTable table;
public void createUI(){
logger.fine("start createUI");

frame = new JFrame("TestFrame");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(width,height));

initTable();
table.requestFocusInWindow();
boolean d = table.editCellAt(0, 1);
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}

DefaultTableModel tableModel;
static final String[] columnNames = {"a","b","c","d","e","f","g","h"};
private void initTable(){
tableModel = new DefaultTableModel(columnNames,2);
table = new JTable(tableModel);
table.setRowSelectionAllowed(false);

table.requestFocusInWindow();
boolean isEditing = table.editCellAt(0, 1);
logger.info("isEditing?:"+isEditing);
}

public static void main(String[] args){
new FrameTest();
}
}

it can't work yet in my PC,
the platform I used is Eclipse3.2/3.1 and JDK1.5/1.6,
is there something wrong?

(I had post it half an hour ago,but it not show ,
so I post it again )

best regards,
Richard
 
P

poorichard

I'd got it,
the editCellAt(row,col) should invoked after the frame painted,
then it will work(though I'd not know why),

the code is as follows:

frame.setVisible(true);
table.editCellAt(row,col);

the main problem is that I need editCellAt custom cell while the
editing stopped,
the default sequence whould be [ (row,col)]
,(0,0),(0,1),....(1,0),(1,1)...
and I need the sequence likes (0,0),(1,0),(0,1),(1,1)....

I tried this at the tableModelListener ,the code is as follows:
class ModelListener implements TableModelListener {

public void tableChanged(TableModelEvent e) {
tableModel.removeTableModelListener(this);
int editingRow = table.getEditingRow();
int editingColumn = table.getEditingColumn();
if(editingRow == 0 && editingColumn == 0){
table.requestFocusInWindow();
table.editCellAt(1, 1);
tableModel.addTableModelListener(this);
}
}
}

and this can't work,I think the problem may be the table will be
repainted after the tableChanged, so the editCell will be removed,

all right ,I know the JTable's actionMap can do this for the key
event,but without the mouse pressed,

so who can give me some tips?

best regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top