Results 1 to 4 of 4
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Something I am very confused by:

    Quote Originally Posted by MW130 View Post
    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.

    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.

    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.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Something I am very confused by:

    Quote Originally Posted by Fubarable View Post
    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Something I am very confused by:

    Quote Originally Posted by MW130 View Post
    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.

    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.

    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.

    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.

Similar Threads

  1. Help Please - I'm confused
    By NextToNothing in forum Threads and Synchronization
    Replies: 4
    Last Post: 07-04-2011, 11:37 PM
  2. Help i am so confused..:(
    By angeltiner in forum AWT / Swing
    Replies: 6
    Last Post: 04-11-2010, 01:43 AM
  3. I am confused
    By prof.deedee in forum New To Java
    Replies: 6
    Last Post: 10-30-2009, 11:32 PM
  4. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 04:34 PM
  5. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •