> 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