logo
down
shadow

Differentiating between no input (null String) and Cancel button in JOptionPane.showInputDialog()?


Differentiating between no input (null String) and Cancel button in JOptionPane.showInputDialog()?

By : Steven Pipes
Date : November 22 2020, 02:42 PM
I hope this helps . You could use "" for the initialSelectionValue parameter to differentiate between OK and Cancel:
code :
String s = JOptionPane.showInputDialog(parent, "Enter a value", "");


Share : facebook icon twitter icon
How to assign a sepecifc action to the ("Cancel") button within (JOptionPane.showInputDialog) in Java?

How to assign a sepecifc action to the ("Cancel") button within (JOptionPane.showInputDialog) in Java?


By : wahyu agung
Date : March 29 2020, 07:55 AM
it fixes the issue First, you need to verify if the input is null. If not, then you use parseDouble on it.
Like this:
code :
try
{
    String i = JOptionPane.showInputDialog("Enter your num:");
    if (i != null)
        num = Double.parseDouble(i);
}
catch (NumberFormatException e)
{
    System.err.println("Error: Invalid Input!");
    JOptionPane.showMessageDialog(null, "Error: Invalid Input!",  
    "Error", JOptionPane.ERROR_MESSAGE);
}
JOptionPane showInputDialog cancel button NullPointerException

JOptionPane showInputDialog cancel button NullPointerException


By : Issac
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying unsuccessfully to trap the user into entering something for a filename. I'm just using input dialog because the path and extension are predetermined, and I need to append to the filename after the user enters it. As the title says I get NullPointerException any time the user clicks the cancel button. Since the input dialog has no way to remove the cancel button I've resorted to this method:
code :
  filename.equals(null) // Compare Object 
  filename == null // Compare Object references
Is there a way to only have the OK button in a JOptionPane showInputDialog (and no CANCEL button)?

Is there a way to only have the OK button in a JOptionPane showInputDialog (and no CANCEL button)?


By : Md Ismail Hoque
Date : March 29 2020, 07:55 AM
should help you out Just add a custom JPanel as a message to JOptionPane.showOptionDialog():
code :
String[] options = {"OK"};
JPanel panel = new JPanel();
JLabel lbl = new JLabel("Enter Your name: ");
JTextField txt = new JTextField(10);
panel.add(lbl);
panel.add(txt);
int selectedOption = JOptionPane.showOptionDialog(null, panel, "The Title", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);

if(selectedOption == 0)
{
    String text = txt.getText();
    // ...
}
Handle java cancel button in two JOptionPane showInputDialog

Handle java cancel button in two JOptionPane showInputDialog


By : Geek Coder
Date : March 29 2020, 07:55 AM
I hope this helps you . First, i am sorry maybe the title is wrong. I will explain my problem. I have one joptionpane with showinputdialog component to input server address. I want after click Cancel button, it will return to main window. But in my case, it instead to second joptionpane.
code :
private void networking() {
    server_address = setServerName();
    if(server_address == null || server_address.equals("")){
        //Handle what happens when the server name is empty (or the user clicked the cancel button. If you let the execution continue, then the try/catch block below will pop up the second jInputDialog
    }
        try {

            PORT = setPortNumber();
            if (server_address != null) {

                sock = new Socket(InetAddress.getByName(server_address) ,
                    PORT);
            } 
            else {
                SocketException sc = new SocketException();
                throw sc;
            }
...
}
Differentiating between pressing close and cancel on JOptionPane.showInputDialog

Differentiating between pressing close and cancel on JOptionPane.showInputDialog


By : seyyed mostafa mousa
Date : March 29 2020, 07:55 AM
it helps some times No, there isn't. That particular API does not provide enough granular information about what happened to know whether they clicked the button or clicked the "X" on the window.
You can of course make your own UI and be able to tell that way, or possibly subclass JOptionPane and add a new showInputDialog method with more knowledge of the internal state of the window.
Related Posts Related Posts :
  • IntelliJ - include pom.xml dependencies in lib directory
  • Tomcat 7 : Filtering lifecycle events
  • JSONException: Names must be strings
  • Build Spark JavaRDD List from DropResult objects
  • Include css file in richlet with zk
  • Jackson CSV's WRAP_AS_ARRAY
  • Priority for a web request in Java web applications
  • Is there a better way to format this timestamp to ISO8601?
  • Issue with Spring Scope
  • How to declare and use an array of dictionaries from Python in Java and filter them?
  • Wrong weekdays from Calendar.getDisplayName()
  • ClassNotFoundException when creating new ClassPathXmlApplicationContext
  • Java Method Call from Thread
  • Remove background noise from image to make text more clear for OCR
  • PostgreSQL query always return true
  • How to connect FX controller with main app
  • Is there a difference between these two code snippets, and if so what?
  • When two interfaces have conflicting return types, why does one method become default?
  • Updating database throws errors in To Do List app
  • Where does the url come from?
  • Dagger 2 issue overriding single provides annotated method from a module in a library which app uses
  • Better way then If(x instanceof y) in Java?
  • how to debug maven enunciate plugin tomcat web service
  • Spring Context Initialization fails when upgrading to version 4.2.3
  • spring boot cannot connect to rabbitmq
  • How to Run Unit Test in Parallel Across All Modules Using Maven?
  • Getting the next Value in a ArrayList(Not working)
  • How to wait for a MouseListener mouse press?
  • Java RMI: Blocking for void method return?
  • Get Table Properties out of Hive using Java API
  • Read CSV file and write to another CSV - ArrayIndexOutOfBoundsException and pattern difficuties
  • Set LineChart(javafx) style to a child element
  • ArrayList how catch exception on one item from constructor
  • java.lang.NumberFormatException: For input string: " 1"
  • Dekker's Algorithm not working fine for three processes
  • Maven javadoc plugin - package does not exist
  • NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0
  • NullPointerExcpetion on findViewById
  • Java ArrayList initialization
  • Problems on making a Tic Tac Toe Game in Java
  • how to set proxy server details on WebServiceTemplate
  • How can I get a Class object form primitive?
  • How to count rows and Columns of excel sheet in Java
  • Should I split up texture atlas?
  • JavaFX freezing issue
  • What is the JavaFX equivalent of androids AsyncTask?
  • Zenity bash command not working with Java
  • GSON serialize multiple properties to sub object
  • How to convert some elements that satisfy a particular condition in a 2d array into a list in java?
  • Setting LinkedList nodes to null
  • Java Swing ignore text size on JButton
  • Collision detection for separate class instances - Processing Java
  • handle unreal numbers, jump to catch
  • Java - 100 decimal places in variable
  • Cancel previously shown toast before showing new toast
  • projectile motion sim - 'illegal start of type' error
  • How to print out the whole contents of a stack?
  • How to find variable name from a list of class in a package
  • how can i make this dialog close when socket is accepted?
  • I have created a small book program, I am new to java. Can't find what the error or mistake in this code?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co