user1060187
user1060187

Reputation: 977

JOptionPane and getting text from input

I have created a button that opens a JOptionPane. It allows the user to input a string..>> String str = JOptionPane.showInputDialog How do I get the text that the user inputs into the joptionpane and use it to search through userobjects?

Many thanks

Upvotes: 2

Views: 29322

Answers (4)

NaveenB2004
NaveenB2004

Reputation: 1

Try this out!

import javax.swing.*;  
public class OptionPaneExample {

    JFrame f;

    OptionPaneExample() {
        f = new JFrame();
        String userinput = JOptionPane.showInputDialog(f, "User Input Here:");
        //condition check
        if (userinput.equals("your condition here!"){
             System.out.println("your output!")
        } else {
             //do something
        }

    }

    public static void main(String[] args) {
        new OptionPaneExample();
    }
}

I found some useful JOptionPane examples with source codes. I think it will help you...

Link : Java JOptionPane

Upvotes: 0

Sardar Faisal
Sardar Faisal

Reputation: 673

Although @JB Nizet already gave a good answer. I would like to add a short code example just for reference if someone comes looking for this problem again.

public class JOptionPaneExample

{ private double price;

private JTextField priceField;

private JLabel priceLabel;

public JOptionPaneExample()
{
    priceField = new JTextField(10);
}

public void createAndDisplayGUI()
{
    int selection = JOptionPane.showConfirmDialog(null, getPanel(), "Price Form : ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

    if (selection == JOptionPane.OK_OPTION)
    {
        price = Double.valueOf(priceField.getText());

        JOptionPane.showMessageDialog(null, "Price is : " + Double.toString(price), "Price : ", JOptionPane.PLAIN_MESSAGE);
    }
    else if (selection == JOptionPane.CANCEL_OPTION)
    {
        // Do something here.
    }
}

private JPanel getPanel()
{
    JPanel basePanel = new JPanel();
    basePanel.setOpaque(true);
    basePanel.setBackground(Color.BLUE.darker());

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(3, 2, 5, 5));
    centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    centerPanel.setOpaque(true);
    centerPanel.setBackground(Color.WHITE);

    priceLabel = new JLabel("Enter Price : ");

    centerPanel.add(priceLabel);
    centerPanel.add(priceField);

    basePanel.add(centerPanel);

    return basePanel;
}

}

The same code can be found this blog

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691645

The returned String is what the user entered, or null if the user chose to cancel:

String whatTheUserEntered = JOptionPane.showInputDialog(...);
if (whatTheUserEntered == null) {
    System.out.println("The user canceled");
}

Upvotes: 6

Theron084
Theron084

Reputation: 155

Your purpose it a bit unclear, but from how I understand it you just want to know how to the info entered, this can be done by simply calling the variable.

To see what is in the variable, use System.out.println(variable name);

Please define userobjects?

Hope this helps.

Upvotes: 0

Related Questions