JOptionPaneに渡されたカスタムボタンによって返される値を取得しようとしています。ただし、渡すボタンは値をまったく返しません。終了ボタンが押された場合にのみ、-1の値が返されます。有効または無効にしたボタンのプロパティを変更しているので、これが必要です。何らかの方法でJOptionPaneに情報を返すためのボタンが必要だと思います。何か案が?
JButton button1= new JButton("Button 1");
JButton button2= new JButton("Button 2");
button1.setEnabled(false);
int value = JOptionPane.showOptionDialog(null, "Heres a test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1);
JOptionPane.showMessageDialog(null, "You entered " + value);
Nbこれは私の前の質問に関連しています-JOptionPaneグレーアウトワンボタン
あなたが言ったようにボタンの値を設定しようとしましたが、OKまたはCANCELを返すことはありません。
ボタンの値をチェックするときはいつでも、私が設定した値を返すことはありません。
JButton button1= new JButton("Button1");
JButton button2= new JButton("Button2");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent)e.getSource());
// set the value of the option pane
pane.setValue(JOptionPane.OK_OPTION);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent)e.getSource());
// set the value of the option pane
pane.setValue(JOptionPane.CANCEL_OPTION);
}
});
if (JOptionPane.showOptionDialog(null, "Pick a button", "Pick", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1) == JOptionPane.OK_OPTION) {
JOptionPane.showMessageDialog(null, "Button1");
}
else{
JOptionPane.showMessageDialog(null, "Button2");
}
上記を参照してください。何があっても常にbutton2ポップアップが表示されます。