0

The application that I am working on involves frames that are created from a JPanel based upon what the user selects. I am trying to prevent a user from launching multiple instances of the same frame if the select the same item twice. Here is the condition what I wrote for that purpose.

Main class:

public void showPieGraphFrame()
{
    final PieGraph gPieGraph = new PieGraph("Traffic Type Distribution", counterOne, counterTwo);
    gPieGraph.pack();
    RefineryUtilities.positionFrameOnScreen(gPieGraph, 0.35, 0.03);

    if(!gPieGraph.isVisible())  
    {
    gPieGraph.setVisible(true);
    }
}

PieGraph class that I want to prevent multiple instances of:

public class PieGraph extends ApplicationFrame implements ActionListener {

    public PieGraph(final String title) {
        super(title);

        // create a menubar
        setJMenuBar(createMenuBar());

        // create a dataset...
        final PieDataset dataset = trafficTypeDataset();

        // create the chart...
        final JFreeChart chart = createChart(dataset);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
        setContentPane(chartPanel);
    }

    private JFreeChart createChart(final PieDataset dataset) {

        final JFreeChart chart = ChartFactory.createPieChart("Test Chart", dataset, false, false, false);

        final PiePlot plot = (PiePlot) chart.getPlot();

        return chart;
    }

However, it is not working and you can still launch the same frame more than once. How can I prevent this?

THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64

3 Answers3

6

How can I prevent this?

Swap the frame to a modal JDialog or a JOptionPane.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Unfortunately, that is not an option. The frame contains a graph and JMenuBar. – THE DOCTOR Feb 21 '13 at 18:40
  • *"The frame contains a graph and JMenuBar."* Why on earth does an element popped out of a GUI need it's own menus? You seem to be going about this all wrong. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for some basic tips. – Andrew Thompson Feb 21 '13 at 18:48
  • 1
    I have been thinking of switching to a JInternalFrame instead of what I have now. Thanks for pointing out that what I'm trying to do probably isn't a good approach. I'm still curious to see if there's a way to do what I asked though. – THE DOCTOR Feb 21 '13 at 19:00
  • If THE DOCTOR insists, there is no problem. A JDialog can also have a JMenuBar. – Costis Aivalis Feb 21 '13 at 19:00
  • *"I'm still curious to see if there's a way to do what I asked though."* Possibly. I expect the reason for the current failure was identified by @GiovaniGuizzo *"Two windows will only appear when you have two JFame objects."* Which makes me think that somewhere outside that snippet, there is code (that allows) for more than one frame to be created. But short of seeing the SSCCE, I really cannot say for sure. – Andrew Thompson Feb 21 '13 at 19:09
  • @andrew I'm more curious as to way a window having a JMenuBar would prevent you from using a JDialog? – MadProgrammer Feb 21 '13 at 20:02
2

It simple doesn't allow you to call setVisible twice. Two windows will only appear when you have two JFame objects. Make your JFrame a Singleton.
Hope I could help.

Giovani Guizzo
  • 527
  • 3
  • 13
2

Use the Singleton Pattern for the frame, e.g:

public class MainFrame extends JFrame() {

 private static MainFrame instance;

 public static MainFrame getInstance(//parameters) {
   if (instance == null){
    instance = new MainFrame(//parameters)
 }
    return instance;
 }

 private MainFrame(//parameters){}

}
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51