Something I am very confused by:
Hello, I wanted to ask about something that I am profoundly puzzled by; I realize that you use defined methods that get overridden such as the paint method or the actionPerformed method, but what happens when you have two buttons for example that you want to change after an event? How would you do that when you can only override the actionPerformed method? Would you override it again? The same question goes for paint; how can you paint something after using the paint method once before? Thank you so much!
p.s. If possible can anyone tell me how to set a specific button size and location please? Thanks again!
Re: Something I am very confused by:
Quote:
Originally Posted by
MW130
Hello, I wanted to ask about something that I am profoundly puzzled by; I realize that you use defined methods that get overridden such as the paint method or the actionPerformed method, but what happens when you have two buttons for example that you want to change after an event? How would you do that when you can only override the actionPerformed method? Would you override it again?
You could remove the old ActionListener from the JButton and add a new one, but easier is to have logic inside the ActionListener that responds to the state of the program. For instance if you want to toggle the color of something on button press from red to blue to red to blue, ... you have the action listener change a class variable and then change the color based on that.
Quote:
The same question goes for paint; how can you paint something after using the paint method once before? Thank you so much!
Surely you're not overriding paint, right? You're overriding paintComponent after reading all our previous recommendations on this subject, right? The answer is the same -- paintComponent should change its behavior depending on the state of the class, in other words depending on what values are held by class fields.
Quote:
p.s. If possible can anyone tell me how to set a specific button size and location please? Thanks again!
You should avoid setting specific size and location. Know the layout managers well and use them is your best option.
Re: Something I am very confused by:
Quote:
Originally Posted by
Fubarable
You could remove the old ActionListener from the JButton and add a new one, but easier is to have logic inside the ActionListener that responds to the state of the program. For instance if you want to toggle the color of something on button press from red to blue to red to blue, ... you have the action listener change a class variable and then change the color based on that.
Surely you're not overriding paint, right? You're overriding paintComponent after reading all our previous recommendations on this subject, right? The answer is the same -- paintComponent should change its behavior depending on the state of the class, in other words depending on what values are held by class fields.
You should avoid setting specific size and location. Know the layout managers well and use them is your best option.
Thank you so much for your reply. So I think I kind of got the gist; if I want to make use paintComponent again, then I have to use it in a seperate class, right? That makes sense. Also, I meant paintcomponent but why are you so against using paint? Lastly, which layout manager do you recommend? Is there absolutely no way of deciding for yourself the size and location of your buttons, like you can do for a rectangle?
Re: Something I am very confused by:
Quote:
Originally Posted by
MW130
Thank you so much for your reply. So I think I kind of got the gist; if I want to make use paintComponent again, then I have to use it in a seperate class, right?
No. Your paintComponent method uses the variables held in the class and changes what it displays based on the state of those variables. Only one paintComponent and one component is usually needed.
Quote:
That makes sense. Also, I meant paintcomponent but why are you so against using paint?
We've been through this. paint cannot do Swing graphics only AWT and so it will not have double buffering. If you don any animation it will look choppy and awful. paint is responsible for not only painting the component, but also its children and borders, and if you're not careful you can mess up the graphics of these things inadvertently.
Quote:
Lastly, which layout manager do you recommend?
All of them. We often nest JPanels, each using its own layout manager, and so can create complex layouts using simple layout managers. I recommend (again) that you not read but study the tutorials on this subject.
Quote:
Is there absolutely no way of deciding for yourself the size and location of your buttons, like you can do for a rectangle?
There are ways but it makes for very brittle code, code that is difficult to maintain or improve.