-
October 12th, 2017, 10:26 AM
#1
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.
-
October 12th, 2017, 12:08 PM
#2
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)
-
October 12th, 2017, 01:54 PM
#3
Re: jtable column blues
Originally Posted by 2kaud
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?
-
October 12th, 2017, 04:36 PM
#4
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)
-
October 12th, 2017, 08:17 PM
#5
Re: jtable column blues
Originally Posted by 2kaud
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.
-
October 13th, 2017, 02:58 AM
#6
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)
-
October 13th, 2017, 05:08 AM
#7
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.
-
October 13th, 2017, 08:23 AM
#8
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
-
October 13th, 2017, 09:06 AM
#9
Re: jtable column blues
The column indexes are 0 based, so the 2nd column would be column 1.
-
October 15th, 2017, 12:32 PM
#10
Re: jtable column blues
[QUOTE=Azoth;2218105]
Originally Posted by jcaccia
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.
-
October 15th, 2017, 12:33 PM
#11
Re: jtable column blues
Ok I got rid of the 2nd column,but can’t get rid of the last one .
Any ideas?
-
October 17th, 2017, 03:41 AM
#12
Re: jtable column blues
Can you post the code you have now?
-
October 17th, 2017, 09:03 AM
#13
Re: jtable column blues
Originally Posted by jcaccia
Can you post the code you have now?
The code has been posted previously.
-
October 17th, 2017, 09:04 AM
#14
Re: jtable column blues
The code has been posted previously.
-
October 17th, 2017, 10:14 AM
#15
Re: jtable column blues
Originally Posted by Azoth
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|