0

I have a form stored in both the inputWidget and the outputWidget. The buttons addInput and addOutput will show two different forms in the secondaryInOutPanel.

However there is a significant delay when moving between the form by clicking the buttons. In fact it changes when I attempting to click on the form. And there is still some visible drawings from the pervious form.

I tried using SwingUtilities but that caused the delay to be worst.

    secondaryInOutPanel = new JPanel(new BorderLayout());
    secondaryInOutPanel.setMinimumSize(new Dimension(200,400));     

    JPanel btnPanel = new JPanel();
    outinPanel.add(btnPanel, BorderLayout.NORTH);

    JButton addInput = new JButton("Add Input");
    btnPanel.add(addInput);
    addInput.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            secondaryInOutPanel.removeAll();                                
            secondaryInOutPanel.add(inputWidget, BorderLayout.NORTH);               
            JButton addBtn = new JButton("Save Input");
            secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
            addBtn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub                      
                }                   
            });
        }
    });

    JButton addOutput = new JButton("Add Output");
    btnPanel.add(addOutput);
    addOutput.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            secondaryInOutPanel.removeAll();                            
            secondaryInOutPanel.add(outputWidget, BorderLayout.NORTH);              
            JButton addBtn = new JButton("Save Output");
            secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
            addBtn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub                      
                }                   
            });
        }
    }); 
kyleED
  • 2,327
  • 2
  • 18
  • 23

2 Answers2

2

A better design is to use a Card Layout to hold your input and output panels. Then you can just swap panels as required. The CardLayout will then manage the revalidating and repainting of the panel for you.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • What is the advantage of using the CardLayout as opposed to the repaint method described above? – kyleED Dec 16 '13 at 17:59
  • As I already mentioned the layout does all the work of swapping panels and he panel is sized based on the size of all the panels. – camickr Dec 16 '13 at 18:28
1

You need to make a call to revalidate() and or repaint() on the secondaryInOutPanel after you make changes.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • +1, for the general rule to revalidate() and repaint() when adding/removing components from a visible GUI. However, when swapping entire panels (ie, whenever I see the usage of `removeAll()` method) there is generally a better design. – camickr Dec 16 '13 at 16:43