简体   繁体   English

在Java中设置JTable中的列宽

[英]Setting column width in JTable in Java

I have created a JTable that retrieves some data from the MySQL database to 2 columns.. For the 2nd column width is not enough.. so i coded below method(Also Includes the data retrieval code from the database)and called it. 我创建了一个JTable,它从MySQL数据库中检索一些数据到2列。对于第二列宽度是不够的..所以我编码下面的方法(还包括数据库中的数据检索代码)并调用它。 But the column width is not effected. 但是柱宽不受影响。 What is the error here? 这里有什么错误?

private void updateTable(){

    try {
        //getting data from the mysql database
        String sql = "SELECT ItemID,ItemName FROM druginfo";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        list_table.setModel(DbUtils.resultSetToTableModel(rs));
        // resizing the column width
       list_table.getColumnModel().getColumn(0).setPreferredWidth(20);
       list_table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Error : "+ex);
    }

}
list_table.getColumnModel().getColumn(0).setPreferredWidth(20);

First, you only set PreferredWidth to your first column not the 2nd. 首先,您只将PreferredWidth设置为第一列而不是第二列。 Do you also want preferredWidth for 2nd? 你还希望preferredWidth为2nd吗? Than you need to call it to the 2nd column. 比你需要把它叫到第二列。

Anyhow setting the width for a column is using function setWidth() 无论如何设置列的宽度是使用函数setWidth()

For 2nd column it is: 对于第二列,它是:

columnModel.getColumn(1).setWidth(100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM