Results 1 to 5 of 5
  1. #1
    Dcalladi is offline Member
    Join Date
    Oct 2011
    Posts
    27
    Rep Power
    0

    Default checking if a cell in JTable is selected

    Java 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();
            }
    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 of
    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

  2. #2
    Dcalladi is offline Member
    Join Date
    Oct 2011
    Posts
    27
    Rep Power
    0

    Default 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) {
                }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: checking if a cell in JTable is selected

    Quote Originally Posted by Dcalladi View Post
    I managed to get it sorted if anyone got same problem i used

    Java Code:
    try {
                    //....
                    }
                } catch (NullPointerException ex) {
                }
    Just. Don't. Do. This. Ever.

  4. #4
    Dcalladi is offline Member
    Join Date
    Oct 2011
    Posts
    27
    Rep Power
    0

    Default Re: checking if a cell in JTable is selected

    but it works and its the only way i could get it to work

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: checking if a cell in JTable is selected

    Quote Originally Posted by Dcalladi View Post
    but it works and its the only way i could get it to work
    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

  1. JButton to detect if cell in a table is selected
    By Levian in forum New To Java
    Replies: 5
    Last Post: 08-09-2011, 07:56 AM
  2. how to get the currently selected row fo a jtable?
    By kulangotski in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2011, 06:22 AM
  3. Replies: 6
    Last Post: 01-30-2010, 03:55 PM
  4. JTable Gridline in selected row
    By arubin in forum AWT / Swing
    Replies: 11
    Last Post: 11-24-2009, 08:05 PM
  5. JTable problem with getting ROW selected
    By nadia in forum Advanced Java
    Replies: 2
    Last Post: 01-13-2009, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •