Monday 9 October 2017

CBSE Class 12 - Informatics Practices - Java GUI Programming – Tour III (Q and A) (#cbseNotes)

Java GUI Programming – Tour III (Q and A)JOptionPane : Built-In Dialog of JAVA

CBSE Class 12 - Informatics Practices - Java GUI Programming – Tour III (Q and A) (#cbseNotes)


JOptionPane allows to create pop-up window or dialog with predefined style.

It provides following types of Dialogs which can be used as per requirement.


Dialog Type: MessageDialog
Method used: showMessageDialog();
It is used to inform user by displaying a message. It includes OK button only.


Input Dialog
Method: showInputDialog();
Used to get user input using Text Field.

Confirm Dialog
Method: showConfirmDialog();
Used to ask a user to confirm some information with Yes/No or OK/Cancel buttons.




Q: Name the library or package to include in order to use JOptionPane.

Answer: To use the JoptionPane dialog control in the application, we must import the following class(s).

import.javax.swing.JOptionPane;

Q: What is the syntax of JOptionPane.showXXX methods? Also provide examples of each.

Answer:
In general, the following syntax of methods along with optional parameters can be used:

JoptionPane.show......([Frame name],<“Message”> [,<“Title”>] );

Frame Name: Generally null is used to indicate current frame.
Message: User given string to covey the message.
Title: Text to be displayed as Title on the Title bar.


Examples are:

JOptinPane.showMessageDialog(null,”JAVA Welcomes You”);

JOptinPane.showMessageDialog (null,”My Name is “+name);

String n= JOptinPane.showInputDialog (null,”Enter your Name ?”);

String n = JOptinPane.showInputDialog (null,”Enter your Name ?”, “Input Name”);

int ch=JOptinPane.showConfirmDialog(null,”Want to exit ?”);

int ch=JOptinPane.showConfirmDialog(null,”Want to exit ?”,”Confirm ?”);


Q: What type of value is returned by JOptionPane.showInputDialog()?

Answer: ShowInputDialog() returns a string value which can be directly assigned on a string type variable. You may use parseXXX() method to convert it into other data types like int or float etc. for further use in the application.
e.g.
String strVal = JOptionPane.showInputDialog(null, ”Enter Name? ”);


Q: Write a Java program that accepts value of a radius using ShowInputDialog() and computes the area of the circle. showMessageDialog().

Answer:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.JOptionPane;
/**
 *
 * @author eduvictors.com
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Demo App");

        jLabel1.setText("Compute Area of Circle");

        jLabel2.setText("Demo Of JOptionPane ShowXXX  apis");

        jButton1.setText("ClickMe");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(jButton1)))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(41, 41, 41)
                .addComponent(jButton1)
                .addContainerGap(141, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        // Program to calculate area of circle using Input dialog
        String str=JOptionPane.showInputDialog(null, "Enter radius of circle ?");
        int radius=Integer.parseInt(str);
        float area=(22/7)*(radius*radius);
        JOptionPane.showMessageDialog(null, "Area of circle="+area);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

Program Output:
CBSE Class 12 - Informatics Practices - Java GUI Programming – Tour III (Q and A) (#cbseNotes)


CBSE Class 12 - Informatics Practices - Java GUI Programming – Tour III (Q and A) (#cbseNotes)


No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.