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: 32689
4
posted
0
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: 32689
4
posted
0
Moved to our GUIs forum where we usually dscuss such questions.
WeiJie Lim
Ranch Hand
Joined: Sep 05, 2012
Posts: 68
posted
0
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: 32689
4
posted
0
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: 68
posted
0
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 JButtonGridLayout.
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
posted
1
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: ActionListener not invoking the method it is instructed to do so.