• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Adding Column to JTable

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Serio wrote:
That makes sense. Thanks for your help.



You're quite welcome!
 
You didn't tell me he was so big. Unlike this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic