How do I set the column width for selected columns in a JTable

For this line --->  table.getColumn(2).setPreferredWidth(10);
i am getting this ERROR --->>
Exception in thread "main" java.lang.IllegalArgumentException: Identifier not found
      at javax.swing.table.DefaultTableColumnModel.getColumnIndex(Unknown Source)
      at javax.swing.JTable.getColumn(Unknown Source)
      at browseSelect.main(

JTable table = new JTable(browseList, headersV);
               JScrollPane scrolls = new JScrollPane(table,

Open in new window

Vanavah EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


look here:

int rows = 3;
int cols = 3;
JTable table = new JTable(rows, cols);

// Disable auto resizing

// Set the first visible column to 100 pixels wide
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
int width = 100;

Open in new window

But befire you do anything - popuate your table and look at it.
Why do you want to chaneg column size?
In majority of tables you dont need to do it
As you see to do it - you need to disable autoaddjustment - so you'll take a lot of unneccassry responsibilities upon yourself - let
swing do its job - it ill in most case do a better job
you can look here:
but as you see it is rather painful

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.