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 HFJ TwoButtons problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "HFJ TwoButtons problems" Watch "HFJ TwoButtons problems" New topic
Author

HFJ TwoButtons problems

Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Hey,
I on the Swing chapter in HFJ, and i stumbled upon this problem. The program is working "apparently" fine. It`s supposed to create a frame, which contains a circle in the center, one button in the bottom of the frame(we call it b1), one button on the right side(button b2) and one label one the left side. When we press b1 the circle changes it color and when we press b2 the label changes its text. In my program b1 changes the circles color but b2 also changes the circles color and the labels text. Is it something i missed in my code? I search the forum and couldn`t find something similar.




Thank you in advance for you time and help!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

You should be using the Panel to repaint and not the frame.


Mohamed Sanaulla | My Blog
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

But i`m not creating any Panel object, i`m just extending from the JPanel class
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

Mihai Lita wrote:But i`m not creating any Panel object, i`m just extending from the JPanel class


on line 25- MyDrawPanel drawPanel = new MyDrawPanel(); - This is creating a instance of MyDrawPanel which is a JPanel
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Your right, i also tryed :

and edited here also:


but without any succes, it manifests the same way
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Which page is it on?
I think this discussion would sit better on the GUIs forum, so I shall move it.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

Ok, I get the reason for the change in the color when you click the Change label button- It is because as the String is smaller- the label gets resized, due to which the panel gets resized and in turn resize of panel calls repaint() method.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

So if i change the String with one that has the same size, it might work, i`ll try and come back with an answer.
Thank you

Edit: If i put original string to "12345" and the new text to "56789" it`s working, but if i put old string like "old label" and new string to "new label" it doesn`t work, they have the same length but different size. What would be a solution for this problem?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

Mihai Lita wrote:So if i change the String with one that has the same size, it might work, i`ll try and come back with an answer.
Thank you

Edit: If i put original string to "12345" and the new text to "56789" it`s working, but if i put old string like "old label" and new string to "new label" it doesn`t work, they have the same length but different size. What would be a solution for this problem?


There should be some method to fix the size of the label to some fixed value.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

i found this method on a site:


EDIT1: in my case i should use:

i think(?)
The above method doesn`t work

I tryed it, found an online site wich allowed me to compile
EDIT 2: I GOT IT:



Now i wonder how can i get the size of button b2 and set the same size for my label

EDIT3: I could do something like:

but this would mess up the Button`s text.

Is there any method like ?


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

The real problem is the business logic (creating a random color) inside the paintComponent(...) override. Always remember that you have absolutely no control over when and how often painting methods are triggered, and don't assume you can prevent that by any combination of set(Preferred/Maximum/Minimum)Size.

Make randomColor an instance field and set its new value in b1's actionPerformed. Then call repaint().


luck, db
There are no new questions, but there may be new answers.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Ok i think i understand, you mean i should declare randomColor outside the method`s body somehow?
Care to give an example?
I`m only learning so any advice is welcomed.
Thank you.


Something like:



will this compile?
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36



this is the correct code, anyone care to help?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

You would have to change the RGB values of the color in the Action event of the the Color change button. Otherwise- with the present code- The color would be same at all times.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Suppose i declare the RGB values in the actionPerformed() method of the ColorButtonListener how can i get an instance of that inner class in MyDrawPanel so i can use it to acces the randomColor variable :
I tryed something like:


and



But it doesn`t find the ColorButtonListener class in this way
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

You would have to make the randomColor as the instance variable. Currently its method local. So you would have to move it to instance level.
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Mohamed Sanaulla wrote:You would have to change the RGB values of the color in the Action event of the the Color change button. Otherwise- with the present code- The color would be same at all times.

Wait a minute didn`t you just said that the RGB values would have to change in the Action event meaning the actionPerformed() method?
I am getting it wrong?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

Mihai Lita wrote:
Mohamed Sanaulla wrote:You would have to change the RGB values of the color in the Action event of the the Color change button. Otherwise- with the present code- The color would be same at all times.

Wait a minute didn`t you just said that the RGB values would have to change in the Action event meaning the actionPerformed() method?
I am getting it wrong?


Yes- Action event is nothing but the actionPerformed for the particular ActionListener.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFJ TwoButtons problems