1

I am developing using Java SE on NetBeans 7.3.1 on Windows 7.

My java main method has the following calls

static Vector<Point2D> acceptedByFilter, coords;

// Some code to read the coords from a file

// Some code to filter coords and produce a subset called acceptedByFilter

DisplayInputPoints();
DisplayPointsAcceptedByFilter();

These methods are defined as follows

static protected void DisplayPointsAcceptedByFilter(){
    Panel panel=new Panel();
    panel.DisplayInputPoints(acceptedByFilter, xMin, xMax, yMin, yMax, true, "Points                  accepted by filter");
 }

static void DisplayInputPoints(){
    Panel panel=new Panel();
    panel.DisplayInputPoints(coords, xMin, xMax, yMin, yMax, true, "Original Points");
}

Panel.DisplayInputPoints is defined as follows

import javax.swing.JFrame;
import javax.swing.JPanel;

public 
class Panel extends JPanel  
{
    public static void DisplayInputPoints(Vector<Point2D> coords, double xMin, double xMax, double yMin, 
        double yMax, boolean invert, String label){

        JFrame frame = new JFrame(label);
        Panel panel = new Panel();
        panel.setPreferredSize(new Dimension((int)Math.round(xMax-xMin) + 10, 
            (int)Math.round(yMax-yMin) + 10));
        panel.loadPoints(coords);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        frame.repaint();
    }
}

When I call

DisplayInputPoints();

the first frame appears and it displays the points in the coords vector. When I call

DisplayPointsAcceptedByFilter();

I get another frame and the points in the acceptedByFilter appear in both frames. That is, the first frame gets overwritten by the display that is in the second frame.

What is the best way to stop the first frame being overwritten by what is supposed to be only in the second frame?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81

2 Answers2

2

Your code structure appears to be way off, and that is likely your main problem. For instance,

  • You shouldn't be using static methods or fields for one thing. The only static methods should be the main and associated methods and utility methods. The only static fields should be class fields such as constants.
  • Your DisplayPointsAcceptedByFilter creates a Panel object which causes the 2nd JFrame to display. Get the display code out of the Panel class, it doesn't belong there and is causing your problem.
  • Don't name a class with the same name as a Java core class, such as Panel.
  • Your Panel class isn't even a real OOP class, but nothing but a holder of a static method. This flies against all OOP rules.
  • If this were my code, I'd scrap it and re-write it using OOP principles from the get-go.

For more detailed and better help, consider supplying more information in your question and perhaps creating and posting an sscce.


Edit
Consider creating:

  • A class that reads in the coordinates from a file. It can then put the coordinates into an ArrayList. It should have no static methods or fields.
  • A class for filtering the coordinates. It also should have no static methods or fields and no GUI code.
  • A GUI class that extends JPanel that is for displaying the data.
  • The GUI class accepts an array list of coordinaates and then somehow draws the coordinates.
  • The GUI class accepts new coordinates with a setter method, and then will re-draw the new coordinates.
  • A class with a main method that creates a JFrame for your GUI class to be displayed in, and that orchestrates all your other classes, gets them started.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I wanted to move DisplayInputPoints() out of a larger class and moved it into the Panel class. I overlooked the fact that it actually called the Panel class. I made a separate class for the display code and got it out of the Panel class. That fixed the problem. I see your point about the naming of the Panel class. I didn't realize there was a java class by that name. (I thought it was just JPanel.) I will change the name of the class. Thanks. – OtagoHarbour Nov 26 '13 at 02:53
1

Have you thought about making Panel an abstract class, and perhaps altering it specifically for two purposes? I mean i feel like there's more code you should show to properly understand the problem

drocktapiff
  • 117
  • 3
  • 11