Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JXTable and column widths

luppyFeb 11 2011 — edited Feb 14 2011
Hello

I am trying to set the column widths using JXTable and a ColumnFactory.
I set the preferred width of the middle column in my example to 10 but when executed that column is slightly smaller that the others but not 10. If I set the resize mode to AUTO_RESIZE_OFF (uncomment line in example) the correct width of 10 is produced.

I would like the middle column to be 10 and then other columns that have not had a width specified by the overridden logic to then allocate the extra space between them.

If I uncomment the line for max width then the column understandably is reduced to a size of 10 I would still like the column to be made bigger and just have an initial size of 10.

Thanks in advance for any help or pointers to help solve my problem
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.ColumnFactory;
import org.jdesktop.swingx.table.TableColumnExt;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;

public class ExampleColumnWidth extends JFrame
{
    private final static int THIN_COLUMN_INDEX = 1;

    public ExampleColumnWidth()
    {
        JXTable table = new JXTable();
        table.setColumnFactory(new MyColumnFactory());
        table.setModel(new MyTableModel());

//        table.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);

        setLayout(new BorderLayout());
        add(new JScrollPane(table), BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new ExampleColumnWidth();
    }

    private class MyColumnFactory extends ColumnFactory
    {
        @Override
        public void configureColumnWidths(JXTable table, TableColumnExt columnExt)
        {
            int modelIndex = columnExt.getModelIndex();
            switch (modelIndex)
            {
                case THIN_COLUMN_INDEX:
                    columnExt.setPreferredWidth(10);
//                    columnExt.setMaxWidth(10);
                    break;
            }
        }
    }

    private class MyTableModel extends AbstractTableModel
    {
        public int getRowCount()
        {
            return 5;
        }

        public int getColumnCount()
        {
            return 3;
        }

        @Override
        public String getColumnName(int column)
        {
            switch (column)
            {
                case THIN_COLUMN_INDEX:
                    return "T";
                default:
                    return "Normal";
            }


        }

        public Object getValueAt(int rowIndex, int columnIndex)
        {
            return "";
        }
    }
}
Edited by: 835936 on Feb 11, 2011 5:51 AM

Edited by: 835936 on Feb 11, 2011 5:55 AM
This post has been answered by kleopatra-JavaNet on Feb 12 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 14 2011
Added on Feb 11 2011
3 comments
534 views