0

How can I get the value of an input field when I click a button?

For example I need the value of textName input from the PersonalInfo class to be used in another class called GenerateRDF

This is my code:

public class PersonalInfo extends JPanel {
    private void initialize() {
        ....
        JTextPane textName = new JTextPane();
        textName.setBounds(95, 36, 302, 20);
        panel.add(textName);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // send the value of `textName` to GenerateRDF
               GenerateRDF generator = new GenerateRDF();
               generator.setRDF();
            }
        }); 
   }
}

public class GenerateRDF {
      public void setRDF() {
        String personURI    = "http://localhost/amitkumar";
        String fullName = textName;

        // print here the value received from the `PersonalInfo` class
        System.out.println(fullName);
        Model model = ModelFactory.createDefaultModel();

        Resource node = model.createResource(personURI)
                 .addProperty(VCARD.FN, fullName);
        model.write(System.out);
    }
}
  • Please refer to this similar example: http://stackoverflow.com/questions/10996479/how-to-update-a-textview-of-an-activity-from-another-class – Jou Mar 23 '17 at 19:50
  • Do you want to create a `GenerateRDF ` object when the button is pressed and send it the text of the `textName` field ? – Titus Mar 23 '17 at 19:52
  • @Titus , yes, exactly! I updated my question – Laura Cîrstea Mar 23 '17 at 19:54

4 Answers4

0

You could use the getText() method in your buttons actionListener to store the input in a variable when it is clicked. Then pass this variable to your GenerateRDF class

pogba123
  • 79
  • 1
  • 3
  • 9
0

How can I get the value of an input field when I click a button?

There exists a method called getText() which enables you to retrieve the text of your textName variable.

Example:

JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        // send the value of `textName` to GenerateRDF
       String getName = textName.getText(); // retrieve the value 
       // do something with it
    }
}); 
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

JtextPane has a method you have to call... the getText method

  public void actionPerformed(ActionEvent arg0) {
                // send the value of `textName` to GenerateRDF
        String input =  texName.getText();
  }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You can do something like this:

public class PersonalInfo extends JPanel {
private void initialize() {
    ....
    JTextPane textName = new JTextPane();
    textName.setBounds(95, 36, 302, 20);
    panel.add(textName);

    JButton btnSave = new JButton("Save");
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            // send the value of `textName` to GenerateRDF
           GenerateRDF generator = new GenerateRDF();
           generator.setRDF(textName.getText());
        }
    }); 
  }
}

public class GenerateRDF {
  public void setRDF(String fullName) {
    String personURI    = "http://localhost/amitkumar";

    // print here the value received from the `PersonalInfo` class
    System.out.println(fullName);
    Model model = ModelFactory.createDefaultModel();

    Resource node = model.createResource(personURI)
             .addProperty(VCARD.FN, fullName);
    model.write(System.out);
  }
}
Titus
  • 22,031
  • 1
  • 23
  • 33