0
votes

In my program it is possible to re-size the columns of a jTable. When the jTable is built the columns are automatically sized by length. But sometimes I want to change the size of a column because it is more readable when the table is printed on paper.

It is possible to show an icon like: icon.

Now only the mouse is shown when re-sizing the columns

Does anyone if this is possible and how to do this?

Code of the table:

        private JTable table;
    table = new JTable(model);
    table.setBounds(1, 1, 450, 0);
    JScrollPane scrollPane = new JScrollPane(table);
    panel_10.add(scrollPane);
    JTableHeader tableHeader = table.getTableHeader();
    tableHeader.setResizingAllowed(true);

Maybe it is someting with my DefaultTableModel?

            DefaultTableModel model = new DefaultTableModel();

        Statement stmt2 = connection.createStatement();
        ResultSet rs2 = stmt2.executeQuery("SELECT * FROM machinecodes WHERE type = '" + type +"'");

        while(rs2.next()){
       for(int i = 1; i < columns ; i++)
       {
            if(rs2.getString(1+i)==null) break;
            model.addColumn(rs2.getString(1+i));
       }
        }
        }
        else
        {
            for(int i = 1 ; i <= 25 ; i++)
                model.addColumn(i);

        }
        // Fetch each row from the result set
             while (rs.next()) {
                Object[] o = new Object[columns];
            // Get the data from the row using the column index
                for(int i = 0; i < columns ; i++)
            o[i]=rs.getString(i+1);

                            model.insertRow(0, o);
             }      
1
please consider to cleanup the code formatting, it's near-to unreadable as is. (Note that this site's formatter doesn't play nicely with tabs .. but alas, we shouldn't be using tabs anyway in our code :) - kleopatra

1 Answers

2
votes

Retrieve the JTableHeader and allow resizing:

JTable yourTable;
...
JTableHeader tableHeader = yourTable.getTableHeader();
tableHeader.setResizingAllowed(true);