0

How to Forced Java J Frame application to run only one instance in windows? base on the image link below

[1]: https://i.stack.imgur.com/hKrNu.jpg`enter code here`

public Home() {

    initialize();   
    
}       
public void mouseClicked(MouseEvent e) {
    
}
    
private void initialize() {
    
            

    frame = new JFrame();
    frame.getContentPane().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            
            
            
            
        }
    });
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.getContentPane().setLayout(null);
    
    txtUsername = new JTextField();
    txtUsername.setBorder(null);
    txtUsername.setBackground(Color.DARK_GRAY);
    txtUsername.setForeground(Color.WHITE);
    txtUsername.addFocusListener(new FocusAdapter() {

1 Answers1

0

You initialize a new JFrame everytime you call the method initialize().

If you want your application to only build one JFrame, create the JFrame object outside the method only one time.

So there should be

JFrame frame = new JFrame();

one time only in your program.

If you want to always create a new JFrame intentionally. You can, before creating a new JFrame call

JFrame.dispose();

to close the previous JFrame

John
  • 136
  • 12
  • Where i put the code sir? after Jframe Frame = new JFrame();? – Ramil H. Bercasio Aug 09 '22 at 12:46
  • @RamilH.Bercasio You can possibly just let the rest be as it is. You only have to extract the initialization of the JFrame. I recommend you make it a class variable. So you will have still access inside the method initialize() without ceating countless JFrame objects – John Aug 09 '22 at 12:50