Search code examples
javaswingjoptionpane

JOptionPane button arrangement


When I use JOptionPane, the options are contained in a drop-down menu. But I want the options are listed as buttons, so that the user can directly click the buttons without clicking the drop-down button first.

My current code:

    newHero.setFaction((Faction) JOptionPane.showInputDialog(null,
            message,
            title,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            myChoices,
            myChoices[0]
    ));

How do I display the options as buttons in a arrangement similar to FlowLayout?


Solution

  • Try using a option dialog instead of an input dialog...

    enter image description here

    String[] myChoices = {"Always", "Yes", "No", "Never"};
    
    JOptionPane.showOptionDialog(
        null, 
        "Happy", 
        "Happy", 
        JOptionPane.OK_OPTION, 
        JOptionPane.INFORMATION_MESSAGE, 
        null, 
        myChoices, 
        myChoices[0]);