使用 MigLayout,为什么 JTable 后面的 JButton 无响应以及如何解决此问题?

发布于 2024-08-27 00:01:04 字数 2611 浏览 7 评论 0原文

关于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2024-09-03 00:01:04

我不认为这是 MigLayout 问题,本身。该按钮可以在没有行的情况下正常工作

panel.add(table.getTableHeader(), "grow");

您可以尝试将标题/表格组合包装在子面板中:

JPanel sub = new JPanel();
sub.add(table.getTableHeader(), "grow");
sub.add(scrollpane, "grow");
panel.add(sub);

I don't think it's a MigLayout problem, per se. The button works correctly without the line

panel.add(table.getTableHeader(), "grow");

You might try wrapping the header/table combination in a sub-panel:

JPanel sub = new JPanel();
sub.add(table.getTableHeader(), "grow");
sub.add(scrollpane, "grow");
panel.add(sub);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文