Magazine
 

Tips & Tricks

 

Code of Program: ShowDialogBox.java

import javax.swing.*;
import java.awt.event.*;
public class ShowDialogBox{
JFrame frame;
public static void main(String[] args){
ShowDialogBox db = new ShowDialogBox();
}
public ShowDialogBox(){
frame = new JFrame(“Show Message Dialog”);
JButton button = new JButton(“Show
Message”);
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements
ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog
(frame,”JavaJazzUp”);
}
}
}

Output:
When you run the program, following window will be displayed:



When you click on “Show Message” button, following Message is displayed:


 

II. Show message and confirm dialog box

There are three types of message dialog box that you can use in your swing applications. In this section, we will display several types of
message boxes. When you run the program, it will display a frame with three buttons.

Description:

1. showMessageDialog(): Above method
shows a simple message dialog box, which holds only one button i.e. “Ok” button. This method takes four arguments in which, first is the parent object name, second is the message as string, third is the title of the message dialog box as string and the last is the type of the
message dialog box.

2. showConfirmDialog(): Above method
asks from the user by displaying a message dialog box, which contains more than one button. Depending on the parameter passed it can be “Ok” and “Cancel” or “Yes”, “No”and “Cancel”. This method returns the integer value.

Code of Program:

ShowMessageDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ShowMessageDialog{
JButton button;
public static void main(String[] args){
ShowMessageDialog md = new


Jan 2007 | Java Jazz Up | 100
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