Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable column question

 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm practicing examples using JTables and am wondering what one line of code refers.
i.e.
TableColumn column=null
for (int i = 0; i<5; i++)
{
column = numberTable.getcolumnModel().getColumn(i);
column.setPreferredWidth(myTableModel.getColumnWidth(i) );
}
I am wondering what the for loop code refers to directly when it comes to determining columns.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for loop runs through each column in the table. This code will make a table with 5 columns, nicely modeled.
 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. My confusion originally stemmed from the fact that there is a getColumnCount() method that created columns for the column model and that that count was not the same as in the for loop. So now I am wondering how the for loop is represented. Does the for loop mean that there are 5 columns in the table model that I can put 5 column models in? Unfortunately my book doesn't do a thourough job explaining this concept at this point.
Regards, Michael
Originally posted by Cindy Glass:
The for loop runs through each column in the table. This code will make a table with 5 columns, nicely modeled.

 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm not incorrect here, but I think the above code just sets to preferred column size of the first five columns.
This morning I needed to set the columns size to a particular size, not knowing the number of columns:
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I am a bit confused. What exactly are you trying to do here?
You have a numberTable and that has a columnModel from which you get the first 5 column from successively.
Then you go to a completely different model called myTableModel and get the width of the corresponding column and set the numberTable column to that width.
When you say that there is a getColumnCount() method that created the columns for the column model, which model are you talking about? The model of the numberTable or the myTableModel which might have a different column count?
You really don't show enough code for me to understand.
 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That resolves my issue. I was a bit confused because the book I was using had a different column number for the Table Column vs. how many were in the overall Table.
Thanks...

Originally posted by Michael Hildner:
[B]I hope I'm not incorrect here, but I think the above code just sets to preferred column size of the first five columns.
This morning I needed to set the columns size to a particular size, not knowing the number of columns:
[/B]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic