I have a JTable with some fields with long text and some with little text.
By default, this is how it looks:

Notice how the “Name” and “Title” rows are not shortened.
I would like to know two things:
- How to manually set the width of Headers(Such as Name, Title, Surname)
- How to automatically resize all of them depending on the text.
Here is the code:
String[] columnNames = {"Name", "Title", "Surname", "ID"};
Object[][] testData = {
{"John", "Mr.", "Doe", "61020384U"},
};
nameTable = new JTable(testData, columnNames);
nameTable.setFont(new Font("Tahoma", Font.PLAIN, 11));
window.add(new JScrollPane(nameTable));
I have looked at the Swing Tutorials but I either must have missed something or it isn’t there.
I think that by defalut is there 80pixels, but you can change that with follows, for example …
EDIT
to your second question is better look here, please there are lots of usefull examples about TableColumn