How to check if a jframe is opened?

柔情痞子 提交于 2019-12-20 02:35:29

问题


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);   
}

回答1:


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.




回答2:


If using JFrames it can be simply done like this:

if (Frame1.component != null) {
   Frame1 is opened
} else if (Frame2.component == null) {
   Frame2 is closed
}

Component ex.JTextField, JComboBox etc.



来源:https://stackoverflow.com/questions/17397442/how-to-check-if-a-jframe-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!