Java-根据列大小调整表的大小
我有一个包含两列的Java-根据列大小调整表的大小,java,swing,jtable,Java,Swing,Jtable,我有一个包含两列的JTable,我试图实现以下目标: 如果第一列单元格的值不适合单元格内部,则可以看到3个结束点。在这种情况下,我希望调整列和表的大小,以便长值合适,以便不更改第二列的宽度。将其视为第一列向左侧的展开 示例代码: /** * Main. * * @param args arguments. */ public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefau
JTable
,我试图实现以下目标:如果第一列单元格的值不适合单元格内部,则可以看到3个结束点。在这种情况下,我希望调整列和表的大小,以便长值合适,以便不更改第二列的宽度。将其视为第一列向左侧的展开 示例代码:
/**
* Main.
*
* @param args arguments.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy Lathy Alberta 1234567890 11 12 13 14", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
// Create the table based on data and column names
JTable table = new JTable(new DefaultTableModel(data, columnNames));
// Set an initial size and add it to the main panel
table.setSize(300, 300);
panel.add(table);
// Compute the width of the first column, based on the longest value
int width = 0, row = 0;
for (row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, 0);
Component comp = table.prepareRenderer(renderer, row, 0);
width = Math.max (comp.getPreferredSize().width, width);
}
// Compute the value to be added to the width of the table
int initialWidth = table.getColumnModel().getColumn(0).getPreferredWidth();
int delta = width - initialWidth;
// Try to resize the table and the first column
table.setPreferredSize(new Dimension(table.getPreferredSize().width + delta, table.getHeight()));
table.getColumnModel().getColumn(0).setPreferredWidth(width);
frame.getContentPane().add(panel);
panel.setLayout(new BorderLayout());
panel.setPreferredSize(table.getPreferredSize());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(800, 300);
frame.setVisible(true);
}
/**
*梅因。
*
*@param args参数。
*/
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=newjpanel(newgridbagloayout());
String[]columnNames={“名字”,
“姓氏”};
对象[][]数据={
{“Kathy Lathy Alberta 1234567890 11 12 13 14”,“Smith”},
{“约翰”,“多伊”},
{“苏”,“黑”},
{“Jane”,“White”},
{“乔”,“布朗”}
};
//基于数据和列名创建表
JTable table=新的JTable(新的DefaultTableModel(数据、列名));
//设置初始尺寸并将其添加到主面板
表.设置尺寸(300300);
面板。添加(表);
//根据最长值计算第一列的宽度
整数宽度=0,行=0;
对于(行=0;行
结果是:
几个问题:
将面板的布局管理器设置两次,首先设置为GridBagLayout,然后设置为BorderLayout。在开始向配电盘添加元件之前,应设置布局管理器一次
width = Math.max (comp.getPreferredSize().width, width);
首选宽度将太小,因为该表还包括列宽度中的单元格间距量。对于简单的解决方案,您可以使用:
width = Math.max (comp.getPreferredSize().width + 1, width);
您可以查看我用来调整列宽的更通用的代码
// Compute the value to be added to the width of the table
// int initialWidth = table.getColumnModel().getColumn(0).getPreferredWidth();
// int delta = width - initialWidth;
// Try to resize the table and the first column
// table.setPreferredSize(new Dimension(table.getPreferredSize().width + delta, table.getHeight()));
table.getColumnModel().getColumn(0).setPreferredWidth(width);
不要尝试使用首选的桌子宽度。只需设置TableColumn的宽度,让表计算它自己的宽度。
JTable
确实应该包装在JScrollPane
中,有关更多详细信息,请参阅