jayjaypg22
jayjaypg22

Reputation: 1751

get values of Jtextfields when click OK in Dialog

My need is to display a tab in a JDialog (confirmDialog or inputDialog). The tab contains 2 JTextField per row. The display works fine :

enter image description here

but I don't know how to get the values of the JTextFields.

Here is the display code :

int size = model.getCheckedApplications().size();
// une ligne par application sélectionnée
layout = new GridLayout(size + 1, 3, 5, 5);
myPanel = new JPanel(layout);
myPanel.add(new JLabel("Application"));
myPanel.add(new JLabel("Version cadre"));
myPanel.add(new JLabel("Nouvelles natures"));
for (Application app : model.getCheckedApplications()) {
    myPanel.add(new JLabel(app.getCode88()));
    JTextField versionActuelleField = new JTextField(30);
    versionActuelleField.setName("versionActuelle"
            + app.getCode88());
    versionActuelleField.setText(app
            .getVersionCadreActuelle());

    JTextField nouvellesNaturesField = new JTextField(
            30);
    nouvellesNaturesField.setName("nouvellesNatures"
            + app.getCode88());

    myPanel.add(versionActuelleField);
    myPanel.add(nouvellesNaturesField);
}
result = JOptionPane.showConfirmDialog(null, myPanel,
        "Valeurs de cette version",
        JOptionPane.OK_CANCEL_OPTION);

Then I don't know how to get the values when the user clicks on the OK Button :

if (result == 0) { // The user clicks on the ok button

Upvotes: 2

Views: 894

Answers (3)

ControlAltDel
ControlAltDel

Reputation: 35011

The values should be in the JTextFields you created:

versionActuelleField
nouvellesNaturesField

Also, you might want to look at ParamDialog, which I implemented to be a generic solution to this question.

EDIT

Yes I see now that you are creating these JTextFields in a loop. So you need to create a Collection, I'd suggest a Map<String, JTextField> where you could map all of your application names to the matching JTextField, as well as iterate over the collection to get all application names / JTextFields.

Upvotes: 0

Draken
Draken

Reputation: 3189

You need to add them to some list that you store, so you can get at them again. Since you are adding them in reference to an application, I would suggest a Map

private Map<Application, JTextField> nouvellesNaturesFields = new ArrayListMultimap<Application, JTextField>(); //Or Hashmap, if the key is unique
private Map<Application, JTextField> versionActuelleFields = new ArrayListMultiMap<Application, JTextField>();

public List<JTextField> getNouvellesNaturesFields() {
    return nouvellesNaturesFields ;
}

public List<JTextField> getVersionActuelleFields () {
    return versionActuelleFields ;
}    
//class code

for (Application app : model.getCheckedApplications()) {
    //Other code
    JTextField nouvellesNaturesField = new JTextField(
            30);
    nouvellesNaturesField.setName("nouvellesNatures"
            + app.getCode88());
    nouvellesNaturesFields.put(app, nouvellesNaturesField);

    //Other code and same for your new nature fields
}
result = JOptionPane.showConfirmDialog(null, myPanel,
        "Valeurs de cette version",
        JOptionPane.OK_CANCEL_OPTION);

Then when the user clicks the confirm button, using the property accessor getNouvellesNaturesFields()or getVersionActuelleFields() you can iterate all the fields created, like so:

for (Map.Entry<Application, JTextField> entry: myMap.entries()) {
    //Do something here
}

Or you could also get them via:

for (Application app : model.getCheckedApplications()) {
    List<JTextField> data = myMap.get(app);
    for(JTextField field : data) {
        field.getText();
    }
}

Since the key value probably won't be unique, I used an ArrayListMultiMap, but if it would be unique, then a HashMap should suffice

Upvotes: 1

Thecarisma
Thecarisma

Reputation: 1434

You assign the Jtextfield value to a string using the getText() method e.g below

String texfield = JTextField.getText();

Subsequently you use the String textfield wherever you want. And to get the right jtextfield you have to get text from the textfield you want for example you have four Jtexfield. Assuming they are JTextField1, JTextField2, JTextField3 and JTextField4. To get the value of JTextField3 you have

String texfield = JTextField3.getText();

Upvotes: 0

Related Questions