D
D
Dmitry072016-07-26 21:14:54
Java
Dmitry07, 2016-07-26 21:14:54

How to properly set column widths in Swing?

Good day.

I am studying an example of a table in Swing, and the following question arose:

lVf7Uyl.png

The table is presented as a DefaultTableModel object, however, an attempt to set the width of the columns: did not lead to success, and the import required for these methods:

infoTable.getColumn(1).setPreferredWidth(150);


import javax.swing.table.TableModel;
import javax.swing.table.TableColumnModel;


ended with the message 'unused import'.

What is the correct way to set the column widths?

Thanks to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry07, 2016-07-26
@Dmitry07

TOB BOT , thank you very much.
I finally found a working version - precisely through the mvc model.

infoTableModel.getColumnModel().getColumn(0).setPreferredWidth(150);

T
TOB BOT, 2016-07-26
@TOBBOT

setting the width is done through the ColumnModel.

for (int i = 0; i <table.getColumnCount(); i++) {
    table.getColumnModel().getColumn(i).setMinWidth(20);
    table.getColumnModel().getColumn(i).setMaxWidth(600);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question