I have a custom JOptionPane with two buttons as follows:
AgreementPanel panel = new AgreementPanel(this); // JPanel with some JLabels and JTextFields
JOptionPane pane = new JOptionPane(panel, JOptionPane.NO_OPTION);
Object[] options = {"Accept", "Decline"};
option = pane.showOptionDialog(null, panel, "Agreement", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, panel.txtMessage);开发者_运维知识库
How can I use doClick()
on Accept
button? I need it somewhere in my program
Instead of passing options as strings, you can directly pass jbuttons to the showOptionDialog() method. Internally it checks if its a button, then it directly adds it to the button area.
So do something like this:
JButton btnAccept = new JButton("Accept");
Object[] options = {btnAccept , "Decline"};
btn.doClick();
Note that the BasicOptionPaneUI will take care of checking and adding the button rightly.
UPDATE: You can set the actionListener manually is the component is directly passed:
ActionListener al = ((BasicOptionPaneUI)optionPane.getUI()).new ButtonActionListener(1);
btnAccept.setActionListener(al);
I found the solution here: Closing a dialog created by JOptionPane.showOptionDialog() :)
精彩评论