Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFJ TwoButtons problems

 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using the Panel to repaint and not the frame.
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i`m not creating any Panel object, i`m just extending from the JPanel class
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right, i also tryed :

and edited here also:


but without any succes, it manifests the same way
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is the correct code, anyone care to help?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic