简体   繁体   中英

As far as I know showinputdialog returns an output of type string but it gives me an error that it cannot convert the object to a String

 String val = JOptionPane.showInputDialog(null,
            "Search item to Edit ",
            "Warning",
            JOptionPane.QUESTION_MESSAGE,
            null,
            new String[]{"Item name", "Item no.","Price"},
            "");

Error:

incompatible types: Object cannot be converted to String

With that much parameters, the method showInputDialog doesn't return a String but an Object as stated in the javadoc

As suggested in the Swing tutorial , you simply have to cast the result to String.

String val = (String) JOptionPane.showInputDialog(null,
        "Search item to Edit ",
        "Warning",
        JOptionPane.QUESTION_MESSAGE,
        null,
        new String[]{"Item name", "Item no.","Price"},
        "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM