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!

why table getWidth and setWidth is not working when resize column the table

843806Oct 3 2007 — edited Oct 5 2007
hi all,

i want to know why the setWidth is not working in the following code,

try to uncomment the code in columnMarginChanged method and run it wont resize the table.
i cont set width(using setWidth) of the other table column using getWidth of the main table column.

and i want to resize the right side columns only (you can check when you resize the any column the left and right side columns also resizing)

any suggestions could be helpful.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.TableColumnModel;

public class SynTableResize extends JFrame implements TableColumnModelListener, ActionListener
{

    JTable  table1       = new JTable(5, 5);
    JTable  table2       = new JTable(5, 5);
    JTable  table3       = new JTable(5, 5);
    JButton btn          = new JButton("refresh");

    JPanel  pnlcontainer = new JPanel();

    public SynTableResize()
    {

        setLayout(new BorderLayout());
        
        pnlcontainer.setLayout(new BoxLayout(pnlcontainer, BoxLayout.Y_AXIS));
        pnlcontainer.add(table1.getTableHeader());
        pnlcontainer.add(Box.createVerticalStrut(5));
        pnlcontainer.add(table2);
        pnlcontainer.add(Box.createVerticalStrut(5));
        pnlcontainer.add(table3);

        table2.setTableHeader(null);
        table3.setTableHeader(null);

        table1.getColumnModel().addColumnModelListener(this);

        table3.setColumnModel(table1.getColumnModel());
        table2.setColumnModel(table1.getColumnModel());

        table2.getColumnModel().addColumnModelListener(table1);
        table3.getColumnModel().addColumnModelListener(table1);

        btn.addActionListener(this);

        getContentPane().add(pnlcontainer, BorderLayout.CENTER);
        getContentPane().add(btn, BorderLayout.SOUTH);

        setSize(new Dimension(400, 400));
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

    public void columnAdded(TableColumnModelEvent e)
    {

    }

    public void columnMarginChanged(ChangeEvent e)
    {
        TableColumnModel tcm = table1.getColumnModel();
        int columns = tcm.getColumnCount();

        for (int i = 0; i < columns; i++)
        {
            table2.getColumnModel().getColumn(i).setPreferredWidth(tcm.getColumn(i).getWidth());
            table3.getColumnModel().getColumn(i).setPreferredWidth(tcm.getColumn(i).getWidth());
            
            // the following commented code wont work.
//            table2.getColumnModel().getColumn(i).setPreferredWidth(tcm.getColumn(i).getPreferredWidth());
//            table3.getColumnModel().getColumn(i).setPreferredWidth(tcm.getColumn(i).getPreferredWidth());
//            
//            table2.getColumnModel().getColumn(i).setWidth(tcm.getColumn(i).getWidth());
//            table3.getColumnModel().getColumn(i).setWidth(tcm.getColumn(i).getWidth());
        }

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                table2.revalidate();
                table3.revalidate();
            }
        });
    }

    public void columnMoved(TableColumnModelEvent e)
    {
    }

    public void columnRemoved(TableColumnModelEvent e)
    {
    }

    public void columnSelectionChanged(ListSelectionEvent e)
    {
    }

    public void actionPerformed(ActionEvent e)
    {
        JTable table = new JTable(5, 5);

        table.setColumnModel(table1.getColumnModel());
        table.getColumnModel().addColumnModelListener(table1);

        pnlcontainer.add(Box.createVerticalStrut(5));
        pnlcontainer.add(table);
        pnlcontainer.validate();
        pnlcontainer.repaint();
    }
}
thanks
dayananda b v
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2007
Added on Oct 3 2007
4 comments
341 views