在新窗口中创建JTable,作为Java中JMenuItem的操作监听器



我想创建一个包含JTable的新窗口,由于按下JMenuItem,我尝试在动作侦听器内部创建一个新类,不确定这是多么正确。无论如何,它不工作,请建议。

...
help.add(currencyTable);
...
        //action listener for the currency table JMenuItem button
        currencyTable.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                class currencyJTableClass extends JFrame
                {
                    JTable currencyTable;
                    public currencyJTableClass()
                    {
                        setLayout(new FlowLayout());
                        String[] headLine = {"x","y","z"} ;
                        String [][] currencyData =
                                {
                                        {
                                          "a","b","c"
                                        },
                                        {
                                          "d","e","f"
                                        },
                                };
                        currencyJTable = new JTable(currencyData,headLine);
                    }

                } 

看来你已经决定要走这条路了,你有很多选择…

艰难的路…

public void actionPerfomed(ActionEvent e) {
    TableModel model = //... Create the new model based on you needs
    JTable table = new JTable(model);
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(table));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

这将要求您在每个操作侦听器中重复此代码…代码太多了…

稍微容易

创建一个简单的实现,将所有需要的逻辑包装到几个基本类中…

public class ModelPane extends JPanel {
    private JTable table;
    public ModelPane(TableModel model) {
        setLayout(new BorderLayout());
        table = new JTable(model);
        add(new JScrollPane(table));
    }
}
public class ModelFrame extends JFrame {
    public ModelFrame(TableModel model) {
        setLayout(new BorderLayout());
        add(new ModelPane(model));
        pack();
        setLocationRelativeTo(null);
    }
}

然后在你的actionPerformed方法你可以简单地做…

public void actionPerfomed(ActionEvent e) {
    TableModel model = //... Create the new model based on you needs
    ModelFrame frame = new ModelFrame(model);
    frame.setVisible(true);
}

这集中了显示和管理表和数据的核心逻辑

好一点了

使用第二个选项,您可以使用Action API来生成提供生成模型所需信息的基本操作,但允许基本操作类实际确定actionPerformed方法应该如何工作…

public abstract class AbstractModelAction extends AbstractAction {
    public abstract TableModel getModel();
    @Override
    public void actionPerformed(ActionEvent e) {
        ModelFrame frame = new ModelFrame(getModel());
        frame.setVisible(true);
    }
}
public class CurrencyModelAction extends AbstractModelAction {
    @Override
    public TableModel getModel() {
        return //... Create the new model based on you needs
    }
}

查看How use Actions获取更多信息…

结论

使用最灵活和提供最佳重用的方法。我很少创建自定义JFrame s,只是因为我喜欢创建面板,因为这意味着我可以在我想要的任何上下文中重用它们,灵活和可重用;)

不要在ActionListener中创建JFrameJTable。相反,在启动时创建一个单个 JFrame和一个JTable,以及一个TableModel。然后,在您的ActionListener中,用您希望显示的数据更新模型。

当开始学习如何使用表模型时,从DefaultTableModel开始,因为它支持通过使用addRow和removeRow来动态更改模型。

下面是一个例子

注意:如果需要创建一个单独的窗口来显示数据,一些可能性是:

  • 使用模式JDialog —将用户的焦点吸引到窗口
  • 中的内容
  • 使用CardLayout代替—

相关内容

最新更新