Message Box example for Java Swing
Hi,
I have worked with VB environment before but not much familiar with Java specially Swing components. Is using Swing for GUI is better?
What I require is to have a message box like thing in java swing. Don't have any example with me right now. If you can give me a good tutorial or an example ill be thankful to you.
Re: Message Box example for Java Swing
Swing allows you to use graphical components in Java classes. If you want to know more about this goto:
http://java.sun.com/docs/books/tutorial/uiswing/
You can also have a look at this website for free examples:
Freeware: Java Swing Message Box
Re: Message Box example for Java Swing
Create a message dialog box with different options
Code:
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("Message dialog");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component) e.getSource(), "Thank you!");
}
});
JButton button2 = new JButton("Input dialog");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = JOptionPane.showInputDialog((Component) e.getSource(), "What is your name?");
if (text != null && !text.equals("")) {
JOptionPane.showMessageDialog((Component) e.getSource(), "Hello " + text);
}
}
});
JButton button3 = new JButton("Yes no dialog");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog((Component) e.getSource(),
"Close this application?");
if (result == JOptionPane.YES_OPTION) {
System.exit(0);
} else if (result == JOptionPane.NO_OPTION) {
System.out.println("Do nothing");
}
}
});
setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
Re: Message Box example for Java Swing
How to show a message dialog box when you click on the button
showMessageDialog():
This method is used to show a message dialog box which contains some text messages. This is being used with two arguments in the program where the first argument is the parent object in which the dialog box opens and another is the message which has to be shown.
code
Code:
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("Click Me");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame,"Roseindia.net");
}
}
}