Problem with jTable

hi

I have a problem with jtable.

I created one and edit a cell ,but if i refres the table then whole table

refresh but the particular cell which i was editing stay the with last content with focus on it.

I am setting new model in refresh method even then it stay thr.

how can i solve this problem , please help.

[349 byte] By [amitwaliaa] at [2007-10-2 10:59:18]
# 1
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
camickra at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 2
Woowwwwthanx camick u r legend.thanks agian it worked.
amitwaliaa at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

another question tribg to solve it since yesterday.

how to add actionlistener to cell cos i want to enter value at top cell by user and press enter.How to trap enter key like button or textfiled and implimenet actionlistener. Is thr anyway to get same functionality with cell in jtable.

Like if i press enter it should add new rows in jtable with results.

amitwaliaa at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well, I don't really understand your question.

When you use the enter key on a table, the "enter Action" is invoked, which simple moves the cell focus to the next row. You can change this behaviour by providing a custom action for the Enter key. See this posting for an example:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=657819

If you are actually editing a cell then the editor has focus, not the table. The default editor is simply a JTextField, so you can add an ActionListener to the editor. You would do something like this to get the editor:

DefaultCellEditor editor = (DefaultCellEditor)table.getEditor(Object.class);

JTextField textField = (JTextField)editor.getComponent();

camickra at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi camickr

I read the API and this forum also and created my code according to my need.But its still not working and i couldn't find out why, i dono what wrong i am doing.

here is the code.

InputMap imap = jTable1.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);

imap.put(tab, "none");

Action newAction = new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

JTable table = (JTable)e.getSource();

int row = table.getSelectedRow();

int column = table.getSelectedColumn();

showMessage(row+"-"+column, sTitle);

if (jTable1.editCellAt(0,0))

jTable1.getEditorComponent().requestFocusInWindow();

}

};

jTable1.getActionMap().put(enterKey, newAction);

amitwaliaa at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 6

hi

Sorry it worked ,its my mistake.I was not mapping the key properly.

But thr is another problem and its regarding the focus,i think.

here is new Code.

public void setTableAction()

{

InputMap imap = jTable1.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

imap.put(enterKey, "enter");

Action newAction = new AbstractAction(){

public void actionPerformed(ActionEvent e){

JTable table= (JTable)e.getSource();

int row = table.getSelectedRow();

int column = table.getSelectedColumn();

if(row==0 && column==0)

showMessage((String)table.getValueAt(0,0), sTitle);

}};

jTable1.getActionMap().put("enter", newAction);

}

now the problem is cell(0,0) has focus and if i edit it and then press enter it doesn't do anything but if i click some where else and then click on this cell(0,0) to select it and press enter, it worked.So if i select it then enter command work only.

So can u plz tell me why this enter command doesn't work when cell was in edit mode.

thanks in advance.

amitwaliaa at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 7

Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code, so the code is readable.

> So can u plz tell me why this enter command doesn't work when cell was in edit mode.

I guess because focus is on the text field used to edit the cell, not the table.

camickra at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hi camickr

I have seen all post in this forum and you seems to b very active and experienced one.Now i believe you can help me.

Regarding my last post i tried to get textfiled component and tried diffenent operations ,just like experiments, but none is working.

I understand while editing i write in the textfield in the cell and also i was able to set value but when i tried to get value on enter key it returned nothing ,it work only if i click on some other cell and then click back to the editable cell and press enter ,then it return the value.

Plz help me.

amitwaliaa at 2007-7-13 3:27:38 > top of Java-index,Desktop,Core GUI APIs...