I develop a notebook/text editor and i want to make a save button. My problem is that the most solutions develop a “Save as” button but I want just a save button. To be more clear, I want to write things to my editor then pressing save and when I open my editor the text will appear. If someone knows how to make this thing then I would really appreciate!
Here is my code:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.*; public class Gui extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel panel = new JPanel(); private JPanel mainPanel= new JPanel(); private JPanel otherPanel = new JPanel(); private TextArea text = new TextArea("", 44, 180, TextArea.SCROLLBARS_VERTICAL_ONLY); private JButton saveButton; GridLayout gridLayout = new GridLayout(); public Gui() { this.setTitle("Notebook"); setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(panel); panel.add(mainPanel, BorderLayout.CENTER); panel.add(otherPanel, BorderLayout.NORTH); mainPanel.setLayout(new GridLayout(1, 1)); otherPanel.setLayout(new GridLayout(1, 2)); saveButton = new JButton("Save"); saveButton.addActionListener(this); mainPanel.add(text); otherPanel.add(saveButton); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == saveButton) { try { // this create a NEW file named note.txt File file = new File("note.txt"); FileWriter out = new FileWriter(file); out.write(text.getText()); out.close(); } catch (FileNotFoundException e2) { e2.printStackTrace(); } catch (IOException e3) { e3.printStackTrace(); } } } }