Search code examples
javaswingjoptionpane

How to "translate" this scanner input into a JOptionPane showOptionDialog


I am trying to make my code more user friendly. I have this part of code and would like to know how can I convert it to joptionpane. I found this int result = JOptionPane.showConfirmDialog(frame, "Continue printing?"); but seems a bit weird to use another jframe for it.

System.out.println("Make more selections? Type Yes or No");

Scanner scanre = new Scanner( System.in );
String selecend;
selecend = scanre.next();
if(selecend.equalsIgnoreCase("Yes")) {
    System.out.println("Enter next selection: ");
    query();
};      

Solution

  • more user friendly

    What about the next?

    private Something showMessage() {
    
        // null for 'console' mode or this if the enclosing type is a frame
        Component parentComponent = null; 
        Object message = "Make more selections?";
        String title = "Message";
        int optionType = JOptionPane.YES_NO_OPTION; // 2 buttons
        int messageType = JOptionPane.QUESTION_MESSAGE; // icon from style
        Icon icon = null;
    
        // String in the buttons!
        Object[] options = { "Yup!", "Nope!" };
    
        // option saves the index 'clicked'
        int option = JOptionPane.showOptionDialog(
                parentComponent, message, title, 
                optionType, messageType, icon,
                options, options[0]);
    
        switch (option) {
        case 0:
            // button with "Yup!"
            break;
        case 1:
            // button with "Nope!"
            break;
        default:
            // you close the dialog or press 'escape'
            break;
        }
    
        return Something;
    }
    

    enter image description here