I am trying to implement action listener on two buttons in JFrame, but the issue is one of the two button is performing both the functions; but i\'ve not configured it to do so.
You think the button triggers both the if
and else
statement but that is not the case. If you would adjust your code in the following way:
setColor
, changeColor
or something similar to your MyDrawPanel
classMyDrawPanel#paintComponent
method to use a fixed color instead of a random color, and only adjust the color through the method created in the first stepMyDrawPanel
The thing is that paintComponent
can be called by Swing itself. It is not only called when you call repaint
(which is a good thing, or all code you write for Swing components would be filled with repaint
calls).
Side note: when overriding the paintComponent
method I would recommended to call super.paintComponent
as well