Reputation: 12249
I'm trying to put a JSpinner in a JOptionPane as follows,
SpinnerModel saModel = new SpinnerNumberModel(11, 1, 36, 1);
JSpinner saSpinner = new JSpinner(saModel);
Dimension d = saSpinner.getSize();
d.width = 20;
saSpinner.setSize(d);
Object[] message = { "Choose the key number for sa.", saSpinner };
JOptionPane optionPane = new JOptionPane(message,
JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(frame, "Change Sa Key");
dialog.setVisible(true);
This works, but the only issue is that the JSpinner fills the width of the Dialog regardless of the size I set. I've tried using setPreferredSize() as well. How do I fix this?
Upvotes: 1
Views: 638
Reputation: 285405
Why not instead just put it in a JPanel?
SpinnerModel saModel = new SpinnerNumberModel(11, 1, 36, 1);
JSpinner saSpinner = new JSpinner(saModel);
Dimension d = saSpinner.getSize();
d.width = 20;
saSpinner.setSize(d);
// Object[] message = { "Choose the key number for sa.", saSpinner };
JPanel panel = new JPanel();
panel.add(new JLabel("Choose the key number for sa:"));
panel.add(saSpinner);
JOptionPane optionPane = new JOptionPane(panel,
JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(frame, "Change Sa Key");
dialog.setVisible(true);
Though myself, I don't know that I'd create a JDialog for this, but instead would simply display the JOptionPane.showConfirmDialog(...)
method:
SpinnerModel saModel = new SpinnerNumberModel(11, 1, 36, 1);
JSpinner saSpinner = new JSpinner(saModel);
Dimension d = saSpinner.getSize();
d.width = 20;
saSpinner.setSize(d);
JPanel panel = new JPanel();
panel.add(new JLabel("Choose the key number for sa:"));
panel.add(saSpinner);
int selection = JOptionPane.showConfirmDialog(frame, panel, "Change Sa Key",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (selection == JOptionPane.OK_OPTION) {
System.out.println("Sa Key is: " + saModel.getValue().toString());
}
Upvotes: 5