Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple java beans problem

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Circle class


CircleComponent


This is the JButton code.


I think i have over complicated the process quite a bit as my other ideas didn't work. If anyone could help that would be great.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your action performed, you are creating a new CircleBeanComponent and calling a repaint on that instance, not the one which exists!
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changed to

which is the circleComponent variable it automatically creates here


Still the same problem that it will not repaint though.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:
Still the same problem that it will not repaint though.

What do you expect to happen on repaint?
As per your posted code, the it should display a colored circle. Do you see that?
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
tom davies wrote:
Still the same problem that it will not repaint though.

What do you expect to happen on repaint?
As per your posted code, the it should display a colored circle. Do you see that?

The circle should repaint filled with a different colour. It should follow a colour pattern so it will cycle through a set of 3 colours.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyone that could help me with this? Its getting frustrating as this should be the easy part, but its taken up all my time to try and make it work.
Thank you
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic