Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help correcting a button behavior in swing

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, how did you get the ok button in the table?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic