How to check if a jframe is opened?
Asked Answered
H

3

3

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);   
}
Hostetter answered 1/7, 2013 at 4:26 Comment(2)
I swear to god I saw this same question earlier today. Have you tried searching this?Fetich
See also - The Use of Multiple JFrames, Good/Bad Practice?Benevolence
H
7

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.

Honduras answered 1/7, 2013 at 4:28 Comment(1)
Thank you for giving me the idea of JDialog.Hostetter
D
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.

Direful answered 25/9, 2015 at 13:24 Comment(1)
getting "error: component has private access in Container"Trueblood
D
0

this is old but i would like to share how i handle this kind of requirement:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class multipleFrames extends JFrame {
    
    static JFrame mainFrame;
    static JFrame secondFrame;

    public multipleFrames() {
        // TODO Auto-generated constructor stub

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // Create the main frame
        openMainFrame();

    }

    protected static void openSecondFrame() {
        // TODO Auto-generated method stub
        secondFrame = new JFrame("Second Frame");
//      secondFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        secondFrame.setSize(400, 200);
        secondFrame.setLayout(new FlowLayout());
        secondFrame.setLocationRelativeTo(mainFrame);
        secondFrame.getContentPane().setBackground(Color.decode("#3d8500"));
        secondFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                JOptionPane.showMessageDialog(secondFrame,"Disposing secondFrame ...","Message",JOptionPane.PLAIN_MESSAGE);
                secondFrame.dispose();
                // clear secondFrame references to destroy it
                secondFrame = null;
            }
        });

        // Create buttons
        JButton backButton = new JButton("Back to Main Frame");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//              secondFrame.setVisible(false);
                if (mainFrame==null) {
                    JOptionPane.showMessageDialog(secondFrame,"mainFrame not exist, creating ...","Message",JOptionPane.PLAIN_MESSAGE);
                    openMainFrame();
                }else {
                    mainFrame.setVisible(true);
                    mainFrame.toFront();
                }

            }
        });
        secondFrame.add(backButton);

        secondFrame.setVisible(true);
        
    }

    protected static void openMainFrame() {
        // TODO Auto-generated method stub
        mainFrame = new JFrame("Main Frame");
//        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setSize(400, 200);
        mainFrame.setLayout(new FlowLayout());
        mainFrame.setLocationRelativeTo(secondFrame);
        mainFrame.getContentPane().setBackground(Color.decode("#3d85c6"));
        mainFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                JOptionPane.showMessageDialog(mainFrame,"Disposing mainFrame ...","Message",JOptionPane.PLAIN_MESSAGE);
                mainFrame.dispose();
                // clear manFrame references to destroy it
                mainFrame = null;
            }
        });
        
        // Create buttons
        JButton secondButton = new JButton("Open Second Frame");
        secondButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//              mainFrame.setVisible(false);
                if (secondFrame==null) {
                    JOptionPane.showMessageDialog(mainFrame,"secondFrame not exist, creating ...","Message",JOptionPane.PLAIN_MESSAGE);
                    openSecondFrame();
                }else {
                    secondFrame.setVisible(true);
                    secondFrame.toFront();
                }

            }
        });
        mainFrame.add(secondButton);
        
        mainFrame.setVisible(true);
        
    }

}
Dagney answered 14/5 at 7:15 Comment(1)
Maybe you could make the code in your answer a minimal reproducible example? As it stands, I don't think it would compile since I can't find the declaration of variable otherframe in class myFrame. By the way, when posting code on SO, it is recommended to adhere to Java naming conventions.Bowman

© 2022 - 2024 — McMap. All rights reserved.