- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个包含许多列的 JTable。我想要调整特定列的大小。我希望通过使用 setPreferredWidth,列将调整到该大小,或者内容的大小,这样不会发生截断,并让其余列占用剩余空间,但是,所有列,包括我调整大小的列,均等地分割了表格的所有空间;好像 setPreferredWidth 什么都没做。实际上,我希望能够设置列的宽度并将其缩小到该大小而不截断内容(我是不是强调得太多了?)以这样一种方式所有列尚未调整大小的填充剩余空间。使用 setMaxWidth 会截断内容(我有没有提到我不喜欢那样?)如何调整/缩小列的大小而不截断它并且它不做绝对没有?这是有问题的代码:
for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++)
if ((x = model.getColumnWidth(i)) > -1)
table.getColumnModel().getColumn(i).setPreferredWidth(x);
表格在一个 JPanel (MyListPanel - BorderLayout) 中,它在另一个 JPanel (GridBagLayout) 中添加:
new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 0, 0, 2), 0, 0))
编辑:这是我的 JPanel 子类的构造函数:
public MyListPanel(boolean showHeader, String title, ColData...columns) {
super(new BorderLayout());
model = new MyListTableModel(columns);
table = new JTable(model);
table.addFocusListener(this);
add(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setTitle(title);
if (showHeader)
add(table.getTableHeader(), BorderLayout.NORTH);
for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++)
if ((x = model.getColumnWidth(i)) > -1)
table.getColumnModel().getColumn(i).setPreferredWidth(x);
setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
}
和 MyListTableModel.ColData:
public static class ColData {
private int index;
private String title;
private int width;
public ColData(int _index, String _title, int _width) { index = _index; title = _title; width = _width; }
}
最佳答案
包括:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
关于java - TableColumn setPreferredWidth 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7782609/
我有一个像这样的图片的表格,它不包含任何数据 所以我想删除发送列后的空格,并且我正在使用 table.getColumnModel().getColumn(0).setPreferredWidth(5
这是我的代码 JTable table = new JTable(attributeValues, attributeNamesString); table.setAutoResizeMode(JTa
我有一个 JTable,其中显示了 AbstractTabelModel。我尝试在项目中创建一个 void 方法,将每列的宽度设置为列中最长值的长度。这是我现在使用的方法,其中“accountWind
我有一个包含许多列的 JTable。我想要调整特定列的大小。我希望通过使用 setPreferredWidth,列将调整到该大小,或者内容的大小,这样不会发生截断,并让其余列占用剩余空间,但是,所有列
编辑:根据 camickr 的建议,我将 JTable 添加到滚动 Pane 中。看来他/她认为布局管理器正在改变列的大小是正确的。 我有一个包含 50 行和 40 列的 JTable。每个单元格要么
我是一名优秀的程序员,十分优秀!