在我的应用程序中,我有一个两列表,它将在程序的生命周期中动态填充。我有一个名为updateTableWidths
的方法,在调用时,执行以下操作:
它似乎工作得很好,但是出于一些非常奇怪的原因,它需要被称为两次(但仅在某些情况下,即当表宽度增长而不是缩小时)。下面的SSCCE说明了我的困境。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
@SuppressWarnings("serial")
public class TableResizeTest extends JComponent {
private JFrame frame;
private JScrollPane scrollPane;
private DefaultTableModel tableModel;
private JTable table;
private JPanel panel;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableResizeTest window = new TableResizeTest();
window.frame.setVisible(true);
window.frame.requestFocusInWindow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TableResizeTest() {
initialize();
}
private void initialize() {
frame = new JFrame("Test");
frame.setBounds(300, 300, 200, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tableModel = new DefaultTableModel(new Object[]{"Col_1", "Col_2"},0);
table = new JTable(tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
table.setAlignmentY(Component.TOP_ALIGNMENT);
table.setAlignmentX(Component.LEFT_ALIGNMENT);
table.setBorder(new EmptyBorder(0, 0, 0, 0));
table.setFillsViewportHeight(true);
table.setTableHeader(null);
table.setEnabled(false);
table.getColumnModel().setColumnMargin(0);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton btnFillTableShort = new JButton("Fill table short");
btnFillTableShort.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(btnFillTableShort);
JButton btnFillTableLong = new JButton("Fill table long");
btnFillTableLong.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(btnFillTableLong);
JButton btnUpdateTableWidths = new JButton("Update table widths");
btnUpdateTableWidths.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(btnUpdateTableWidths);
btnUpdateTableWidths.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateTableWidths();
}
});
btnFillTableLong.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fillTableLong();
}
});
btnFillTableShort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fillTableShort();
}
});
scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(200, 100));
scrollPane.setMaximumSize(new Dimension(32767, 100));
frame.getContentPane().add(scrollPane);
scrollPane.setAlignmentY(Component.TOP_ALIGNMENT);
scrollPane.setBorder(new LineBorder(new Color(0, 0, 0)));
frame.pack();
}
public void fillTableShort() {
tableModel.setRowCount(0);
tableModel.addRow(new Object[]{"short", "short"});
}
public void fillTableLong() {
tableModel.setRowCount(0);
tableModel.addRow(new Object[]{"looooooooooooooooooooong", "looooooooooooooooooooong"});
}
public void updateTableWidths() {
int col_1_width = 0;
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, 0);
Component comp = table.prepareRenderer(renderer, row, 0);
col_1_width = Math.max (comp.getPreferredSize().width, col_1_width);
}
col_1_width += table.getIntercellSpacing().width;
col_1_width += 10;
table.getColumn("Col_1").setMinWidth(col_1_width);
table.getColumn("Col_1").setMaxWidth(col_1_width);
System.out.println("col_1_width was set to " + col_1_width);
int col_2_width = 0;
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, 1);
Component comp = table.prepareRenderer(renderer, row, 1);
col_2_width = Math.max (comp.getPreferredSize().width, col_2_width);
}
col_2_width += table.getIntercellSpacing().width;
int tableWidth = col_2_width + col_1_width;
if (scrollPane.getVerticalScrollBar().isVisible()) {
tableWidth += scrollPane.getVerticalScrollBar().getWidth();
}
if (tableWidth > scrollPane.getWidth()) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
System.out.println("Auto resize was set to AUTO_RESIZE_OFF");
} else {
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
col_2_width = scrollPane.getWidth() + col_1_width;
System.out.println("Auto resize was set to AUTO_RESIZE_LAST COLUMN");
}
table.getColumn("Col_2").setPreferredWidth(col_2_width);
table.getColumn("Col_2").setMinWidth(col_2_width);
System.out.println("col_2_width was set to " + col_2_width + "\n");
}
}
以下是重现行为可以遵循的一系列步骤:
1)启动应用
2)点击“填表短”
按钮
3)单击“更新表格宽度”按钮(在这种情况下只需单击一次)
4)点击“长桌填充”按钮
5)单击“更新表格宽度”按钮(第一次点击)
6)单击“更新表格宽度”按钮(第二次点击)
第二次点击后,它会正确显示。我有一些打印到控制台的调试信息,对于第一次和第二次点击,该方法似乎做了完全相同的事情:
col_1_width was set to 152
Auto resize was set to AUTO_RESIZE_OFF
col_2_width was set to 142
因为在第一次运行期间发生了某些变化,第二次发生了一些明显的差异。正如我之前提到的,这只发生在表列增长而不是缩小时。
我真的很难过。有什么想法吗?
答案 0 :(得分:4)
您可能需要为第一列指定首选大小,以便与滚动一起正常工作。尝试添加以下行:
table.getColumn("Col_1").setPreferredWidth(col_1_width);
在Col_1
方法中处理updateTableWidths
计算的块中。它解决了发布的SSCCE中的问题。
编辑:
setPreferredWidth
中的maxWidth和minWidth存在依赖关系。以下是setPreferredWidth
的代码。所以订单可能很重要。
public void setPreferredWidth(int preferredWidth) {
int old = this.preferredWidth;
this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth);
firePropertyChange("preferredWidth", old, this.preferredWidth);
}
同时探索setMaxWidth
表示它也可以设置首选宽度:
public void setMaxWidth(int maxWidth) {
int old = this.maxWidth;
this.maxWidth = Math.max(minWidth, maxWidth);
if (width > this.maxWidth) {
setWidth(this.maxWidth);
}
if (preferredWidth > this.maxWidth) {
setPreferredWidth(this.maxWidth);
}
firePropertyChange("maxWidth", old, this.maxWidth);
}
答案 1 :(得分:3)
尝试添加TableColumn#setWidth
和/或TableColumn#setPreferredWidth
即
table.getColumn("Col_1").setWidth(col_1_width);
table.getColumn("Col_1").setPreferredWidth(col_1_width);
//...///
table.getColumn("Col_2").setWidth(col_2_width);
table.getColumn("Col_2").setPreferredWidth(col_2_width);