Tuesday, February 7, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

Java’s JOptionPane showOptionDialog by Example

learningcode_x1mckf by learningcode_x1mckf
September 12, 2022
in Java
0
Java’s JOptionPane showOptionDialog by Example
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Easy methods to use the JOptionPane’s showOptionDialog technique

Java’s JOptionPane supplies a easy method to learn enter from the person and show data again.

There are various useful capabilities included within the JOptionPane’s listing of strategies, together with:

  • showInputDialog
  • showMessageDialog
  • showConfirmDialog
  • showOptionDialog

Every of those capabilities is comparatively straight ahead except the JOptionPane’s showOptionDialog technique.

JOptionPane showOptionDialog examples

To assist demystify essentially the most difficult of those, let’s check out a number of totally different JOptionPane showOptionDialog examples.

The JOptionPane’s showOptionDialog technique requires an array of textual content Strings.

Every ingredient within the array is displayed as a clickable button. When a button is clicked, the home windows closes and the array index of the ingredient chosen is returned to this system.

Right here’s a easy showOptionDialog  instance. A picture of the dialog field this code generates may be seen beneath.

import javax.swing.*;
public class ShowOptionDialogExample 

  public static void principal(String[] args) 
    /* Easy JOptionPane ShowOptionDialogJava instance */    
    String[] choices =  "rock", "paper", "scissors" ;
    var choice = JOptionPane.showOptionDialog(null, "Choose one:", "Let's play a recreation!", 
                                                      0, 3, null, choices, choices[0]);
    if (choice == 0) 
      JOptionPane.showMessageDialog(null, "You selected rock!");
    
    if (choice == 1)  
      JOptionPane.showMessageDialog(null, "You selected paper.");
    
    if (choice == 2)  
      JOptionPane.showMessageDialog(null, "You selected scissors!");
    
  


Java’s showOptionDialog technique defined

These are the 2 most vital traces of code from the showOptionDialog instance above:

String[] choices =  "rock", "paper", "scissors" ;
var choice = JOptionPane.showOptionDialog(null, "Choose one:", "Let's play a recreation!", 
                                                      0, 3, null, choices, choices[0]);

The Swing JOptionPane showOptionDialog can take customized enter from the person.

The array named choices defines the three fields to show within the dialog field.

The array is then handed because the seventh of eight parameters required by the JOptionPane’s showOptionDialog technique.

When the dialog field seems, the names of the weather within the array seem as clickable buttons.

The array index of the button clicked is returned to this system.

The showOptionDialog’s many arguments

The truth that the showOptionDialog operate takes eight parameters could make it each intimidating to make use of and obscure.

Listed here are the eight fields the showOptionDialog technique defines, together with how they can be utilized in your software:

  1. The primary parameter is the parentComponent, which may be set to null if the dialog field is to be rendered independently
  2. The second parameter is the immediate to be displayed contained in the dialog field
  3. The third parameter is the textual content to be displayed within the title of the dialog field
  4. The fourth parameter is the optionType, which would be the quantity 0,1,2, or 3, to specify whether or not sure, no or cancel buttons will seem
  5. The fifth parameter is the messageType, which would be the quantity 0,1 or 2 or 3 to specify whether or not an error, information, warning, query or default message icon will seem
  6. The sixth parameter is an non-obligatory picture icon to show within the message field
  7. The seventh parameter is the array of choices to make use of
  8. The both parameter is the array object to pick out because the default worth

Easy showOptionDialog defaults

To simplify the usage of the JOptionPane’s showOptionDialog technique, each parameter besides the worth of the array may be set to null or zero and the dialog field will operate correctly:

String[] choices =  "brownie", "pie", "cake" ;
int response = JOptionPane.showOptionDialog(null, null, null, 0, 0, null, choices, null);

Moreover, the JOptionPane class defines enums for the optionType and messageType parameters, as demonstrated beneath:

String[] choices =  "brownie", "pie", "cake" ;
int response = JOptionPane.showOptionDialog(null, null, null, 
                                            JOptionPane.DEFAULT_OPTION, 
                                            JOptionPane.QUESTION_MESSAGE, 
                                            null, choices, null);

Superior JOptionsPane’s showOptionDialog instance

To take advantage of use of the JOptionsPane’s showOptionDialog technique, a developer ought to make the most of the entire accessible parameters.

The next superior JOptionsPane’s showOptionDialog instance takes benefit of every parameter, including a pink JFrame upon which to show the dialog field, and a picture icon that includes the Java mascot duke. A picture of the JOptionPane this code creates may be seen beneath.

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

public class JOptionPaneExample 

    public static void principal(String[] args) 
        String[] choices =  "brownie", "pie", "cake" ;
        int x = JOptionPane.showOptionDialog(null, null, null, 
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, choices, null);
        
        JFrame body = new JFrame();
        body.getContentPane().setBackground(Coloration.blue);
        body.setBounds(300, 300, 500, 350);
        body.setVisible(true);

        int dessert = JOptionPane.showOptionDialog(body, 
                "Which dessert would you want?", "Choose a dessert", 
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                new ImageIcon("C:_toolsjava-duke-sm.jpg"), 
                choices, choices[0]);
        
        if (dessert == 0) 
            JOptionPane.showMessageDialog(body, "You selected a brownie.");
        
        if (dessert == 1) 
            JOptionPane.showMessageDialog(body, "You selected pie.");
        
        if (dessert == 2) 
            JOptionPane.showMessageDialog(body, "You selected cake.");
        
    


When the JOptionPane showMessageDialog() operate runs, the dialog field seems on high of a blue body, with Java’s Duke mascot because the icon.

Swing JOptionPane showOptionDialog

When absolutely parameterized, the JOptionPane showOptionDialog shows on a body with a customized icon.

Garnering person enter in Java applications may be troublesome, however the JOptionPane’s showMessageDialog() makes it attainable to create partaking, visible enter varieties that can enhance the expertise of your customers.

 



Source link

You might also like

JobRunr, the Java Scheduler Library, Released Version 6.0 – InfoQ.com

Oracle Criticized Over Price Change for New Oracle Java SE Licenses – Slashdot

Oracle per-employee Java licensing could benefit rivals – InfoWorld

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

JobRunr, the Java Scheduler Library, Released Version 6.0 – InfoQ.com

by learningcode_x1mckf
February 7, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JobRunr, the Java Scheduler Library, Released Version 6.0  InfoQ.com Source link

Read more

Oracle Criticized Over Price Change for New Oracle Java SE Licenses – Slashdot

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle Criticized Over Price Change for New Oracle Java SE Licenses  Slashdot Source link

Read more

Oracle per-employee Java licensing could benefit rivals – InfoWorld

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle per-employee Java licensing could benefit rivals  InfoWorld Source link

Read more

Java Classes and Objects: How to use classes and objects in Java … – Medium

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Classes and Objects: How to use classes and objects in Java ...  Medium Source link

Read more

Java's James Gosling on Fame, Freedom, Failure Modes and Fun – The New Stack

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java's James Gosling on Fame, Freedom, Failure Modes and Fun  The New Stack Source link

Read more
Next Post
The 23 year-old C++ developers with three job offers over $500k

The 23 year-old C++ developers with three job offers over $500k

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

GraalVM Java Compilers Join OpenJDK in 2023, Align with OpenJDK Releases and Processes

GraalVM Java Compilers Join OpenJDK in 2023, Align with OpenJDK Releases and Processes

December 22, 2022
Should You Update to the Latest Python Bugfix Version? – Real Python

Should You Update to the Latest Python Bugfix Version? – Real Python

November 2, 2022
JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

February 1, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • JobRunr, the Java Scheduler Library, Released Version 6.0 – InfoQ.com
  • An Introduction to Lodash and Its Benefits for JavaScript Developers – MUO – MakeUseOf
  • "Used properly, Python is not slower than C++" – eFinancialCareers (US)

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?