3

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;

}   
vallen
  • 83
  • 3
  • 13
  • 1
    The user would probably prefer seeing a `JSpinner` (with a `SpinnerNumberModel`) for the component. You'd pass it as the object to the option pane `showMessageDialog(..)`, and query it straight afterwards in the next line of code.. E.G. as seen in [this answer](http://stackoverflow.com/a/10021773/418556). – Andrew Thompson Jan 04 '14 at 13:40

2 Answers2

6

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.

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
  • wow other good answer.. but there is a little error, you must add `(String)` before JOptionPane. – vallen Jan 04 '14 at 13:42
3
String portNumber = (String) JOptionPane.showInputDialog(frame,
        "Enter the Port number for server creation",
        "Server Connection\n", JOptionPane.OK_CANCEL_OPTION, null,
        null, "9090");

Read the docs

Reimeus
  • 158,255
  • 15
  • 216
  • 276