File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Simple java beans problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple java beans problem" Watch "Simple java beans problem" New topic
Author

Simple java beans problem

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jul 26, 2007
Posts: 10175
    
    8

In your action performed, you are creating a new CircleBeanComponent and calling a repaint on that instance, not the one which exists!


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jul 26, 2007
Posts: 10175
    
    8

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

Joined: Apr 27, 2012
Posts: 168
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

Joined: Apr 27, 2012
Posts: 168
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple java beans problem
 
Similar Threads
Problems with Constructors and Drawing a Car
Cannot find symbol compiler error
Applet Frustration
Custom JComponent Problem
Checkerboard w/ 64 squares