• 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

Help correcting a button behavior in swing

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

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