I have an existing frame with 2 panels on it. When the user presses one of the JButtons found on the JPanel, I need to handle the event by opening a JDialog box that allows the user to set settings - settings that include 3 sliders and 2 drop lists, when the user saves the settings I need to pull them from this dialog and use them later. I'm new to GUI and for the past 2 and a half days I have searched for many ways to create this such of thing, and I have succeeded to make a slider and a drop list each one by its own, but I still haven't figured out how to handle an event while a frame is open, because from what I have seen I need a new JFrame, but what happens to the one alrady open? In addition, how can I make this dialog with these sliders and drop lists?
Here is the code I wrote for the slider:
public void setSlider()
{
optionPane = new JOptionPane();
JSlider slider = getSlider(optionPane);
JSlider slider2=getSlider(optionPane);
optionPane.setMessage(new Object[] { "Select animal's speed: ", slider });
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
dialog = optionPane.createDialog(Frame, "Set speed");
dialog.pack();
dialog.setVisible(true);
int speed = (int) optionPane.getInputValue();
}
static JSlider getSlider(final JOptionPane optionPane) {
JSlider slider = new JSlider();
slider.setMajorTickSpacing(1);
slider.setMaximum(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;
}
I hope this post is specified enough. Thanks!!!
As you want the dialog's results "only after it closes," simply collect the results at that time. Starting from this complete example, the following update produces the output and appearance shown:
…
JSlider slider = new JSlider();
panel.add(slider);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText()
+ " " + slider.getValue());
} else {
…
One 1234.56 9876.54 50