• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

B&S: Delete Problem with last row in JTable, getSelectedRow() return wrong row index

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a very annoying problem in my MVC design and have no clue at all why it happen.

The problem is as follow:

When I try to delete a row from my JTable it works perfect unless I delete the last row. While for all other row the next row is selected, this is not the case for the last row. Hence when I delete the last row, there is no selected row after.

If someone again click on the Delete button the table.getSelectedRow() method should return -1 as there are no rows selected.

Unfortunately, this is not the case and the method return the last stored selected row say 3 for example . As a consequence the program run into an exception.

To make things a little bit tangible I post a simple JTable test program on a MVC base.

To reproduce the error do the following:

  • 1. Start SimpleJTableTest
  • 2. Select last row in table
  • 3. Click on the Delete button, the selected row index is shown, click OK, the row becomes removed from the view and the model, no other remaining row is selected.
  • 4. Click on the Select button, the selected row index -1 should be presented and that's the problem.


  • Point 4. is the annoying problem. I don't know why the select row is not -1 .

    Any help is welcome :roll: .

    Regards,
    Darya

    Here the code for testing:

     
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Darya,

    Did you have any luck with this problem?

    Have you tried the following code?



    Notice that I've added a line - jt.clearSelection();, this will clear any selected rows once the row has been deleted. When you perform the select now, -1 is returned.

    Cheers,
    Matt.
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Matt,

    Your jt.clearSelection() unfortunately doesn't help here either.

    I have a workaround in my assignment solution, so I can deal with the problem. But honestly I would be more satisfied when we can make this minimized model I've posted run.

    Regards,
    Darya
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's strange cause it worked when I added the line to your code.
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    On what system are you testing? I test on Win XP. Did you follow the steps I described?

    Regards,
    Darya
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Maybe I forgot to say that one must copy all code and paste it in a file named SimpleJTableTest.java.

    Regards,
    Darya
     
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Matt Garner:
    That's strange cause it worked when I added the line to your code.



    Tested, it works here too, running win xp atm, let me know if you want me to try in linux
     
    Ranch Hand
    Posts: 918
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, Darya


    I use sometimes the :
    ListSelectionModel (each table has one).This model has a method :
    removeSelectionInterval(int,int)

    I hope this will help.

    Regards,
    Mihai
     
    Mihai Radulescu
    Ranch Hand
    Posts: 918
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have also table(s) in my gui(s) and after I move/deletete some row(amd I change the model) I do :



    In this way I have control over the row selection.

    I hope this will help


    Regards,
    Mihai.
    [ July 11, 2005: Message edited by: Mihai Radulescu ]
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ta Ri Ki Sun,

    what is atm? Nice to see whether it works under linux, however I have the problem under the following java version and operation system:

    JAVA VERSION:
    -------------

    java version "1.4.2_05"


    HOST PLATFORM:
    --------------

    Dell Latitude D800
    Intel Pentium M processor 1600MHz
    processor speed 1.56 GHz
    RAM 1024 MB
    Microsoft Windows XP Professional

    It's very strange that you do not reproduce the problem as I do

    Do you really get a -1

    Regards,
    Darya
     
    Ta Ri Ki Sun
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Darya Akbari:
    Hi Ta Ri Ki Sun,

    what is atm?



    At the moment, sorry I assumed

    Nice to see whether it works under linux


    No it works under windows, but I don't see why it wouldn't work under linux

    Here's my version info

    C:\SunCerts\SCJD\URLyBird\dist>java -version
    java version "1.4.2"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
    Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)



    I specifically tested the change Matt suggested, without his line clearSelection I always ended up with the last selection, this way it always goes to -1
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic