Two classes in my program are causing me trouble. The first opens up a JFrame. The second updates data on a .properties file. Within the JFrame there is a panel with JTextAreas and a button "Save Changes." When that button is pressed I call a method from the second class but to do that I have to
firstClass x = new firstClass();
So when the button is pressed, the file is updated but a new JFrame opens up. I'm pretty sure creating the instance x
is what's causing this, but I don't know any other way to accomplish this without doing that.
Class 1:
public class firstClass{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new firstClass();
}
});
}
JFrame go = new JFrame("firstClass");
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
JButton savechanges = new JButton("Save");
public firstClass() {
panel.add(textArea);
panel.add(savechanges);
savechanges.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
secondClass f = new secondClass();
try {
f.UpdateData();
} catch (IOException e) {
e.printStackTrace();
}
}
});
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(750, 750);
go.setVisible(true);
}
Class 2:
public class secondClass {
public static void main(String[] args) {
//creates properties file
}
public void UpdateData() throws IOException{
firstClass x = new firstClass(); // <-------------------------------
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
props.setProperty("prop1", x.textArea.getText().toString());
props.store(out, null);
out.close();
}