aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help correcting a button behavior in swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help correcting a button behavior in swing" Watch "Help correcting a button behavior in swing" New topic
Author

Help correcting a button behavior in swing

Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I'm looking for a little assistance correcting a minor issue. When I run the following program, and click on the "search for keywords" button, if I change my mind and hit the cancel button, I would like to return to the main menu "guiChoices()". Currently, it closes the program, which I do not want as eventually, this will be a part of a much larger program. Is there a way to alter the behavior of the cancel button? Below is the code:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
1) you have most of your program in main() - main should be used to start the program only
2) way too much duplication e.g. all those JOptionPane codes, identical except for the display message
3) too much happening in one method, break it down into smaller, specific functions.

here's a re-write, it's still not the best, just a little better (and it should resolve your cancel/return problem)

Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Please forgive me. I've only been teaching myself this for about 3 weeks, so I'm making amateur mistakes.

When I compile your code, it didn't seem to want to do anything, but thanks for the effort.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> When I compile your code, it didn't seem to want to do anything

strange, I just now copied the code from the post into another java file, compiled OK,
then ran it and the optionDialog appeared.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I'm not sure what is wrong. All I'm getting on this end end is the black command prompt screen and a message stating "Press any key to continue...".
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
about the only other thing I can think of is perhaps there's another .class file that's interfering.

try another folder/directory to compile/run
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I got it to work. I had to change all of the methods to public static void, and change the array to String[] static keyword. Once I did that, it came alive. Thank you. Now, I can study the efficiency of your coding and incorporate those ideas into future projects.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Oh, how did you get the ok button in the table?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Oh, how did you get the ok button in the table?

the OK button is part of the messageDialog I used to display the scrollPane/table.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help correcting a button behavior in swing
 
Similar Threads
Keywords ?
null is java keywork or not?.
need help with double and integer code
Display an array in a swing type box
Display information from a database in a custom made GUI