How to check if jframe is opening?

My code below creates a new array and sends it to the 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 change this code to work in such a way that if the jframe of the chat is open, then do not open a new jFrame. Instead, open a new tab in your jframe chat. Code for chat frame:

 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); } 
+3
source share
2 answers

I wonder if JDialogs should be used instead of JFrames if the window depends on another window.

The solution is to use the class field to bind to the window (JFrame or JDialog) and check if it is null or visible, and if so, 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 all I can say based on the information provided. If you need more specific help, you need to provide more information.

+7
source

If you use JFrames, you can do this as follows:

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

Component ex.JTextField, JComboBox, etc.

+1
source

Source: https://habr.com/ru/post/1489024/


All Articles