Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Button Problems

 
james wilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope someone can help.
I have a program that invokes a method in a seperate class, but when I do so the button doesn't work. Am I doing something stupid?!
Any help appreciated.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by james wilson:
Hope someone can help.
I have a program that invokes a method in a seperate class, but when I do so the button doesn't work. Am I doing something stupid?!
Any help appreciated.

 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James
Can you provide more details and/or some code ? The problem you need to solve is not clear from your posting. Which class contains the button? What is the button supposed to do? What are the symptoms of it 'not working'?
Make sure you have added a valid action listener object to the button - this could be your problem. Buttons simply fire off events when they get clicked, it is up to the programmer to decide what to do in response to this event.
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
james wilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right ok sorry.
Here goes.
Class One (Main) invokes two methods in class two (LoggedIn) this done as follows;
loggedin=new LoggedIn();
loggedin.minidisplay();
loggedin.timer(time);
the minidisplay method creates a small gui with a textarea and a button(the button). The timer method just counts down from whatever number has been passed into it.
In class two (LoggedIn) the minidisplay method creates the button as follows;
logout=new Button("LOG OUT");
fr.add(logout);
logout.addActionListener(new ActionListener()
{
public void actionperformed(ActionEvent ae)
{
System.out.print(time);
}
});
when i test the second class with a public static void main it works fine, but when i invoke it from another class i cannot even depress the button.
I hope this is enough and isn't to ambiguous, it's the first time i've left a message so don't know how much info to give.
Cheers James
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Your description of being unable to press the button makes me wonder if maybe there's a thread problem. I was recently looking at "Java thread programming" by Paul Hyde. He works through an example of a graphical timer - ie press a button to watch a sweeping minute hand clock display count off the passage of time. Anyway, he demonstrates how things can freeze up if you have the one thread looking after event handling as well as running the timer.
Essentially, the program required that the timer happen in one process while event handling happened in another. This way, both threads could get something like 'equal time'. The event handling thread would get a chance to listen for button pressing, and the timer thread would also get a chance to do it's processing.
I don't know if this is what's happening in your program, but it maybe something to consider, or at least cross off the list of possible problems
cheerio
rowan
~~~~~~~~~~~~~
when i test the second class with a public static void main it works fine, but when i invoke it from another class i cannot even depress the button.
~~~~~~~~~~~~~
 
james wilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rowan,
Thanks for your suggestion, I created a thread fro my timer method in the LoggedIn Class and now everything works fine .
Cheers
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic