Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 31-07-2009
Member
 
Join Date: Jul 2009
Posts: 1,113
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.
Reply With Quote
  #2  
Old 31-07-2009
Member
 
Join Date: Apr 2008
Posts: 33
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
Reply With Quote
  #3  
Old 31-07-2009
Member
 
Join Date: May 2008
Posts: 32
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);
  }
}
Reply With Quote
  #4  
Old 31-07-2009
Member
 
Join Date: May 2008
Posts: 44
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");
    }
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Message Box example for Java Swing"
Thread Thread Starter Forum Replies Last Post
Applets V/s Swing in JAVA "Dritan" Software Development 3 14-12-2010 09:04 AM
Print in java using Swing Sheenas Software Development 3 18-11-2009 09:50 AM
Java Swing and MySQL shelton141 Software Development 1 22-09-2009 08:50 AM
drawPolygon in java Swing? JagdishP Software Development 3 07-08-2009 11:33 PM
Java swing help with repaint() grudge Software Development 4 23-07-2009 03:41 PM


All times are GMT +5.5. The time now is 07:38 AM.