• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener Problems  RSS feed

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been playing around with this code for quite awhile now and can't get any versions of it to work correctly. Nor can I understand the logic of why it isn't working. It complies and executes but refuses to react correctly to my ActionListener.



Anyone please comment on any aspects of this that they see I could improve on, I don't know anyone who actually programs so I am relying on you all to be my peers. Thanks
[ December 13, 2008: Message edited by: Chadd Franck ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> It complies and executes...

not for me.



I'm happy to look at things once...
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have only looked at your Listener very briefly, but I liked what I saw.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I forgot to post the class SetGridBagLayout, sorry, I've edited the first post to contain it as well as listed it here, now it compiles (G);

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, if your problem is in repainting the beginButton's text correctly try as follows.


In MakeIntroSequence class

> beginButton.addActionListener(new BeginButtonListener(this));
beginButton.addActionListener(new BeginButtonListener(beginButtonPanel));


In BeginButtonListener class

>public void actionPerformed (ActionEvent e)
>{
>beginButton.setText("hi");
>a.validate();
>}
public void actionPerformed (ActionEvent e)
{
beginButton.setText("hi");
a.invalidate();
a.repaint();
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the problem with it?

the only thing odd is the buttons move when the text changes to 'hi',
and this can be fixed by adding the indicated line

beginButton.addActionListener(new BeginButtonListener(this));
beginButton.setPreferredSize(beginButton.getPreferredSize());//<-------------

as for the listener, what are you trying to do with the panel?
you don't even need to refer to 'beginButton'.
the class is called BeginButtonListener, so, presumably, it is only added to 'beginButton',
and if so, just getting the source will get you the button.

when changing the button's text, repaint() is called automatically, so there's no need for validate() or repaint()
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, This isn't the full code that I was working on, I'll print that out now, here's the key thing, after hitting the BEGIN button, the JFrame isn't repainting, but if I resize the screen it immediately shows the next panel,

try it, run the program, hit the begin button then resize the container,

I'm assuming I'm not calling the .invalidate() or .repaint() correctly or they are not the correct thing to use?

Full Code

[ December 13, 2008: Message edited by: Chadd Franck ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add the indicated line

contentPanel.invalidate();
contentPanel.revalidate();//<--------------
contentPanel.repaint();

far easier to instead use a cardlayout.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, that worked thanks Michael and Campbell

I've made a program like this one before and didn't have to put that in, didn't have to put in any repaint type methods, what determines when you have to and when you don't?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> what determines when you have to and when you don't?

whenever you add or remove components from a 'visible' container you have to call
swing components - revalidate()
awt components - validate()

now always required, but safer to always include, call
repaint()
after validating
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read more document to knowledge.

invalidate()
validate()
revalidate()
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers,
thanks for the info,
I'll be looking into that shortly.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So revalidate() would be the best method to use. I wouldn't need to call invalidate, because revaildate auto calls invalidate() then adds the component's validateRoot to a list of components that need to be validated;

Next question, how does revalidate() add the component's validateRoot to the list of components?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!