-1

我没有找到好的标题,我想对此感到抱歉,

正如你将看到的底部,我已经打电话给

ssframe.add(new JScrollPane(table),BorderLayout.CENTER);

3 次,如果我单击 typeButton 然后 unitButton 它会在屏幕上生成 2 个表格。我希望每个按钮单击一个表,我该怎么做。

    private class searchListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JButton clickd = (JButton)e.getSource();
            if (clickd==typeButton)
                {
                    try
                        {
                            SwingUtilities.updateComponentTreeUI(ssframe);
                            showTable1("type",typefield.getText());
                            ssframe.add(new JScrollPane(table),BorderLayout.CENTER);
                        }catch(Exception a)
                        {
                            a.printStackTrace();
                            JOptionPane.showMessageDialog(null,"Error Code = 0112");
                        }
                }
            else if (clickd==unitButton)
                {
                    try
                        {
                            SwingUtilities.updateComponentTreeUI(frame);
                            showTable1("unit",unitfield.getText());
                            ssframe.add(new JScrollPane(table),BorderLayout.CENTER);
                        }catch(Exception a)
                        {
                            a.printStackTrace();
                            JOptionPane.showMessageDialog(null,"Error Code = 0112");
                        }
                }
            else if (clickd==priceButton)
                {
                    try
                        {
                            SwingUtilities.updateComponentTreeUI(frame);
                            showTable3("price",Integer.parseInt(pricefield.getText()));
                            ssframe.add(new JScrollPane(table),BorderLayout.CENTER);
                        }catch(Exception a)
                        {
                            a.printStackTrace();
                            JOptionPane.showMessageDialog(null,"Error C0de = 0112");
                        }
                }
        }

编辑:它可能会帮助我解决问题的人:

model.setRowCount(0);
model.fireTableDataChanged();
4

1 回答 1

4

有很多方法可以做到这一点。

  1. 您可以使用 维护对 的引用JScrollPane并替换它的视图setViewportView,提供您要显示的表格。将JScrollPane保留在框架上
  2. (最好)更新表格的表格模型,这将替换内容并更新 UI...这意味着JScrollPaneJTable将作为单个参考进行维护,然后您只需更改模型即可。
  3. 另一种解决方案可能是使用 a CardLayout,这将允许您根据需要切换视图
  4. 一个非常非常糟糕的主意是在添加新组件之前删除以前添加的组件......
于 2015-01-03T02:53:58.413 回答