• 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

good coding style?

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about coding style. I have always done the following and
I am wondering if such a thing is a good coding practice or if there is
something better. Here is the scenario:
class A extends JFrame
/*this is a window with a JList and 2 JButtons(OK/CANCEL).
I have several methods: 2 that set the ActionListeners for the
two buttons(they have none of their own and must be set from the calling
class)
and one that returns the selected string from the JList.
*/
class B
/* This contains the main method which instantiates class B.
In the constructor, class A is instantiated and the ActionListeners
are defined and set for A's buttons.
main looks like this
{
B binstance = new B();
}
*/
When OK is clicked the action listener calls A's getSelection method which
returns the string selected. The action listener sets a static String in B
to the selected value(inside the actionPerformed method). The program
cannot continue until the static String is not null.
My question is this: should I call the next part of my program from inside
the action listener or should I use a loop like this:
while(true)
{
if(staticString!=null)
{
break;
}
}
/*call next part of program*/

Thanks a lot everyone.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all you have is just a list and ok,cancel button u could use the JOptionPane with the following code
Object[] possibleValues = "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
this helps keep everything in the main program and also reduces a lot of code.
the code is from
http://java.sun.com/j2se/1.4/docs/api/index.html
hope this helps
bye
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for the general question (for similar situations that can't be handled by a simple JOptionPane) - you should probably fire off the next part of the program from the ActionListener. The while loop solution you consider is referred to as a "busy wait" - it has the processor spending a lot of time doing nothing. Better to have some other method call back when you're ready. Like actionPerformed().
Additionally, unless staticString is declared volatile or is accessed using synchronized code, the while loop may never see any changes in the staticString, since they will be made from a different thread (the GUI event handling thread). Different threads can have very different views of data unless access is synchronized. You could modify your while loop to use a wait/notify protocol using B.class as the monitor (since you're looking for changes in a static variable of B). This would both synchronize the code where necessary, and provide an alternative to the busy wait. Though if you're not experienced with threads, the actionPerformed() solution is probably simpler.
reply
    Bookmark Topic Watch Topic
  • New Topic