0

I tried to create an array of my custom buttons, and add it as an option in showOptionDialog and it's actually worked out but the problem is now they don't behave like a normal yes_no buttons. What should i do to them so they would work exactly like a default one?

Here is my custom button class:

public class MenuButton extends JButton {

    MenuButton(String text) {

        super(text);
        setBorder(null);
        setContentAreaFilled(false);
        //setBackground(new Color(153,0,0));

    }

}

Buttons:

 MenuButton yes = new MenuButton("Yes");
    MenuButton no = new MenuButton("no");
    MenuButton[] yes_no = {yes, no};

And method where i am using OptionDialog:

void confirmExit() {

        int exitDecision = JOptionPane.showOptionDialog(Menu.this, "Are you sure?", ":(", 
        JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, yes_no, yes_no[1]);
        if(exitDecision == 0) { setVisible(false); dispose(); }

    }
  • 2
    What you are trying to do is not easily done using `JOptionPane`. The second last parameter in method `showOptionDialog` is meant to be an array of strings. Refer to [How to Make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) as well as this SO question: [JOptionPane Passing Custom Buttons](https://stackoverflow.com/questions/14591089/joptionpane-passing-custom-buttons) Perhaps consider creating and using a custom `JDialog` rather than `JOptionPane`. – Abra May 08 '21 at 14:31
  • Ok, works for me, thank you for your help. I just thought there is a way to easily do it with `JOptionPane`. – Artsiom Siarheyeu May 08 '21 at 15:03
  • 1
    A `JOptionPane` is a great utility class for throwing things (messages, components) on-screen. It is also very configurable and adjustable. But the moment you start thinking *"I'd just like to tweak .."* - it's time to reach for a more generic, simpler class like `JDialog` and just make it the exact way needed. – Andrew Thompson May 08 '21 at 16:11

0 Answers0