JOptionPane demo
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ManualDisplayPopup {
public static void main(String args[]) {
JFrame frame = new JFrame("NoButton Popup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Ask");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Component source = (Component) actionEvent.getSource();
JOptionPane optionPane = new JOptionPane("Continue printing?",
JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JDialog dialog = optionPane.createDialog(source,
"Manual Creation");
dialog.show();
int selection = OptionPaneUtils.getSelection(optionPane);
System.out.println(selection);
}
};
button.addActionListener(actionListener);
Container contentPane = frame.getContentPane();
contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
final class OptionPaneUtils {
private OptionPaneUtils() {
}
public static JOptionPane getNarrowOptionPane(int maxCharactersPerLineCount) {
// Our inner class definition
class NarrowOptionPane extends JOptionPane {
int maxCharactersPerLineCount;
NarrowOptionPane(int maxCharactersPerLineCount) {
this.maxCharactersPerLineCount = maxCharactersPerLineCount;
}
public int getMaxCharactersPerLineCount() {
return maxCharactersPerLineCount;
}
}
return new NarrowOptionPane(maxCharactersPerLineCount);
}
public static JButton getButton(final JOptionPane optionPane, String text,
Icon icon) {
final JButton button = new JButton(text, icon);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Return current text label, instead of argument to method
optionPane.setValue(button.getText());
}
};
button.addActionListener(actionListener);
return button;
}
public static JSlider getSlider(final JOptionPane optionPane) {
JSlider slider = new JSlider();
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JSlider theSlider = (JSlider) changeEvent.getSource();
if (!theSlider.getValueIsAdjusting()) {
optionPane.setInputValue(new Integer(theSlider.getValue()));
}
}
};
slider.addChangeListener(changeListener);
return slider;
}
public static int getSelection(JOptionPane optionPane) {
// Default return value, signals nothing selected
int returnValue = JOptionPane.CLOSED_OPTION;
// Get selected Value
Object selectedValue = optionPane.getValue();
System.out.println(selectedValue);
// If none, then nothing selected
if (selectedValue != null) {
Object options[] = optionPane.getOptions();
if (options == null) {
// default buttons, no array specified
if (selectedValue instanceof Integer) {
returnValue = ((Integer) selectedValue).intValue();
}
} else {
// Array of option buttons specified
for (int i = 0, n = options.length; i < n; i++) {
if (options[i].equals(selectedValue)) {
returnValue = i;
break; // out of for loop
}
}
}
}
return returnValue;
}
}
Related examples in the same category