CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    jtable column blues

    I have a table with 7 columns. I am trying to delete columns 2-6. Here is the code:
    Code:
    for(int col=1;col<6;col++)
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(col));
    It does not get rid of column 2.
    I also get the error
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    I think I can fix this . If not tell me how to.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: jtable column blues

    When you remove a column, the column numbers get changed. So if you remove column 1 then what was column 2 becomes the new column 1, old column 3 becomes column 2 etc and the old last column becomes invalid as it no longer exists. So you always remove the first column in the range you want to delete and iterate for the required number of columns to delete.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    Quote Originally Posted by 2kaud View Post
    When you remove a column, the column numbers get changed. So if you remove column 1 then what was column 2 becomes the new column 1, old column 3 becomes column 2 etc and the old last column becomes invalid as it no longer exists. So you always remove the first column in the range you want to delete and iterate for the required number of columns to delete.
    Uh huh..I didn't think of that about the columns
    Could you give the code for the loop that describes
    this?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: jtable column blues

    I'm not java (I'm c++) so this may not be quite right, but something like

    Code:
    for (int col=2; col <= 6; col++)
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(2));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    Quote Originally Posted by 2kaud View Post
    I'm not java (I'm c++) so this may not be quite right, but something like

    Code:
    for (int col=2; col <= 6; col++)
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(2));
    Ok that gets rid of many columns except the 2nd one.
    I tried variants like
    Code:
    for (int col=3; col <= 6; col++)
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(3));
    But 2nd column still remains ,arg
    Last edited by Azoth; October 12th, 2017 at 11:24 PM.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: jtable column blues

    Do the column numbers start at 1 or 0 (what is the basis for element access to .getColumn()? You may need

    Code:
    for (int col=2; col <= 6; col++)
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(1));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: jtable column blues

    Is it the original column #2 that is not deleted or a column that shows now in the second position after deleting them?
    The correct code should be:
    Code:
    for (int col = 1; col < 6; col++) {
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(1));
    }
    This would leave only one column. Make sure the limit of the for (6) is a constant or a variable with the value of StatsTable.getColumnCount() before you enter the for. If you use
    Code:
    for (int col = 1; col < StatsTable.geColumnCount(); col++ ...
    the column count will be calculated at the end of each loop, and decreases with every deleted column.

  8. #8
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    [QUOTE=jcaccia;2218091]Is it the original column #2 that is not deleted or a column that shows now in the second position after deleting them?
    The correct code should be:
    Code:
    for (int col = 1; col < 6; col++) {
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(1));
    }
    This would leave only one column. Make sure the limit of the for (6) is a constant or a variable with the value of StatsTable.getColumnCount() before you enter the for. If you use
    Code:
    for (int col = 1; col < StatsTable.geColumnCount(); col++ ...
    the column count will be calculated at the end of each loop, and decreases with every deleted

    When I look at the specs of table it says I have 7 columns
    I use Netbeans 8.2 I guess column 1 is the first
    column ? Ok I will try this

  9. #9
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: jtable column blues

    The column indexes are 0 based, so the 2nd column would be column 1.

  10. #10
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    [QUOTE=Azoth;2218105]
    Quote Originally Posted by jcaccia View Post
    Is it the original column #2 that is not deleted or a column that shows now in the second position after deleting them?
    The correct code should be:
    Code:
    for (int col = 1; col < 6; col++) {
        StatsTable.removeColumn(StatsTable.getColumnModel().getColumn(1));
    }
    This would leave only one column. Make sure the limit of the for (6) is a constant or a variable with the value of StatsTable.getColumnCount() before you enter the for. If you use
    Code:
    for (int col = 1; col < StatsTable.geColumnCount(); col++ ...
    the column count will be calculated at the end of each loop, and decreases with every deleted

    When I look at the specs of table it says I have 7 columns
    I use Netbeans 8.2 I guess column 1 is the first
    column ? Ok I will try this
    Last edited by Azoth; October 15th, 2017 at 12:34 PM.

  11. #11
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    Ok I got rid of the 2nd column,but can’t get rid of the last one .
    Any ideas?

  12. #12
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: jtable column blues

    Can you post the code you have now?

  13. #13
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    Quote Originally Posted by jcaccia View Post
    Can you post the code you have now?
    The code has been posted previously.

  14. #14
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: jtable column blues

    The code has been posted previously.

  15. #15
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: jtable column blues

    Quote Originally Posted by Azoth View Post
    The code has been posted previously.
    but it didn't work, and there were a couple of versions with different starting values for col and the argument for .getColumn. According to your post #11 you "got rid of the 2nd column,but can’t get rid of the last one"; if you don't want to show us what you are running now we cannot guess what is happening.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured