0

I am calling a thread in which i am again calling the same class

TrafficMainGUI traffic=new TrafficMainGUI(storeValue);
traffic.setVisible(true);

but i want the previous class object to get destroy.How can i acheive this.

As TrafficMainGUI is a jFrame object.Please help??

AlexR
  • 114,158
  • 16
  • 130
  • 208
rahul bilove
  • 75
  • 1
  • 1
  • 8

3 Answers3

2

To properly destroy a JFrame, you should dispose it.

previousTraffic.dispose();
TrafficMainGUI traffic=new TrafficMainGUI(storeValue);
traffic.setVisible(true);

From the documentation :

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

Your question is quite vague about what you are doing with the threads. As mentioned by @MadProgrammer, when you are working with swing, you should take into account the EDT. But to get a more specific help, you should provide an sscce.

gontard
  • 28,720
  • 11
  • 94
  • 117
0

add this code:

traffic = new TrafficMainGUI(newValues);

traffic will be assigned by new Object, and the previous object will be replaced as new function is request new object in memory.

Dion Dirza
  • 2,575
  • 2
  • 17
  • 21
0

To make your frame disappear just call

traffic.setVisible(true);

This however does not remove the instance of TrafficMainGUI you created. Since java has automatic garbage collection this object will be removed at some point of time automatically when all references that refer to it are not accessible. For example if your variable traffic is defined in method scope it becomes obsolete once your code exits the method. If not you can say traffic = null;. This will remove the reference.

You should note however that GC (garbage collector) lives its own life and can decide itself when to remove your object. It can decide not to remove it even forever. But you should not care about it.

AlexR
  • 114,158
  • 16
  • 130
  • 208