Skip to content

Instantly share code, notes, and snippets.

@khannedy
Created August 30, 2011 08:20
Show Gist options
  • Save khannedy/1180454 to your computer and use it in GitHub Desktop.
Save khannedy/1180454 to your computer and use it in GitHub Desktop.
Mengubah Tombol JOptionPane di Java
int result = MessageBoxCreator.showQuestionBox(this, "Buka Website", "Aplikasi akan membuka website StripBandunk \nhttp://stripbandunk.com/", new String[]{"Buka Website", "Batalkan"}, 0);
if (result == 0) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI("http://stripbandunk.com/"));
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
} catch (URISyntaxException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
Logger.getLogger(Form.class.getName()).info("Anda Membatalkan Membuka Website StripBandunk");
}
int result = MessageBoxCreator.showQuestionBox(this, "Hapus", "Anda tidak akan dapat mengembalikan \ndata yang telah dihapus.", new String[]{"Hapus", "Batalkan"}, 0);
if (result == 0) {
Logger.getLogger(Form.class.getName()).info("Anda Melakukan Penghapusan");
} else {
Logger.getLogger(Form.class.getName()).info("Anda Membatalkan Penghapusan");
}
/**
* Brings up a dialog with a specified icon, where the initial
* choice is determined by the <code>initialValue</code> parameter and
* the number of choices is determined by the <code>optionType</code>
* parameter.
* <p>
* If <code>optionType</code> is <code>YES_NO_OPTION</code>,
* or <code>YES_NO_CANCEL_OPTION</code>
* and the <code>options</code> parameter is <code>null</code>,
* then the options are
* supplied by the look and feel.
* <p>
* The <code>messageType</code> parameter is primarily used to supply
* a default icon from the look and feel.
*
* @param parentComponent determines the <code>Frame</code>
* in which the dialog is displayed; if
* <code>null</code>, or if the
* <code>parentComponent</code> has no
* <code>Frame</code>, a
* default <code>Frame</code> is used
* @param message the <code>Object</code> to display
* @param title the title string for the dialog
* @param optionType an integer designating the options available on the
* dialog: <code>DEFAULT_OPTION</code>,
* <code>YES_NO_OPTION</code>,
* <code>YES_NO_CANCEL_OPTION</code>,
* or <code>OK_CANCEL_OPTION</code>
* @param messageType an integer designating the kind of message this is,
* primarily used to determine the icon from the
* pluggable Look and Feel: <code>ERROR_MESSAGE</code>,
* <code>INFORMATION_MESSAGE</code>,
* <code>WARNING_MESSAGE</code>,
* <code>QUESTION_MESSAGE</code>,
* or <code>PLAIN_MESSAGE</code>
* @param icon the icon to display in the dialog
* @param options an array of objects indicating the possible choices
* the user can make; if the objects are components, they
* are rendered properly; non-<code>String</code>
* objects are
* rendered using their <code>toString</code> methods;
* if this parameter is <code>null</code>,
* the options are determined by the Look and Feel
* @param initialValue the object that represents the default selection
* for the dialog; only meaningful if <code>options</code>
* is used; can be <code>null</code>
* @return an integer indicating the option chosen by the user,
* or <code>CLOSED_OPTION</code> if the user closed
* the dialog
* @exception HeadlessException if
* <code>GraphicsEnvironment.isHeadless</code> returns
* <code>true</code>
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static int showOptionDialog(Component parentComponent,
Object message, String title, int optionType, int messageType,
Icon icon, Object[] options, Object initialValue)
public final class MessageBoxCreator {
public static int showQuestionBox(Component parent, String title, String message,
String[] option, int defaultIndex) {
return JOptionPane.showOptionDialog(parent, message, title, JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, option, option[defaultIndex]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment