Search code examples
javaswingjframejdialogjtabbedpane

How to check if a jframe is opened?


My code below create a new array and sends it to chat(jFrame).

String info1[]=new String[3];
 // username , userid , userid2 are variables
 info1[0]=username4;
 info1[1]=""+userid;
 info1[2]=""+userid2;

 chat.main(info1);

But i need to modify this code to work such a way that it , if the chat jframe was opened, then dont open a new jFrame .But instead open a new tab in chat jframe . The code for chat frame is :

private void formWindowActivated(java.awt.event.WindowEvent evt) {       
  JScrollPane panel2 = new JScrollPane();
  JTextArea ta=new JTextArea("");
  ta.setColumns(30);
  ta.setRows(19);
  panel2.setViewportView(ta);
  jTabbedPane1.add("Hello", panel2);   
}

Solution

  • I wonder if you shouldn't be using JDialogs instead of JFrames, if the window is dependent on another window.

    A solution is to use a class field to hold a reference to the window (JFrame or JDialog) and checking if it is null or visible, and if so, then lazily create/open the window,

    public void newChat(User user) {
      if (chatWindow == null) {
        // create chatWindow in a lazy fashion
        chatWindow = new JDialog(myMainFrame, "Chat", /* modality type */);
        // ...  set up the chat window dialog
      }
    
      chatWindow.setVisible(true);
      addTabWithUser(user);
    }
    

    but that's about all I can say based on the information provided. If you need more specific help, then you will need to provide more information.