简体   繁体   中英

Repainting a JPanel from a JCheckBox ActionListener in another Package

OK, I am going to try to explain this as best as I can. I am fairly proficient at Java but am unable to find a logical solution to this after extensive searching. Lets say that I have a JPanel class inside package A that will contain a graph that will be drawn.

package A

public class DrawGraph extends JPanel
{
    public DrawGraph()
    {
    }

    @Override
    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        // other stuff
    }

    public void updateGraph()
    {
        repaint();
    }
}

In a different class inside package BI have a JCheckBox that when selected should trigger a repaint of the graph in package A. This class does not initialize the DrawGraph class. That class is initialized elsewhere.

package B

public class CheckBoxClass extends JPanel

public CheckBoxClass
{
    graphicsCheckBox.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent cb) 
        {
           GUI_Data.graphics = true;
           DrawGraph.updateGraph();  // Calls Update Graph function
        }
    });
}

Now how do I call the updateGraph function in my DrawGraph class without having to create a new instance of the DrawGraph class? I know that I cannot make the updateGraph method static since repaint() is not static. I feel like there has to be a way to do this without repainting via a timer or some other convoluted and inefficient method. Am I looking at this problem in the wrong way? Basically I need a way to trigger a repaint of the DrawGraph class from the JCheckBox class actionlistener. If this question isn't clear, please let me know so I can revise it. Thank you guys in advance, this is my first question but I have been using you guys for several years.

There seem to be a bit of confusion of concepts here, let me try to explain.

static vs. instance methods: A class can have static and non static methods. non-static methods can be called only on an instance of the class. static can be called only on the class (although you can call them through a variable containing an instnace of the class).

method visibility: a method can be private, protected, package-protected or public. private methods can be called only from within the same class). protected can be called only from within the class or subclasses. package-protected (without any qualifier) can be called from classes within the same package. public can be called from anywhere.

In your case, the fact that the CheckBoxClass is not in the same package has nothing to do with the fact that you can't call DrawGraph.updateGraph() . updateGraph() is an instance method (non-static), hence you have to have an instance to call it. Now, if you know that there is going to be only one instance of DrawGraph in your program, then you can use the singleton pattern: 1) in the DrawGraph class have a static variable initialized to an instance of the class itself. Also have a static method that returns such instance:

public class DrawGraph {
  private static DrawGraph singleton = new DrawGraph();

  public static getInstance() {
    return singleton;
  }
}

Now you can do the following from CheckBoxClass :

  @Override
    public void actionPerformed(ActionEvent cb) 
    {
       GUI_Data.graphics = true;
       DrawGraph.getInstance().updateGraph();  // Calls Update Graph function
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM