aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes getValueAt in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getValueAt in JTable" Watch "getValueAt in JTable" New topic
Author

getValueAt in JTable

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I have a JTable that I have the ability to scroll up and down through the rows using the up and down arrow keys. I also have the information from the rows going into some textFields. My problem is, that I am doing a getValueAt on a keyPressed event and when the info is displayed into the text fields, it is always one row off. I want the row that is highlighted to display in the fields. Not the row that the keyPressed event was on. Does this make sense?
Thanks for any help!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Instead of KeyPress use the ListSelectionListener like in the following code.
CODE] JTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
tableRowHasChanged();
}
}); [/CODE]
where tableRowHasChanged is a method to display your JTextFields.
Mark
[ May 20, 2002: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the reply Mark. Using ListSelectionListener worked. However, it created another problem for me, which is one I don't understand. I'm a newbie at using ListSelectionListener so I was wondering if you could help me figure out where the problem lies.
In my application, there is a JComboBox that the user picks a customer. Each time there is an actionEvent on the customer JComboBox, a method is run that populates a table below with the corresponding information for that customer.
Since I am now using ListSelectionListener, I can click on a row on a table, or scroll up and down through it using arrow keys and the info is put into my text fields just wonderfully. However, when I change the customer in the JComboBox, I keep getting an ArrayIndexOutOfBoundsException -1 < 0.
This only happens if I have selected a row in the table. If I don't do anything in the table, I can choose different customers left and right without any grief.
Could you please help me out? I guess I don't understand what it's trying to tell me. If you need me to post code, let me know.
Thanks a million!!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well basically you have a row selected, so the row number is used, when you select from the JComboBox, the results that come from that don't have the same amount of records, and the past selected is higher than the max number of records of the new customer.
Let me look for some code to fix that for you.
Actually I don't have any code like that.
I think somewhere in your code you are using the selected RowNumber, and then when the JCombobox changes the TableModel, it also tries to run code with that RuwNumber you had previously, which is higher than the actual amount of rows in the JTable now.
Does that make sense?
Mark
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Mark,
I don't know if this helps you any, but here is some of my code.

Thanks again for your help.
I really appreciate it.
[ May 21, 2002: Message edited by: Jennifer Sohl ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Ok try this.
You have the following code in the getSelectedValue


The first line says
i = table.getSelectedRow();
If you are refreshing the JTable with new data from the selection made in the JCombobox, then the i might end up equaling -1 which would then make the code after that line fail with Out of Subscript range errors.
So add an if (i > -1) or should it be i > 0. one of these two, that way it won't try to run the code that assigns the String variables.
Like the line: nameField = (String)rm.getValueAt(i,2);
if i = -1 then you will get an error here, but if it is in an if statement and i = -1 then the code will never run.
Mark
[ May 21, 2002: Message edited by: Mark Spritzler ]
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Mark,
Guess What?? It worked! Don't I feel stupid! That was so simple, I can't believe I missed it.
Once again, thanks so much for your help.
It is VERY MUCH appreciated!!
Have a great day!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getValueAt in JTable