aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Delete Problem with last row in JTable, getSelectedRow() return wrong row index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S: Delete Problem with last row in JTable, getSelectedRow() return wrong row index" Watch "B&S: Delete Problem with last row in JTable, getSelectedRow() return wrong row index" New topic
Author

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

Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
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:



    SCJP, SCJD, SCWCD, SCBCD
    Anonymous
    Ranch Hand

    Joined: Nov 22, 2008
    Posts: 18944
    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

    Joined: Aug 21, 2004
    Posts: 1855
    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

    Joined: Nov 22, 2008
    Posts: 18944
    That's strange cause it worked when I added the line to your code.
    Darya Akbari
    Ranch Hand

    Joined: Aug 21, 2004
    Posts: 1855
    On what system are you testing? I test on Win XP. Did you follow the steps I described?

    Regards,
    Darya
    Darya Akbari
    Ranch Hand

    Joined: Aug 21, 2004
    Posts: 1855
    Maybe I forgot to say that one must copy all code and paste it in a file named SimpleJTableTest.java.

    Regards,
    Darya
    Ta Ri Ki Sun
    Ranch Hand

    Joined: Mar 26, 2002
    Posts: 442
    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
    Mihai Radulescu
    Ranch Hand

    Joined: Sep 18, 2003
    Posts: 916

    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


    SCJP, SCJD, SCWCD, OCPJBCD
    Mihai Radulescu
    Ranch Hand

    Joined: Sep 18, 2003
    Posts: 916

    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

    Joined: Aug 21, 2004
    Posts: 1855
    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

    Joined: Mar 26, 2002
    Posts: 442
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: B&S: Delete Problem with last row in JTable, getSelectedRow() return wrong row index
     
    Similar Threads
    JTable Columns
    JTable related problem
    jtable
    JTable
    JTable selected row problem