• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ActionListener Problems

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> It complies and executes...

not for me.



I'm happy to look at things once...
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic