billiout
billiout

Reputation: 705

JOptionPane with custom array JButtons

I have this code but it doesnt work with the array of buttons

        JButton[] option = new JButton[2];
        option[0].setText("sad");
        option[0].setEnabled(true);
        option[1].setText("sasdd");
        option[1].setEnabled(true);
        Object[] options = {option[0], option[1]};
        int i = JOptionPane.showOptionDialog(null, "SADASD", "dfgdfgg", 0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

If I make single button works perfect but I need to be on an array for controling some of them.

Upvotes: 0

Views: 347

Answers (2)

SuRu
SuRu

Reputation: 739

String[] options = {"Java", "C", "C++", "C#"};
String msg = "What is your favourite language";
String title = "Language Poll";
int result = JOptionPane.showOptionDialog(parentComponent, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(result == options[0] {
    //do some thing
} else {
.......
}

....

Upvotes: 0

prmottajr
prmottajr

Reputation: 1824

Before setting values you have to create JButton objects like this:

option[0] = new JButton();
option[1] = new JButton();

Write this lines just after creating the array.

Upvotes: 2

Related Questions