0

I tried

frame.removell();
frame.revalidate();
frame.repaint();

It didn't worked. Then I tried

frame.getContentPane().removeAll();
frame.revalidate();
frame.repaint();

But this isn't working also. I am simply trying to remove all the components from existing frame and reload the same frame with Same components with different values but with the above codes, application is simply showing duplicate panels in my existing frame.

pah
  • 4,700
  • 6
  • 28
  • 37
Naseem
  • 911
  • 5
  • 18
  • 48
  • Apologies. I did tried with the above commands as well but it didn't work. I have updated my question. – Naseem Jul 23 '16 at 11:40
  • Do you add the components directly to the frame or do you add them to a panel that gets added to the frame ? My guess is that you're adding duplicate components to a panel not to the frame itself. – Titus Jul 23 '16 at 12:01
  • I am adding components to the panel and then adding those panels in the JFrame. – Naseem Jul 23 '16 at 12:05
  • 2
    `frame.removeAll()` doesn't affect the panels added to the frame, those panels will still have components after they get removed from the frame. If you only want to change the components' values, you can do that without removing them. – Titus Jul 23 '16 at 12:07
  • Sounds like a good solution. Thanks, will try and let you all know :) – Naseem Jul 23 '16 at 12:10
  • 5
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). This is the type of functionality that *card layout is **made** for!* 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 23 '16 at 13:04
  • 5
    *"Same components with different values"* Actually, scrap that idea about the card layout. The ***best*** approach is not to remove or add anything. Simply keep references to the components or their models and define & call a `setDefaults()` method when needed. After the components are initially created, and then whenever required afterwards. – Andrew Thompson Jul 23 '16 at 13:07
  • Thank you all for the solution. @Andrew, Next time when I'll ask the question, I'll definitely use the pointer you mentioned. – Naseem Jul 24 '16 at 11:22
  • @Titus Could you please add your comment as an answer, I'll accept it as an answer :) – Naseem Jul 24 '16 at 11:23

1 Answers1

2

You need to validate() after you remove. Here is a working demo of remove all and adding a new component :

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;

public class Test extends JFrame {

    private int click_count = 0;

    public Test(){

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout());
        addLabel();
        validate();
        pack();
    }

    /**
     *
     */
    private void labelClicked() {

        getContentPane().removeAll();
        addLabel();
        revalidate();
        repaint();
        click_count++;
    }

    /**
     *
     */
    private void addLabel() {

        JLabel label = new JLabel("click count " + click_count);
        label.setPreferredSize(new Dimension(200,100));
        label.addMouseListener(new MouseInputAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                labelClicked();
            }
        });

        add(label);
    }

    public static void main(String[]arghs){

        Test frame = new Test();
        frame.setVisible(true);
    }
}

Alternatively you could change a component's properties by updating it, without removing it and adding a new one:

import java.awt.Dimension;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;

public class Test extends JFrame {

    private JLabel label;
    private int click_count = 0;

    public Test(){

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label = new JLabel("click count " + click_count);
        label.setPreferredSize(new Dimension(200,100));
        label.addMouseListener(new MouseInputAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                labelClicked();
            }
        });

        add(label);
        validate();
        pack();
    }

    /**
     *
     */
    private void labelClicked() {
        click_count++;
        updateLabel();
        repaint();

    }

    /**
     *
     */
    private void updateLabel() {

        label.setText("click count " + click_count);
    }

    public static void main(String[]arghs){

        Test frame = new Test();
        frame.setVisible(true);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    You need to (re)validate() and repaint() after you remove, because LayoutManager hasn't implemented any notifier about relayout the changes at runtime – mKorbel Jul 23 '16 at 17:58
  • You don't need to validate if you revalidate, which is what the OP is doing. How does this answer solve the problem? – user1803551 Jul 25 '16 at 13:11
  • @user1803551 it implements what the user wanted : remove all and new components. In addition it demonstrates another option. – c0der Jul 25 '16 at 16:32