aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ActionListener not invoking the method it is instructed to do so. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListener not invoking the method it is instructed to do so." Watch "ActionListener not invoking the method it is instructed to do so." New topic
Author

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

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
When I pressed the "Start Game" button, the setImage() method doesn't run.

Any help is appreciated

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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)?


Steve
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

    Joined: Oct 13, 2005
    Posts: 38788
        
      23
    Moved to our GUIs forum where we usually dscuss such questions.
    WeiJie Lim
    Ranch Hand

    Joined: Sep 05, 2012
    Posts: 85
    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

    Joined: Oct 13, 2005
    Posts: 38788
        
      23
    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

    Joined: Sep 05, 2012
    Posts: 85
    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

    Joined: Jun 09, 2003
    Posts: 4632
    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)

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ActionListener not invoking the method it is instructed to do so.