*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Preserving previous selection in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Preserving previous selection in JTable" Watch "Preserving previous selection in JTable" New topic
Author

Preserving previous selection in JTable

Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hello Everybody,

I was wondering if most of the people who have passed the assignment attempted to manage to preserve
previous selection and content displayed in the JTable.

I mean, your Network or Standalone client starts you select a Hotel Room, then you do a Search,
do a new selection as if you were going to book a Room, Search with name and location text fields empty
to reload all the Hotel Room data records, etc. preserving when you can the previous selection.

The approach to load my Hotel Records data is in the JTable is described here.

Currently trying to follow a similar but not identical approach to the Monkhouse book where I have
a method loadTable() that gets called from the actionPerformed() method of my Search class that implements
ActionListener and also when the GUI initially starts.

Currently I have a bug in the following scenario:
- GUI starts
- Select the 11th record down from the top (Record number 10 in the JTable since the rows start with number 0)
- Do a Search for Name "Palace" that should return 4 records
- I get:



If instead I have the following scenario:
- Select the 1st record down from the Top (Record number 0 in the JTable)
- Do a Search for Name "Palace".
- I get the 4 records without any problems.

For some reason it's like the the record selected in the JTable is sticking all the time.


Any thoughts? Suggestions?

Perhaps this is not a MUST requirement but I thought it would be nice to get the preservation of selections
to work correctly.


Thanks in advance,


Carlos.

SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Hi Carlos,

I think the logic to preserve selection (which I implemented too by the way), should go as follows:
  • get the selected room
  • do a search (which results in a new table model)
  • pass the new table model to the jtable
  • select the previous selected room (if applicable)


  • And the "if applicable" means: iterate over each record the new table model and see if the previous selected room still exists in the new table model (by checking if their primary keys are equal). When the record still exist select the corresponding row in the table by calling setRowSelectionInterval(index, index).

    Good luck!
    Kind regards,
    Roel


    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Sounds kind of similar to what I am doing.

    I modified the actionPerformed() method of my SearchHotelRoom class that
    implements ActionListener to do exactly the same steps in your answer and it worked.

    I had to delete the call to loadTable() though.

    Before when I had the bug I was doing in the actionPerformed() method the following:
    - does a search (getting a new table model).
    - call loadTable().

    My loadTable() method is similar to Andrew Monkhouse's example, it does the
    following:

    - gets the selected room
    - Updates the Model in the JTable
    - Selects the previously selected room (if applicable).


    By the way, before I posted this I had already overwritten the equals() method
    in my Room class (checking for equality of the primary key).


    Thanks once again for your help!



    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5212
        
      12

    Hi Carlos,

    Glad to hear you were able to solve the bug.

    I had another look at my code and I have also the same logic in the search as you (and as described in Andrew's book I guess):
  • do a search
  • call loadTable (which will set the new table model and preserve selection)

  • Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Strange ... my actionPerformed() method in my Search class implementing ActionListener
    no longer

    - does a search
    - calls loadTable()

    Instead now it has the four steps you mentioned in your first reply and obviously has more
    code now and repeated code from loadTable().

    I wonder if perhaps the way I am checking and getting the selected record is the problem???


    The way I am doing it in loadTable() is like this:




    Do you see anything wrong with this code?


    Thanks,

    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5212
        
      12

    Hi Carlos,

    My code to select the previously selected room:


    The getHotelRoomRecord-method just does the same like your code: if the index is valid (between 0 and record count), it returns the room. Otherwise null is returned.

    Maybe it is a stupid remark, but just to be 100% sure: you have to get the previously selected room before you pass the new table model (from the search) to your JTable.


    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,

    Roel De Nijs wrote:
    The getHotelRoomRecord-method just does the same like your code: if the index is valid (between 0 and record count), it returns the room. Otherwise null is returned.


    This was the problem in the scenario described originally why I would get an
    IndexOutOfBoundsException.
    I was returning in the getHotelRoomRecord() method of the table model class
    the Room object at that row index without checking the boundaries.


    With this change I changed the actionPerformed() of my Search to the way it was originally:
    - do the search.
    - load the table with the new table model.

    and I no longer get the IndexOutOfBoundsException in the scenario above.


    but it is not preserving the selection after selecting in a search.

    Let's say I start the GUI in standalone mode.
    I select the 10th record down from the top (11th record).
    Search for name "Palace" that returns 4 records.
    Select record number 3 (4th record) that has name "Palace" and Location "Digitopolis".
    Click search again with name and location fields empty and the JTable displays as selected
    record number 3 in the new model (4th record) with name "Grandview" and location "Whoville" instead of the record that
    has name "Palace" and Location "Digitopolis".

    I am scratching my head because if applicable when I iterate through all the rooms in the new
    model I compared for the primary key of the Rooms in order to reselect like we discussed
    before.

    If I modify my Search class actionPerformed() method to
    - get the selected room
    - do a search (which results in a new table model)
    - pass the new table model to the jtable
    - select the previous selected room (if applicable)
    It does preserve the selection.





    Anyway, I guess some Eclipse debugging awaits for me.


    Thanks,

    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5212
        
      12

    Hi Carlos,

    Glad to hear you have solved your initial issue.

    I can only tell you that the correct approach for the actionPerformed() of your Search is:
  • execute search
  • call loadTable (with new table model)


  • The loadTable method will:
  • get the selected room
  • pass the new table model to the jtable
  • select the previous selected room (if applicable)


  • And selecting the previous selected room (if applicable) is based on the primary key of a hotel room. It has nothing to do with the index of the selected record in the table. You should iterate through the records in the new table model and compare (based on primary key) each room with the previous selected room. If you find a match, you simply call jtable.setRowSelectionInterval(index, index); where index is the index of the record in the table model.
    It is certainly not that hard to preserve selection, but I guess you make a (silly) mistake and the silly ones are mostly those who are very hard to spot. Good luck with the debugging! I'm pretty sure you'll be able to get this working!

    Good luck.
    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    I fixed the bug.


    The issue was I was getting the previously selected room already from the new updated
    model before updating the JTable, instead of getting the previously selected room from
    the old model.

    In other words, before with the bug in my loadTable() method I had:




    and with the bug fixed and working now I have:




    Obviously I save in previousTableData the model every time before I get a new updated model
    in tableData.


    Thanks!


    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5212
        
      12

    Hi Carlos,

    Glad to hear you were able to fix this hard-to-spot bug

    Kind regards,
    Roel
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Preserving previous selection in JTable