Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

How do I stop from having multiple dialog open?

If my user clicks the top menu button my dialog opens, then if the user clicks the button again a second dialog opens.  I really only want one dialog open at a time
0
jkteater
Asked:
jkteater
  • 9
  • 2
1 Solution
 
for_yanCommented:
you can make this diialog a singleton class as we did in previous question
0
 
for_yanCommented:

look at this example from our previous
question and turn your dialog into singleton :

import javax.swing.table.AbstractTableModel;
import java.awt.*;

public class TestSingleton extends AbstractTableModel{

    private static TestSingleton instance = null;
      private TestSingleton() {
         // Exists only to defeat instantiation.
      }
      public static TestSingleton getInstance() {
         if(instance == null) {
            instance = new TestSingleton();
         }
         return instance;
      }


   public int getRowCount(){ return 1; }
  public int getColumnCount(){return 1;}
  public Object getValueAt(int row, int column){return null;}


}

class Try {
   

    public static void main(String[] args) {
      TestSingleton ts =   new TestSingleton();  // compilation error - cannot dierctly instantiatite
    }
}

Open in new window

0
 
for_yanCommented:
After that you'll not be isntatitating this diaog with contructor (new) everytime
but rather will be getting hold of the same existing instance
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
You don't need special patterns. All you need do is set the dialog to modal (see below)
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class FB extends JFrame implements ActionListener {
    private JButton b;
    private int currentIndex;

    public void actionPerformed(java.awt.event.ActionEvent e) {
        JDialog d = new JDialog(this);
	Container c = d.getContentPane();
	c.add(new JLabel(String.format("Modal %d", ++currentIndex)));
	d.setLocation(300, 300);
	d.setModal(true);
	d.pack();
	d.setVisible(true);
    }

    private void setGui() {
	try {
	    setLocation(0, 100);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    Container cp = getContentPane();
	    JPanel bPanel = new JPanel();
	    b = new JButton("Show dialog");
	    b.addActionListener(this);
	    bPanel.add(b);
	    cp.add(bPanel, BorderLayout.SOUTH);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public static void main(String[] args) {
	try {
	    SwingUtilities.invokeAndWait(new Runnable() {
		public void run() {
		    FB f = new FB();
		    f.setGui();
		    f.setSize(200, 200);
		    f.setVisible(true);
		}
	    });
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

Open in new window

0
 
jkteaterAuthor Commented:
when I set Modal to true the dialog comes up and the application quits responding
0
 
for_yanCommented:
No it responds only to diaalog until you clode it
0
 
jkteaterAuthor Commented:
no the whole dialog freezes can't click anything. it will not close or drag.  I have to control-alt-delete can kill the process
0
 
for_yanCommented:
The simple way to do what you need is to have your dialog variable instance variable in the class where yoiu create your dialog (ususlly JFrame). And then yoiu check if this variabkle is not null, you do not create a new dialog
0
 
for_yanCommented:
And when you dispose iof your dialofg you set this variable to null again
0
 
for_yanCommented:
Modal is good solution if you really want to prevent user from accessing your frame unti he/she deals with the dialog and closes it
0
 
for_yanCommented:
That is really strange - it should not freeze
0
 
for_yanCommented:
But anyway modal - is not for your pourpose of having one dialog only
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now