1

How can I call a method when I press a button from a separate class?

For example, when the click event fires on the Save button I want to call the setRDF method from the other class named GenerateRDF

This is my code:

public class PersonalInfo extends JPanel { private void initialize() { JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // call the `setRDF` method here } }); } } public class GenerateRDF extends Object { public void setRDF() { String personURI = "http://localhost/amitkumar"; String givenName = "Amit"; String familyName = "Kumar"; String fullName = givenName+familyName; Model model = ModelFactory.createDefaultModel(); Resource node = model.createResource(personURI) .addProperty(VCARD.FN, fullName) .addProperty(VCARD.N, model.createResource() .addProperty(VCARD.Given, givenName) .addProperty(VCARD.Family, familyName)); model.write(System.out); } } 
1
  • Side note: extends Object is not necessary, it's implicit. All classes extend Object by default in java. And if Object is a class you created, you should rename it. Creating classes with the same name as other default java classes is the best way to confuse other developers. Commented Mar 22, 2017 at 19:58

1 Answer 1

1

You would create a new GenerateRDF object and call the method on that. For example:

public class PersonalInfo extends JPanel { private void initialize() { JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { GenerateRDF generator = new GenerateRDF(); generator.setRDF(); } }); } } 

Side note: you don't need to write extends Ojbect; everything extends Object by default.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.