Java Reference
In-Depth Information
Our dialog box will not allow users to start the client application until they have entered
the required details; however, the required details change depending on the client application
modeā€”in stand-alone mode, we only need to know the URI of the data file. But in networked
mode we need to know the URL of the server, its port number, and the type of server we are
connecting to. The constructor starts by assuming that if we are building a dialog box for
stand-alone mode, then we don't need a port number or connection type. It then goes on to
create an instance of the ConfigOptions pane and add itself as an observer of the pane.
A simple OK/Cancel question JOptionPane is then created, with the common pane as its
main object. We then override the buttons that will be displayed so that we can enable the
connect button when the user has entered the required data.
If the user closes the dialog box rather than clicking the Connect or Exit button, we want
to treat it as though they had clicked the Exit button. So we set the dialog box to do nothing on
close, and then we add a window listener to the dialog box.
Listing 8-30. The DatabaseLocationDialog
public DatabaseLocationDialog (Frame parent, int connectionMode) {
// the port and connection type are irrelevant in standalone mode
if (connectionMode == ApplicationMode.STANDALONE_CLIENT)
validPort = true;
validCnx = true;
}
configOptions = (new ConfigOptions(connectionMode));
configOptions.getObservable().addObserver(this);
options = new JOptionPane(configOptions,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
connectButton.setActionCommand(CONNECT);
connectButton.addActionListener(this);
connectButton.setEnabled(false);
exitButton.setActionCommand(EXIT);
exitButton.addActionListener(this);
options.setOptions(new Object[] {connectButton, exitButton});
dialog = options.createDialog(parent, TITLE);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(this);
dialog.setVisible(true);
}
Search WWH ::




Custom Search