Skip to content
Advertisement

How to call ActionPerformed method from different class

I’m trying to call an actionperformed method from a file under the same package as the one I wish to call it from. I have two classes, EditSeriesPaint and MyDataVisualization. I want to call EditSeriesPaint from MyDataVisualization. This is what I’ve tried:

EditSeriesPaint Class:

public class EditSeriesPaint extends MyDataVisualization {
        
        public void editColorActionPerformed(java.awt.event.ActionEvent evt) {

        Color newColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
        CategoryPlot plot = jchart.getCategoryPlot();
        plot.getRenderer().setSeriesPaint(0, newColor);
        
    }
    
}

MyDataVisualization Class:

editColor.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        EditSeriesPaint esp = new EditSeriesPaint();
        esp.editColorActionPerformed(evt);
    }
});

However, when I add the method to esp and go to use the color picker, it errors out at as jchart is null. Is it not getting the jchart value from MyDataVisualization and if so, how do I fix this?

Advertisement

Answer

I can’t really tell from the two snippets how jchart has been used before the editColorActionPerformed call, but if I had to guess I’d say that either the jcahrt property hasn’t been initialized in MyDataVisualization’s constructor (or EditSeriesPaint’s constructor if jchart has been declared with any access modifier but private) or jchart has been reset in some methods called within the constructor.

Maybe you could share MyDataVisualization and EditSeriesPaint’s constrcutors if this answer doesn’t help you.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement