Search code examples
javaswingpointerswindowsetfocus

java Access a window/Jframe by window name/title


I have an array of Strings and i want to print the Strings in Jframes. I use a for to call another class wich will create a Jframe with a Jpanel. It goes something like this:

for(int i=0;i!=v.length;i++){  
  (...)
  NewWindow wind = new NewWindow();
}

The problem is when i want to close one of those Jframes. I know the title/window name but i lost the pointer because wind is only valid for the last Jframe created.

I don't know another way to create an unknown number of Jframes, without loosing the pointer, or to get focus of the Jframe. Is it possible in java?


Solution

  • Why not just keep your references to the windows?

    NewWindow[] windows = new NewWindow[v.length];
    for (int i = 0; i < v.length; i++) {
        // (...)
        windows[i] = new NewWindow();
    }
    

    Or, alternatively:

    ArrayList<NewWindow> windows = new ArrayList<NewWindow>(v.length);
    for (int i = 0; i < v.length; i++) {
        // (...)
        windows.add(new NewWindow());
    }
    

    EDIT: Or as per skirsch's answer, if you want to be able to access the windows by the value of the string, use a Map<String, NewWindow>