Search code examples
javaswingdefault-valuejoptionpane

joptionpane default value showinputdialog


Can i do that ? I want to set default value of my option pane is 9090.. This is my option pane code

private static int setPortNumber()
{
    String portNumber = JOptionPane.showInputDialog(frame,
            "Enter the Port number for server creation","Server Connection\n",
            JOptionPane.OK_CANCEL_OPTION);
    int PORT = Integer.parseInt(portNumber);

    return PORT;

}   

Solution

  • Yes you can do that.

    private static int setPortNumber()
    {
    
        String [] possiblePorts = { "9090", "8080", "8081" }; 
        String selectedPort = (String) JOptionPane.showInputDialog(frame, "Select the Port number for server creation", "Server Connection\n", JOptionPane.OK_CANCEL_OPTION, null, possiblePorts, possiblePorts[0]);
    
        int PORT = Integer.parseInt(selectedPort);
    
        return PORT;
    
    }
    

    In this way user need not to write just select.