1

I am working on a project where I add the column names from mysql into jTable. I want that when a name is added, the column should adjust its size according to the length of column name. I will make myself clearer, but first heres the existing code:

jTextArea1.setText(null);
tableModel.setColumnCount(0);
colwidth = 0;
try {
    Class.forName("java.sql.DriverManager");
    con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:" + GlobalParams.portvar + "/" + (String) jList1.getSelectedValue(), "" + GlobalParams.uservar, "" + GlobalParams.passvar);
    Statement stmnt = (Statement) con.createStatement();
    String query1 = "Use " + GlobalParams.dbvar;
    stmnt.executeQuery(query1);
    String query2 = "desc " + (String) jList2.getSelectedValue();
    ResultSet rs = stmnt.executeQuery(query2);
    while (rs.next()) {
        fieldo = rs.getString("Field");
        jTextArea1.append(fieldo + "\n");
    }
    Scanner scanner = new Scanner(jTextArea1.getText());
    timeget();
    jTextArea4.append(now + ":   " + "/ Scanning Available feeds from table '" + (String) jList2.getSelectedValue() + "' / \n");
    timeget();
    jTextArea4.append(now + ":   " + "/ Getting feeds from table '" + (String) jList2.getSelectedValue() + "' / \n \n");
    if (scanner.hasNextLine()) {
        scanner.nextLine();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            tableModel.addColumn(line);
            jTable1.getColumnModel().getColumn(colwidth).setWidth(line.length());
            colwidth++;
            int end = jTextArea1.getLineEndOffset(0);
            jTextArea1.replaceRange("", 0, end);
        }
    }
} catch (Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());
}

"colwidth" is a public static int and "fieldo" is a public static String.

What i want is, for ex.-> the string to be made into column is "asdadsasd123". I want that the column should adjust its width according to length of this string. I have set the auto resize of table to off. The table is enclosed in a scollpane. The layout of frame is Free Design.

I also did find the thread here-> Auto resizing the JTable column widths but i cant seem to understand this code:

    TableCellRenderer renderer = table.getCellRenderer(row, column);
    Component comp = table.prepareRenderer(renderer, row, column);
    width = Math.max(comp.getPreferredSize().width, width);

(Yeah, I aint very experienced with java). And I also dont want to do anything with rows for now, just resize the columns.

Here is screenshot of Default-> enter image description here

Here is how I want it to be-> enter image description here

Please help,thanks.

Community
  • 1
  • 1
user3271166
  • 573
  • 1
  • 6
  • 17

1 Answers1

1

You can use the Table Column Adjuster.

It will adjust the column width based on the size of the header the cell data or both.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thats indeed the solution, thanks. But can you please simplify the code a little for me? I tried but cant seem to get it )only been a month barely since i started learning java). I want to know how does it adjust columns size dynamically – user3271166 May 12 '14 at 09:48
  • `how does it adjust columns size dynamically` - it uses a `TableModelListener`. The TableModel will generate an event whenever any of the data in the TableModel is changed. The TableModelListener listener listens for these events and then does the appropriate calculations to determine the new column size(s). For example if you change the data in one cell then it only need to potentially adjust a single column. If you add (or remove) a row of data then you need to potentially adjust all the columns. – camickr May 12 '14 at 18:12