Results 1 to 4 of 4

Thread: Message Box example for Java Swing

  1. #1
    Join Date
    Jul 2009
    Posts
    1,118

    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.

  2. #2
    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

  3. #3
    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);
      }
    }

  4. #4
    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");
        }
      }
    }

Similar Threads

  1. Applets V/s Swing in JAVA
    By "Dritan" in forum Software Development
    Replies: 3
    Last Post: 14-12-2010, 09:04 AM
  2. Print in java using Swing
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 09:50 AM
  3. Java Swing and MySQL
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 22-09-2009, 08:50 AM
  4. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  5. Java swing help with repaint()
    By grudge in forum Software Development
    Replies: 4
    Last Post: 23-07-2009, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,034,470.04632 seconds with 17 queries