Simple java beans problem

Ok, so i am new to java beans and i am having trouble with a small program. I have a circle class, and a CircleComponentBean.
I have added the CircleComponentBean to my JFrame using the GUI builder in net beans. I want to add a JButton which when clicked, repaints the component with the changed colour.
EDIT: Forgot to say the problem. The circle component is added to the frame, so is the button. But when pressed nothing happens, and the component is not re painted.

Step through the paintComponent's code one line at a time. You'll find that the end result is that it paints the component GREEN every time. (More precisely it paints it RED then ORANGE then GREEN, but you only see the end result.)

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168

posted Nov 07, 2012 12:42:19

0

Paul Clapham wrote:Step through the paintComponent's code one line at a time. You'll find that the end result is that it paints the component GREEN every time. (More precisely it paints it RED then ORANGE then GREEN, but you only see the end result.)

Problem is sorted now. I effectively started form scratch, only dealing with the colour change in circleComponent and comparing Color instead of strings.

tom davies wrote:Problem is sorted now. I effectively started form scratch, only dealing with the colour change in circleComponent and comparing Color instead of strings.

Sounds like an excellent plan. Having the paintComponent method change the state of the component, like you originally did, was also bad because paintComponent doesn't only happen when you call repaint on the component. It also happens at other times when the GUI decides the component needs to be redisplayed. Like if you minimized your application and then restored it, then the component needs to be redisplayed. Or if you maximized some other application in front of yours and then minimized that application, then the component needs to be redisplayed.

(Actually you might want to test those scenarios too -- it isn't clear from your description that they are handled correctly.)