Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto resize the widths of JTable's columns dynamically

Tags:

java

jtable

I have a JTable with 3 columns:

- No. #
- Name
- PhoneNumber

I want to make specific width for each column as follows:

enter image description here

and I want the JTable able to update the widths of its columns dynamically if needed (for example, inserting large number in the column #) and keeping same style of the JTable

I solved the first issue, using this code:

myTable.getColumnModel().getColumn(columnNumber).setPreferredWidth(columnWidth);

but I didn't success to make myTable to update the widths dynamically ONLY if the current width of the column doesn't fit its contents. Can you help me solving this issue?

like image 861
Eng.Fouad Avatar asked Jun 23 '11 00:06

Eng.Fouad


People also ask

How to change the width of each column in a JTable?

A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces. By default the width of a JTable is fixed, we can also change the width of each column by using table.getColumnModel ().getColumn ().setPreferredWidth () method of JTable class.

How to add rows and columns to a JTable dynamically?

A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically.

How to change the default column width and row height in swing?

Let’s see how to change the defaults of column width and row height of this sample Swing program: 1. Setting row height for JTable: setRowHeight (int row, int rowHeight): sets the height (in pixels) for an individual row.

How to extend abstracttablemodel and JLabel in JTable?

The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically. The DefaultTableCellRenderer class can extend JLabel class and it can be used to add images, colored text and etc. inside the JTable cell.


2 Answers

Here I found my answer: http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/
The idea is to check some rows' content length to adjust the column width.
In the article, the author provided a full code in a downloadable java file.

JTable table = new JTable( ... );
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

for (int column = 0; column < table.getColumnCount(); column++)
{
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    int preferredWidth = tableColumn.getMinWidth();
    int maxWidth = tableColumn.getMaxWidth();

    for (int row = 0; row < table.getRowCount(); row++)
    {
        TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
        Component c = table.prepareRenderer(cellRenderer, row, column);
        int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
        preferredWidth = Math.max(preferredWidth, width);

        //  We've exceeded the maximum width, no need to check other rows

        if (preferredWidth >= maxWidth)
        {
            preferredWidth = maxWidth;
            break;
        }
    }

    tableColumn.setPreferredWidth( preferredWidth );
}
like image 80
Eng.Fouad Avatar answered Oct 11 '22 12:10

Eng.Fouad


Use the addRow(...) method of the DefaultTableModel to add data to the table dynamically.

Update:

To adjust the width of a visible column I think you need to use:

tableColumn.setWidth(...);
like image 24
camickr Avatar answered Oct 11 '22 12:10

camickr