Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListener not invoking the method it is instructed to do so.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I pressed the "Start Game" button, the setImage() method doesn't run.

Any help is appreciated

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:When I pressed the "Start Game" button, the setImage() method doesn't run.

Does System.out.println("bbb"); get executed (does bbb show up on the console)?
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
WeiJie Lim wrote:When I pressed the "Start Game" button, the setImage() method doesn't run.

Does System.out.println("bbb"); get executed (does bbb show up on the console)?


Nope. Do you mean when I press "Start Game" button or some other button ?

If you mean "Start Game" , nope, nothing is printed to console.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then if the code you have is correct, the setImage() method is being called. You should put System.out.println() statements inside the method to show where and how that method executes.

If none of the print statements inside the setImage() method get called then there are important details you are not showing us.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Okay, then if the code you have is correct . . .
In my opinion, even the little code I can see ain’t correct. I can see two problems:-
  • 1: Why is the button a field? Why is it not a local variable in the constructor/initGUI method or wherever?
  • 2: As regular readers will know, I do not like addActionListener(this) at all. You should use an ActionListener object from its own class, eg StartListener, or anonymous.
  • Even so, it appears likely that the setImage() method is in fact being called and something is going wrong there, as SL has already suggested.
     
    Campbell Ritchie
    Sheriff
    Posts: 48910
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Moved to our GUIs forum where we usually dscuss such questions.
     
    WeiJie Lim
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Steve Luke wrote:Okay, then if the code you have is correct, the setImage() method is being called. You should put System.out.println() statements inside the method to show where and how that method executes.

    If none of the print statements inside the setImage() method get called then there are important details you are not showing us.


    I set System.out.println() statements in the setImage() methods, it is printing.

    So the method call works, but it doesn't work.

    I think it is because I have to either override the setPreferredSize() or remove the orginal Jbuttons created.

    Updated code shown shortly.
     
    Campbell Ritchie
    Sheriff
    Posts: 48910
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    WeiJie Lim wrote: . . . I think it is because I have to either override the setPreferredSize() or remove the orginal Jbuttons created. . . .
    I am really confused about what that all means. And it makes me suspect that you are confused too. I have seen people say things rather like that, and it makes me think that they are simply guessing about what to do. You can guess 1000000 times and there is a chance your code will work.

    Or you can work out what you should really do and it will work first time.
    Write down what the start button is supposed to do: if it sets the image, why is it called start? Write down how the setImage method ought to work: where does the image come from, and how is it supposed to be displayed?
     
    WeiJie Lim
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    WeiJie Lim wrote: . . . I think it is because I have to either override the setPreferredSize() or remove the orginal Jbuttons created. . . .
    I am really confused about what that all means. And it makes me suspect that you are confused too. I have seen people say things rather like that, and it makes me think that they are simply guessing about what to do. You can guess 1000000 times and there is a chance your code will work.

    Or you can work out what you should really do and it will work first time.
    Write down what the start button is supposed to do: if it sets the image, why is it called start? Write down how the setImage method ought to work: where does the image come from, and how is it supposed to be displayed?


    -- Some explanation, hopefully it helps somewhat --

    In essence, I am creating a puzzle game. It contains an image , and the image is further divided into 9 pieces which is placed onto 3x3 JButton GridLayout.

    The issue is because the size of the JPanel in which the original 9 buttons without images is created is based on the size of the image itself. ( due to usage of insets )

    But there is no image at the start, so I use setPreferredSize() instead to set the size of the JPanel.

    When the user clicks "Start Game", the new 9 buttons with images are supposed to show.

    I typed "override the setPreferredSize() or remove the orginal Jbuttons created." because I think that the setPreferredSize() set initially is affecting the insets which I have used to size the panel with the 9 imaged buttons.
     
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    your problem is here (in actionPerformed())

    any time you add/remove components from a visible Swing container, you must include these lines (after the add/remove)

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic