• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Column to JTable

 
David Serio
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to add a column to a JTable. I am able to do this with one section of code but not sure why the second section of code does not work.

Working (iTable is of type JTable):



Not working:


Error:

.\ItemTable.java:23: 'void' type not allowed here
iTable.addColumn((new TableColumn()).setHeaderValue("Item"));
^
1 error


The ^ is pointing to ("Item").....

I'm not sure why I am getting this error. It looks like I am collapsing the working code into one line, but obviously I'm doing something wrong. Any ideas?

Thanks.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Serio wrote:
Not working:


Error:

.\ItemTable.java:23: 'void' type not allowed here
iTable.addColumn((new TableColumn()).setHeaderValue("Item"));
1 error



You're trying to chain your methods in a way that is not possible. If setHeaderValue(...) returned the TableColumn object that is calling this method, then you're golden, but if you go to the TableColumn API and look up the setHeaderValue() method, you'll see that it doesn't, it returns void. So what you are doing here is by trying to save a few lines you end up trying to pass void as the parameter to your addColumn method, and this just won't work.

Solution: just don't do it.
 
Gerbrand van Dieijen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Serio wrote:
Not working:




As stated in the javadoc on JTable
the addColumn method has void return type.
So a corect syntax could be:

 
David Serio
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
David Serio wrote:
Not working:


Error:

.\ItemTable.java:23: 'void' type not allowed here
iTable.addColumn((new TableColumn()).setHeaderValue("Item"));
1 error



You're trying to chain your methods in a way that is not possible. If setHeaderValue(...) returned the TableColumn object that is calling this method, then you're golden, but if you go to the TableColumn API and look up the setHeaderValue() method, you'll see that it doesn't, it returns void. So what you are doing here is by trying to save a few lines you end up trying to pass void as the parameter to your addColumn method, and this just won't work.

Solution: just don't do it.



That makes sense. Thanks for your help.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Serio wrote:
That makes sense. Thanks for your help.


You're quite welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!