Results 1 to 5 of 5
- 10-20-2011, 06:25 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 27
- Rep Power
- 0
checking if a cell in JTable is selected
i have this bit of code here that obviously takes some values from a JTable and puts them into some text fields then clears the selection ofJava Code:public void valueChanged(ListSelectionEvent e) { barcodeTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 1).toString()); nameTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 2).toString()); costTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 3).toString()); searchResults.clearSelection(); }
the JTable. the only problem with this is when i do a new search it runs this code because it takes the new search as a valuechange so i
either need a way to stop the searches from being a valuechange or a way to detect if a cell is selected so the code only runs when one is because at the moment i it works but i get a null pointer exception which is completely right cos it still runs when nothing is selected. any help would be appricated.
thank you
- 10-21-2011, 12:01 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 27
- Rep Power
- 0
Re: checking if a cell in JTable is selected
I managed to get it sorted if anyone got same problem i used
Java Code:try { int rowcheck = searchResults.getSelectedRow(); if (rowcheck > -1) { idTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 0).toString()); barcodeTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 1).toString()); nameTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 2).toString()); costTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 3).toString()); searchResults.clearSelection(); } } catch (NullPointerException ex) { }
-
Re: checking if a cell in JTable is selected
- 10-21-2011, 12:14 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 27
- Rep Power
- 0
Re: checking if a cell in JTable is selected
but it works and its the only way i could get it to work
-
Re: checking if a cell in JTable is selected
You're essentially ignoring a fatal bug in your code and that is just such bad practice, that it will bite you in the tail eventually, and soon. Better to check if a variable is null before you try to use it. First figure out which variable is null -- possibly searchResults, but you'll want to test for this in your code. If so, figure out why and then what you want to do if it is null.
Similar Threads
-
JButton to detect if cell in a table is selected
By Levian in forum New To JavaReplies: 5Last Post: 08-09-2011, 06:56 AM -
how to get the currently selected row fo a jtable?
By kulangotski in forum AWT / SwingReplies: 4Last Post: 01-16-2011, 05:22 AM -
confusing output of a selected cell in a JTable
By bigj in forum New To JavaReplies: 6Last Post: 01-30-2010, 02:55 PM -
JTable Gridline in selected row
By arubin in forum AWT / SwingReplies: 11Last Post: 11-24-2009, 07:05 PM -
JTable problem with getting ROW selected
By nadia in forum Advanced JavaReplies: 2Last Post: 01-13-2009, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks