Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Passing Control

Thread view: 
Nandu - 20 Mar 2006 06:49 GMT
Hi Java Gurus
  I wrote two programs. In the first program i am reading 6 inputs
thru textfield. then i created the object of program 1 in progam 2. so
that when i run my program 2, i can read input there it self and access
them.
 But when i run my second program... it displays the window ( object
of program 1) to read the input but its not waiting to read input but
just continuing with next statements in sequence  (i.e., statements in
sequence after object of program 1)of  the progam 2 ..
 Can you please help me out... how to make my object of program 1 to
wait until all the input is read.

If i am not clear at describing the question .plz let me know.. i try
to make it clear for you
Thank you
Nandu
Rhino - 20 Mar 2006 17:28 GMT
> Hi Java Gurus
>   I wrote two programs. In the first program i am reading 6 inputs
[quoted text clipped - 10 lines]
> If i am not clear at describing the question .plz let me know.. i try
> to make it clear for you

It's difficult to suggest a solution to a problem that is only described in
words; it's a lot easier to be sure of your problem if you supply the code
(or a simplified version of the code that demonstrates the problem if your
code is complex.) Right now, I don't know if you are describing an
application, applet, servlet, or midlet, let alone what sort of GUI controls
you are using, and all of those elements will affect the answer. I also
don't know which version of Java you are using which can also affect the
answer.

But, if I had to guess, I'd assume that you are writing an application and
that you are using a reasonably current version of Java, say 1.4 or later.
If that is the case, try some Google newsgroups searches on the
comp.lang.java.* newsgroups on the words "modal" and "callback" and you will
probably find some clues to solving your problem.

If you are not writing an application, perhaps you can clarify what sort of
program you are writing and provide a simple version of the code that
demonstrates the problem so that you can get some more specific answers.

--
Rhino
Nandu - 20 Mar 2006 20:30 GMT
Hi Rhino
  Following is the typical structure of my program... where i can read
6 inputs in program 1... i have created object of program 1 in program
2 and i am running program 2. Instead of first reading all 6 inputs, it
displays the program 1 reading window( without any fields) and
immediatley displays JOptionPane. i am using Java1.5

program 1

public class Display extends JFrame{

     i have created here 2 radio buttons these radio button are
grouped using ButtonGroup
    contains 6 textfields from where i read the inputs
    two buttons OK and Cancel
    Once i fill in all the textfields i can press OK button
    All of the control have their cooresponding actionListeners
}

program 2

public Class Test extends JFrame
{
   Display display ;
   public Test()
   {
     functionn();
  }

  void function()
 {
    display = new Display();
   String str = JOptionPane.showInputDialog("Enter number of
Iterations::");
 }

}
Rhino - 20 Mar 2006 23:17 GMT
> Hi Rhino
>   Following is the typical structure of my program... where i can read
[quoted text clipped - 33 lines]
>
> }

Nandu,

I've created a very simple example, containing two classes, that illustrates
the techniques you need. The first class, Nandu1, corresponds to Program 1
and the second class, Nandu2, corresponds to Program 2. Here's the code; my
comments describing how it works follow the code:

=================================================================
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/*
* Created on 20-Mar-2006
*
*/

@SuppressWarnings("serial")
public class Nandu1 extends JDialog implements ActionListener {

   private final static String redDate = "Display the date in red.";
//$NON-NLS-1$
   private final static String greenDate = "Display the date in green.";
//$NON-NLS-1$
   private final static String blueDate = "Display the date in blue.";
//$NON-NLS-1$
   private final static String ok = "Okay"; //$NON-NLS-1$

   JRadioButton redButton = null;
   JRadioButton greenButton = null;
   JRadioButton blueButton = null;

   public Nandu1(JFrame parent, boolean modal) {

       super(parent, "Nandu1", modal); //$NON-NLS-1$

       /* Create a panel to hold the radio buttons. */
       JPanel radioButtonPanel = new JPanel();
       radioButtonPanel.setLayout(new GridLayout(3,1));

       this.redButton = new JRadioButton(redDate);
       this.redButton.setActionCommand(redDate);
       this.redButton.addActionListener(this);
       radioButtonPanel.add(this.redButton);

       this.greenButton = new JRadioButton(greenDate);
       this.greenButton.setActionCommand(greenDate);
       this.greenButton.addActionListener(this);
       radioButtonPanel.add(this.greenButton);

       this.blueButton = new JRadioButton(blueDate);
       this.blueButton.setActionCommand(blueDate);
       this.blueButton.addActionListener(this);
       radioButtonPanel.add(this.blueButton);

       ButtonGroup colorChoice = new ButtonGroup();
       colorChoice.add(this.redButton);
       colorChoice.add(this.greenButton);
       colorChoice.add(this.blueButton);

       /* Create panel to hold a control button. */
       JPanel buttonPanel = new JPanel();
       JButton okButton = new JButton(ok);
       okButton.setActionCommand(ok);
       okButton.addActionListener(this);
       buttonPanel.add(okButton);

       /* Place the radio button panel and the ok button in the GUI. */
       getContentPane().setLayout(new BorderLayout());
       getContentPane().add(radioButtonPanel, BorderLayout.CENTER);
       getContentPane().add(buttonPanel, BorderLayout.SOUTH);

   }

   public void actionPerformed(ActionEvent actionEvent) {

       String command = actionEvent.getActionCommand();

       if (command.equals(ok)) {
           /*
            * If any of the radio buttons was selected, the dialog has done
its job; dispose
            * of the dialog. Otherwise, if no radio button was selected,
display an error message
            * to force the dialog to remain until a radio button is
selected.
            */
           if (this.redButton.isSelected() | this.greenButton.isSelected()
| this.blueButton.isSelected()) {
               this.dispose();
           } else {
               JOptionPane.showMessageDialog(null, "You must choose one of
the radio buttons before you can leave this dialog.", "Error",
JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
           }
       }
   }

   /**
    * Returns a Color based on which radio button was selected.
    *
    * @return a specific color
    */
   public Color getRadioButtonChoice() {

       if (this.redButton.isSelected()) {
           return Color.RED; //$NON-NLS-1$
       }
       else
       if (this.greenButton.isSelected()) {
           return Color.GREEN; //$NON-NLS-1$
       }
       else {
           return Color.BLUE; //$NON-NLS-1$
       }
   }
}
=================================================================

=================================================================
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
/*
* Created on 20-Mar-2006
*
*/

@SuppressWarnings("serial")
public class Nandu2 extends JFrame {

   /**
    * @param args
    */
   public static void main(String[] args) {

       /*
        * Create the main application frame. Be sure to close the frame if
the user presses
        * the 'close' button in the upper right corner of the frame.
        */
       Nandu2 nandu2 = new Nandu2("Nandu2"); //$NON-NLS-1$
       nandu2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       nandu2.validate();
       nandu2.pack();
       nandu2.setVisible(true);

   }

   public Nandu2(String title) {

       super(title);

       /*
        * Invoke a dialog which obtains a color choice. Set the dialog
'modal' so that the user
        * has to finish using the dialog before control returns to the
application.
        * Be sure to ignore any attempt by the user to click on the "Close"
button in the upper
        * right corner of the dialog. The dialog will remain visible until
the user has made a
        * valid choice.
        */
       boolean modal = true;
       Nandu1 nandu1 = new Nandu1(this, modal);
       nandu1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
       nandu1.pack();
       nandu1.setVisible(true);

       /*
        * Create a label with some text on it. Set foreground color of the
label based on the
        * value chosen in the dialog.
        */
       JLabel hello = new JLabel("Hello World!");  //$NON-NLS-1$
       hello.setFont(new Font("Arial", Font.PLAIN, 48)); //$NON-NLS-1$
       Color color = nandu1.getRadioButtonChoice();
       hello.setForeground(color);

       /* Put the label in the frame. */
       getContentPane().add(hello);
   }
}
=================================================================

Nandu2 extends JFrame. Nandu1 extends _JDialog_, not JFrame. I chose a
JDialog instead of a JFrame because a JDialog can be made 'modal': when the
modal attribute of the JDialog constructor is set to true, the user is
forced to work with the dialog and cannot access any other window in the
application, including the GUI for Nandu2.

Nandu2's constructor creates the Nandu1 dialog and waits until the user has
completed using the Nandu1 GUI. The user is not going to be able to touch
any of the Nandu2 GUI until the Nandu1 dialog is dismissed; in effect,
Nandu2 cannot proceed until Nandu1 is finished.

There are only two ways to get rid of Nandu1 the way it is written:
1. Click on the Close button in the top right hand corner of the dialog (the
big red button with the white X on it Windows).
2. Click on the Okay button in the Nandu1 GUI.
In this particular case, the first of these two options is disabled; this
was accomplished via this line in Nandu2:

   nandu1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Therefore, the _only_ (nice) way to end the Nandu1 dialog is to click on the
Okay button. (Of course, you could kill the process which is running the
application but that's not a 'nice' way.)

In order to make sure that something productive is done before the Okay
button is clicked, the actionPerformed() method of Nandu1 ensures that one
of the radio buttons is selected; if a radio button is selected, the dialog
is 'disposed'. See the dispose() method from the JDialog class for the full
details but, basically, dispose() releases some resources and simply makes
the dialog invisible without destroying it. This is important because the
color corresponding to the radio button will be determined within Nandu2
after the user has clicked the Okay button; if the dialog has actually been
destroyed, it won't be possible to determine which color was chosen.

If no radio button was selected before the okay button was clicked, the
actionPerformed() code will detect that fact and display an error message
telling the user to select one of the radio buttons.

Once the Nandu1 dialog has been successfully disposed, the Nandu2
constructor creates a JLabel and asks Nandu1 what color to set the text on
that label. Nandu2 invokes the public method getRadioButtonChoice() in
Nandu1 (remember, Nandu1 still exists but is invisible), and returns a Color
based on the radio button which was chosen. Nandu2 sets the foreground color
of the label to the color obtained via the getRadioButtonChoice() method.
Then the label is added to the Nandu2 frame and the frame is displayed with
the JLabel in it.

To keep the example small, I didn't provide any buttons in Nandu2 so you can
simply click on the Close button, which is _not_ ignored for Nandu2, to end
Nandu2. A "real" application would probably have a tool bar with an exit
option on it to end the application.

Program 1 will ask for different input than Nandu1 and Program 2 will do
different things with that input than Nandu2 but the basic technique in
these examples should work for you.

--
Rhino
Nandu - 21 Mar 2006 21:31 GMT
thank you very much rhino... i appreciate your help .. thanks agian
Nandu - 21 Mar 2006 21:31 GMT
thank you very much rhino... i appreciate your help .. thanks agian
Nandu - 21 Mar 2006 21:31 GMT
thank you very much rhino... i appreciate your help .. thanks agian
Rhino - 22 Mar 2006 14:46 GMT
> thank you very much rhino... i appreciate your help .. thanks agian

You're welcome! It was good practice for me to review these techniques :-)

--
Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.