javaswingjtablelayout-managerswingutilities

swing how to override a component


I didn't find good title I guess sorry for this,

As you will see bottom I've called

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

3 times and if I click the typeButton then unitButton it makes 2 table on the screen. I want to have one table each button clicked, how can I do that.

    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");
                        }
                }
        }

Edit : It might help someone I have fixed problem with :

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

Solution

  • There are a number of ways you could do this.

    1. You could maintain a reference to the JScrollPane and replace it's view using setViewportView, supplying the table you want to show. The JScrollPane would remain on the frame
    2. (Preferably) Update the table model of the table, this will replace the content and update the UI...This means that the JScrollPane and JTable would be maintained as a single reference and you would then simply change the model.
    3. A another solution might be to use a CardLayout, which would allow you to switch the views as needed
    4. A really, really bad idea would be to remove the previously added components BEFORE you add the new components...