Hello everyone,
I have written my own class that shows a customized confirm dialog (code is pasted below). I need to be able to tell whether the user clicks "YES", "NO" or "CANCEL".
My main app has a panel with a JButton "Start System". When this button is clicked, the following code instantiates OperatorConfirmDialog:
public void startNetwork(){
confirmDialog = new OperatorConfirmDialog("Are you sure you want to start the network?");
System.out.println("WE MADE IT TO HERE");//problem we do not get to here until confirm dialog acked
}
I referred to the examples for JOptionPane given at:
http://java.sun.com/j2se/1.5.0/docs/api/index.html
for examples but it doesn't seem to be working for me.
Here is the code for OperatorConfirmDialog
public class OperatorConfirmDialog extends JFrame {
JOptionPane pane;
public OperatorConfirmDialog(String message){
pane = new JOptionPane();
this.setDefaultLookAndFeelDecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
pack();
//pane.getSelectionValues()
pane.showConfirmDialog(this, message, "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
Object selectedValue = pane.getValue(); //problem here
if(selectedValue == null){
//do nothing...they just killed window
}else {
for(int counter = 0, maxCounter = pane.getOptions().length; counter < maxCounter; counter++){
if(pane.getOptions()[counter].equals(selectedValue)){
//do something
}
}
}
this.dispose();
}
public JOptionPane getPane(){
return pane;
}
public static void main(String[] args) {
OperatorConfirmDialog dialog = new OperatorConfirmDialog("Please enter a valid username.");
}
}
I put a break point at the line:
if(selectedValue == null){
When I run the debugger, confirmDialog prompts me to click "YES", "NO" or "CANCEL". I click "YES" and then arrive at the breakpoint. But, selectedValue doesn't seem to have the right value....the debugger says its value is "uninitializedValue" (literally). Any ideas?
Thanks!