使用 MigLayout,为什么 JTable 后面的 JButton 无响应以及如何解决此问题?
关于在 JTable 和 MigLayout 后面使用 JButton,我遇到了一个令人难以置信的问题。它完全没有响应,除非我将它推到 JTable 足够远的地方(然后它可以正常运行)。
我尝试使用我们用于最终用户产品的 MigLayout JAR 版本和最新版本来运行代码;相同的结果。
这是重现问题的示例代码(Main.java):
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
public class Main extends JFrame {
private JPanel panel;
private JTextField textField;
private JButton chooseButton;
private JTable table;
private JButton reloadButton;
private final DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
public Main() {
panel = new JPanel(new MigLayout("debug", "[][grow][]"));
setContentPane(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*** First row ***/
// "File:"
panel.add(new JLabel("File:"));
// textField for filename
textField = new JTextField("No file selected yet!");
textField.setEditable(false);
panel.add(textField, "growx");
// "Choose..." button
chooseButton = new JButton("Choose...");
panel.add(chooseButton, "wrap, sg buttons");
/*** Second row ***/
panel.add(new JLabel());
table = new JTable(model);
model.setColumnIdentifiers(new String[] {"col title"});
JScrollPane scrollpane = new JScrollPane(table);
Dimension scrollpaneDimension = new Dimension(125, 110);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
table.setPreferredScrollableViewportSize(scrollpaneDimension);
table.setFillsViewportHeight(true);
panel.add(table.getTableHeader(), "grow");
panel.add(scrollpane, "grow");
reloadButton = new JButton("Reload");
panel.add(reloadButton, "top, wrap, sg buttons");
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
我想它与表头和表本身最终位于同一布局单元格有关,但我真的不确定这一点。
正如我所说,如果我将按钮推到 JTable 之外足够远的位置,它就会再次工作。如果我把它放在下一行,它就不起作用,我必须将它再向下移动一行。
工作区中运行代码所需的唯一库是 MigLayout。
谢谢大家的帮助,非常感谢!
M·乔尼斯
I am having a mind-boggling problem regarding the use of a JButton following a JTable with MigLayout. It is totally unresponsive unless I push it far enough past the JTable (then it can behave correctly).
I have tried running the code with both the MigLayout JAR of the version we use for end user products and with the very most recent one; same result.
Here is a sample code reproducing the problem (Main.java):
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
public class Main extends JFrame {
private JPanel panel;
private JTextField textField;
private JButton chooseButton;
private JTable table;
private JButton reloadButton;
private final DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
public Main() {
panel = new JPanel(new MigLayout("debug", "[][grow][]"));
setContentPane(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*** First row ***/
// "File:"
panel.add(new JLabel("File:"));
// textField for filename
textField = new JTextField("No file selected yet!");
textField.setEditable(false);
panel.add(textField, "growx");
// "Choose..." button
chooseButton = new JButton("Choose...");
panel.add(chooseButton, "wrap, sg buttons");
/*** Second row ***/
panel.add(new JLabel());
table = new JTable(model);
model.setColumnIdentifiers(new String[] {"col title"});
JScrollPane scrollpane = new JScrollPane(table);
Dimension scrollpaneDimension = new Dimension(125, 110);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
table.setPreferredScrollableViewportSize(scrollpaneDimension);
table.setFillsViewportHeight(true);
panel.add(table.getTableHeader(), "grow");
panel.add(scrollpane, "grow");
reloadButton = new JButton("Reload");
panel.add(reloadButton, "top, wrap, sg buttons");
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
I suppose it has something to do with the table header and the table itself ending up in the same layout cell, but I'm really not sure of this.
As I said, if I push the button far enough past the JTable, it will work again. If I drop it on the next row, it doesn't work, I have to move it down one more row.
The only library you need in your workspace to run the code is MigLayout.
Thank you all for your help, much appreciated!
M. Joanis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为这是 MigLayout 问题,本身。该按钮可以在没有行的情况下正常工作
您可以尝试将标题/表格组合包装在子面板中:
I don't think it's a MigLayout problem, per se. The button works correctly without the line
You might try wrapping the header/table combination in a sub-panel: