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?