Magazine
 

Tips & Tricks

 
ShowMessageDialog();
}
public ShowMessageDialog(){
JFrame frame = new JFrame(“Message
Dialog Box”);
button = new JButton(“Simple message
dialog box”);
button.addActionListener(new MyAction());
JPanel panel = new JPanel();
panel.add(button);
button = new JButton(“\”Ok/Cancel\”
message dialog box”);
button.addActionListener(new MyAction());
panel.add(button);
button = new JButton(“\”Yes/No/Cancel\”
dialog box”);
button.addActionListener(new MyAction());
panel.add(button);
frame.add(panel);
frame.setSize(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements
ActionListener{
public void actionPerformed(ActionEvent
ae){
String str = ae.getActionCommand();
if(str.equals(“Simple message dialog
box”)){
JOptionPane.showMessageDialog(null,
“Simple message dialog box.”, “javajazzup”,
1);
}
else if(str.equals(“\”Ok/Cancel\” message
dialog box”)){
if(JOptionPane.showConfirmDialog(null,
“\”Ok/Cancel\” message dialog box.”,
“javajazzup”,
JOptionPane.OK_CANCEL_OPTION) == 0)
JOptionPane.showMessageDialog(null,
“You clicked on \”Ok\” button”, “javajazzup”,
1);
else
JOptionPane.showMessageDialog(null,
“You clicked on \”Cancel\” button”,
“javajazzup”, 1);
}
else if(str.equals(“\”Yes/No/Cancel\”
 

dialog box”)){
JOptionPane.showConfirmDialog(null,
“\”Yes/No/Cancel\” message dialog box.”);
}
}
}
}

Output
If you click on the first button then the simple message box will open which holds only “Ok” button as shown below:

If you click on the second button then the confirm dialog box will open which asks for “Ok” and “Cancel”.

     
Jan 2008 | Java Jazz Up | 101
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF