Create JLabel 's Text Using JFrame in Java

Posted By: Java Examples - 8:37 PM

Share

& Comment

Description of this program :
Message dialog box is used to display informative messages to the user. In this section we will use JOptionPane class to display the message Dialog box. Our program display ">>Click<< " button on the window and when user clicks on it program displays Message box with "OK" button and message "You Clicked the Button"

JOptionPane Class:
In non-swing application we were using System.in class for input or output some text or numeric values but now in the swing application we can use JOptionPane to show the output or show the message. This way of inputting or outputting works very efficiently in the Swing Applications. The window for showing message for input or output makes your application very innovative.


JOptionPane class is available in the javax.swing.*; package. This class provide various types of message dialog box as follows:
  • A simple message dialog box which has only one button i.e. "Ok". This type of message dialog box is used only for showing the appropriate message and user can finish the message dialog box by clicking the "Ok" button.
  • A message dialog box which has two or three buttons. You can set several values for viewing several message dialog box as follows:
    1.)  "Yes" and "No"
    2.)  "Yes", "No" and "Cancel"
    3.)  "Ok", and "Cancel"
  • A input dialog box which contains two buttons "Ok" and "Cancel".
The JOptionPane class has three methods as follows:
  • showMessageDialog(): First is the showMessageDialog() method which is used to display a simple message.
  • showInputDialog(): Second is the showInputDialog() method which is used to display a prompt for inputting. This method returns a String value which is entered by you.
  • showConfirmDialog(): And the last or third method is the showConfirmDialog() which asks the user for confirmation (Yes/No) by displaying message. This method return a numeric value either 0 or 1. If you click on the "Yes" button then the method returns 1 otherwise 0.
Java version of JButton is given bellow :
/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author ACHCHUTHAN
 */
public class ButtonTest extends JFrame implements ActionListener {

    private JButton plainbutton;

    ButtonTest() {
        super("Testing Buttons");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        plainbutton = new JButton(">>Click<<");
        c.add(plainbutton);
        plainbutton.addActionListener(this);
        setSize(275, 100);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(ButtonTest.this, "You Clicked the Button \n JAVA90.BLOGSPOT.COM");
    }

    public static void main(String args[]) {
        ButtonTest b = new ButtonTest();
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


Output of this program :
Test Buttons 1

Test Button 2

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .