0

I would like to know how to get the column width after a user changes it so that i may save it off.

after messing around i came up with this method but it's not perfect. if the user doesn't actually select a column by clicking in a row of the column, the selected column is -1 thus throws an error so i prevented that with an if statement.

the next issue is, if i select a column then change the width, it works and prints out the width as i change the column, but if i selected say column 3 but then proceed to change column 2 width without selecting in row in that column 2, it will continue to spit out the width of column 3 as i change column 2. so its working as expected with a select method, but there has to be a better way to get the width of the column i'm changing.

any ideas?

jt.getColumnModel().addColumnModelListener(new TableColumnModelListener() {

        public void columnAdded(TableColumnModelEvent e) {
            // TODO Auto-generated method stub

        }

        public void columnRemoved(TableColumnModelEvent e) {
            // TODO Auto-generated method stub

        }

        public void columnMoved(TableColumnModelEvent e) {
            // TODO Auto-generated method stub

        }

        public void columnMarginChanged(ChangeEvent e) {
            int col = jt.getSelectedColumn();
            if (col >= 0) {
                TableColumn tc = jt.getColumnModel().getColumn(col);
                System.out.println(tc.getWidth());
            }
        }

        public void columnSelectionChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub

        }

    });
Kyle
  • 89
  • 9
  • What is the `source` of the `ChangeEvent`? Hopefully it is the actual column that changed... – hooknc May 28 '19 at 15:44
  • yes it is but how do i get its width from the source change? its not like i can do e.getwidth(). im still a novice at java lol – Kyle May 28 '19 at 16:12
  • If the object is a column, you can cast the incoming object to a column and then call all the methods you normally would on a column. – hooknc May 28 '19 at 16:35
  • This question might be a duplicate. Please see this answer when you have a moment: https://stackoverflow.com/questions/8752694/java-jtable-detect-column-re-sized-by-user – hooknc May 29 '19 at 16:57

2 Answers2

0

You could try casting the source of the ChangeEvent to a TableColumn.

When you get a moment, try the following in your TableColumnModelListener...

jt.getColumnModel().addColumnModelListener(new TableColumnModelListener() {

        public void columnAdded(TableColumnModelEvent e) {
            // TODO Auto-generated method stub
        }

        public void columnRemoved(TableColumnModelEvent e) {
            // TODO Auto-generated method stub
        }

        public void columnMoved(TableColumnModelEvent e) {
            // TODO Auto-generated method stub
        }

        public void columnMarginChanged(ChangeEvent e) {

            Object source = e.getSource();
            if (source instanceof TableColumn) {

                TableColumn tableColumn = (TableColumn) source;

                System.out.println(tableColumn.getWidth());
            }
        }

        public void columnSelectionChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
        }
    });
hooknc
  • 4,854
  • 5
  • 31
  • 60
  • this did not work for me, which leads me to think that my source is not what i expected it to be. the select method was working when i changed a column width on my table, but this code is not doing anything. so how do i know if my source is the column being changed? – Kyle May 28 '19 at 18:32
  • Print out the object that is returned from the `ChangeEvent`. `System.out.println(e.getSource());` and see what the object is. Hopefully it is fairly obvious. – hooknc May 28 '19 at 19:43
  • so this is what i got: javax.swing.table.DefaultTableColumnModel@322f59ba did it not work because it is an instance of a default table column model? im not sure what i need to do at this point ive tried a few other things with no luck. i just don't know enough at this point in time – Kyle May 29 '19 at 01:31
0

I managed to find a solution a while back, but forgot to share. I am storing all the column widths every time the mouse is released from resizing on the table header.

jt.getTableHeader().addMouseListener( new MouseAdapter() {
        
        public void mouseReleased(MouseEvent arg0){
            for(int i=0;i<jt.getColumnModel().getColumnCount();i++ ) {
                TableColumn column = jt.getColumnModel().getColumn(i);
                int tableColWidth = column.getWidth();
                String colHeader = (String) column.getHeaderValue();
                
                //Store the column Header with the column width
            }
        }
    });
Kyle
  • 89
  • 9