2

How to access different JFrame executing in different event dispatch threads of a Java application Please go through the SSCCE posed here Will new instance of JVM or reflection help in this case

In the end as I mentioned If the following code is added to the first class

Frame[] f2 = JFrame.getFrames();

for(Frame fx: f2){
    System.out.println(fx.getTitle());
    fx.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
}

only a single frame is returned if added to second class in sequence in SSCCE two frames are returned and if added to third class All frames are returned. Is this a problem due to event dispatch threads what is the reason behind this behavior. How to access all frames from Main frame i.e., MyApp.

Why does JFrame.getFrames(); returns awt Frames say for example if I want to change the default close operation of all JFrames in my application gerFrames returns awt frames which if caset in JFrame produces an exception.

Exception in thread "main" java.lang.ClassCastException: [Ljava.awt.Frame; cannot be cast to [Ljavax.swing.JFrame; at myApp.MYApp.main(MYApp.java:48)

Community
  • 1
  • 1
Sanyam Goel
  • 2,138
  • 22
  • 40
  • `[Ljava.awt.Frame; cannot be cast to [Ljavax.swing.JFrame; at myApp.MYApp.main(MYApp.java:48)` this is because you are trying to cast the _array_. You need to cast the frames _individually_ as mKorbel describes. – Jacob Raihle Jul 20 '12 at 06:59
  • 2
    Didn't I just answer this @ http://stackoverflow.com/questions/11573259/will-new-instance-of-jvm-or-reflection-help-in-this-case/11573328#comment15312477_11573328 ... – MadProgrammer Jul 20 '12 at 09:32
  • @MadProgrammer sorry I had already posted this here and it was not answered so I though to ask in in continuation there the last part :( – Sanyam Goel Jul 20 '12 at 09:41
  • @sand1988, no worries, bust thought I was going crazy ;) – MadProgrammer Jul 20 '12 at 09:42
  • 1) Anything that requires more than one source code is not an SSCCE. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jul 21 '12 at 00:12

1 Answers1

7

code line

Window[] allWindows = Window.getWindows();

returns arrays of all Top-Level Containers from current JVM

  • J/Frame

  • J/Dialog(JOptionPane)

  • J/Window

have to test if

if (allWindows[i] instanceof JFrame) {
mKorbel
  • 109,525
  • 20
  • 134
  • 319