JMenuBar中的按钮无法更改JTable中的数据

发布于 2025-01-01 05:04:22 字数 3799 浏览 1 评论 0原文

我正在使用 swing 用 java 编写应用程序,它使用 JTable 来显示文件中的数据。我创建了 ActionListener 来处理在 JTable 中添加新数据的过程。在我的应用程序中,JButton 使用这个 ActionListener,并且它工作得很好。但是当我将相同的 ActionListener 绑定到 JMenuBar 中的按钮时,有些东西不起作用。有一些操作 - 应用程序从文件加载数据,但 JTable 根本没有改变。会是什么? 代码:

    //button that works. It locates in constructor
    ...
    b1.addActionListener(bl);
    ...
    //button that loads data from file, but can't update table
    ...
        public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;

        // Create the menu bar.
        menuBar = new JMenuBar();

        // Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(menu);

        // a group of JMenuItems
        menuItem = new JMenuItem("import...", KeyEvent.VK_T);
        // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
                ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("add file...");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("open catalog");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(bl);

        menu.add(menuItem);

        menuItem = new JMenuItem("save catalog");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(b2l);
        menu.add(menuItem);

        // a group of radio button menu items
        menu.addSeparator();

        // a submenu
        menu.addSeparator();
        submenu = new JMenu("A submenu");
        submenu.setMnemonic(KeyEvent.VK_S);

        menuItem = new JMenuItem("An item in the submenu");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
                ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        submenu.add(menuItem);

        menuItem = new JMenuItem("Another item");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);

        // Build second menu in the menu bar.
        menu = new JMenu("Edit");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "This menu does nothing");
        menuBar.add(menu);

        return menuBar;
    }
...
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Media Cataloguer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MainInterface dm = new MainInterface();
        frame.setJMenuBar(dm.createMenuBar());
...



    ...
    //mutual ActionListener
    class ButtonListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                        //loading of array with data
                CatalogueLoader loader = new CatalogueLoader("test.ctlg");
                try {
                    Object[][] ctlgData = loader.readCtlg();

                    for (int i = 0; i < loader.size; i++) {

                        for (int j = 0; j < 5; j++) {

                            table.setValueAt(ctlgData[i][j], i, j);

                        }

                    }

                } catch (IOException ex) {

                }
            }
        }
    private ButtonListener bl = new ButtonListener();

I'm writting application in java with use of swing, that uses JTable for dispaying the data from file. I created ActionListener to handle the process of adding of new data in JTable. In my app, JButton uses this ActionListener, and it works perfectly. But when i tie the same ActionListener to button in JMenuBar, something doesn't work. There is some action - apps loads data from file, but JTable doens't change at all. What can it be?
Code:

    //button that works. It locates in constructor
    ...
    b1.addActionListener(bl);
    ...
    //button that loads data from file, but can't update table
    ...
        public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;

        // Create the menu bar.
        menuBar = new JMenuBar();

        // Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(menu);

        // a group of JMenuItems
        menuItem = new JMenuItem("import...", KeyEvent.VK_T);
        // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
                ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("add file...");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("open catalog");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(bl);

        menu.add(menuItem);

        menuItem = new JMenuItem("save catalog");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(b2l);
        menu.add(menuItem);

        // a group of radio button menu items
        menu.addSeparator();

        // a submenu
        menu.addSeparator();
        submenu = new JMenu("A submenu");
        submenu.setMnemonic(KeyEvent.VK_S);

        menuItem = new JMenuItem("An item in the submenu");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
                ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        submenu.add(menuItem);

        menuItem = new JMenuItem("Another item");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);

        // Build second menu in the menu bar.
        menu = new JMenu("Edit");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "This menu does nothing");
        menuBar.add(menu);

        return menuBar;
    }
...
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Media Cataloguer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MainInterface dm = new MainInterface();
        frame.setJMenuBar(dm.createMenuBar());
...



    ...
    //mutual ActionListener
    class ButtonListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                        //loading of array with data
                CatalogueLoader loader = new CatalogueLoader("test.ctlg");
                try {
                    Object[][] ctlgData = loader.readCtlg();

                    for (int i = 0; i < loader.size; i++) {

                        for (int j = 0; j < 5; j++) {

                            table.setValueAt(ctlgData[i][j], i, j);

                        }

                    }

                } catch (IOException ex) {

                }
            }
        }
    private ButtonListener bl = new ButtonListener();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你的笑 2025-01-08 05:04:22

您没有向 b1 和 menuItem 添加相同的侦听器。我从你的代码中复制了这个:

b1.addActionListener(bl);
menuItem.addActionListener(this);

You're not adding the same listener to the b1 and menuItem. I copied this from your code:

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