Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSpinner in JOptionPane?

I need to put a JSpinner in a JOptionPane. Here is what I've tried:

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

    public static void main(String[] args) {
        SpinnerNumberModel sModel = new SpinnerNumberModel(0, 0, 30, 1);
        JSpinner spinner = new JSpinner(sModel);
        JOptionPane.showInputDialog(spinner);
    }

Which results in:

enter image description here

How do I remove the textbox?

like image 369
David Avatar asked Apr 11 '12 13:04

David


People also ask

Can you customize a JOptionPane?

ImageIcon icon = new ImageIcon(new URL("http −//www.tutorialspoint.com/images/C-PLUS.png")); JLabel label = new JLabel(icon); JPanel panel = new JPanel(new GridBagLayout()); panel. add(label); panel. setOpaque(true); panel.

How do you parse an int in JOptionPane?

Simply use: int ans = Integer. parseInt( JOptionPane. showInputDialog(frame, "Text", JOptionPane.


1 Answers

You have to use showMessageDialog.

SpinnerNumberModel sModel = new SpinnerNumberModel(0, 0, 30, 1);
JSpinner spinner = new JSpinner(sModel);
JOptionPane.showMessageDialog(null, spinner);

For still having a cancel button, use:

int option = JOptionPane.showOptionDialog(null, spinner, "Enter valid number", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (option == JOptionPane.CANCEL_OPTION)
{
    // user hit cancel
} else if (option == JOptionPane.OK_OPTION)
{
    // user entered a number
}

Here is a screenshot on OS X:

enter image description here

like image 127
Martijn Courteaux Avatar answered Oct 06 '22 00:10

Martijn Courteaux