Ronan’s Blog


Random thoughts of an Irish Software Architect in the GIS Industry.

Handy way to add extra custom Option Buttons into a JOptionPane

Posted in Java by Ro on the June 8th, 2007

private void showMyPopup()
{
//Object accessed iunside inner class must be final
final MyObject myo = new MyObject();

//Create Buttons
JButton btnOK = new JButton( “Ok” );
JButton btnCancel = new JButton( “Cancel” );
JButton btnPreview = new JButton( “Preview” );
Object[] options = new Object[]{btnOK, btnCancel, btnPreview};

//Create JOptionPane Object
JOptionPane jop = new JOptionPane(
myo, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options );

//Create JDialog from JOptionPane Object
final JDialog jd = jop.createDialog( editorApp, “JOptionPane Tester” );

//Create ActionListener
ActionListener list = new ActionListener()
{
public void actionPerformed(ActionEvent ae){
if ( ae.getActionCommand().equals( “Ok” ) || ae.getActionCommand().equals( “Preview” ) )
{
//Do the Work when Preview or OK are pressed
myo.executePreview();
}

if ( ae.getActionCommand().equals( “Ok” ) || ae.getActionCommand().equals( “Cancel” ) )
{
//Close the Dialig when OK or Cancel Are pressed
jd.dispose();
}
}};

//Add ActionListners
btnOK.addActionListener( list );
btnCancel.addActionListener( list );
btnPreview.addActionListener( list );

//Show JDialog
jd.show();
}


Leave a Reply