Search code examples
javaswingjtextfieldjdialogsaving-data

saving setText after closing JDialog


I'm having a JDialog that works as a "Settings Window". I Choose a Save-File-Path and I click a button named Save. It Stores the Path and displays it on a JTextField. My problem is when i close the JDialog called "Settings" and open it again the JTextField doesn't display the newest Path. I think it has something to do with the JDialog and that it doesn't store the setText variable. How can I store the new text in the JTextField?

This is a fragment of my code:

public class Settings extends JDialog {

textField = new JTextField("C\\:");
        textField.setBounds(10, 36, 254, 28);
        panel.add(textField);
        textField.setEditable(false);
        textField.setColumns(10);

button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              choose= new JFileChooser();
              choose.setCurrentDirectory(new java.io.File("."));
              choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

              int rVal = choose.showSaveDialog(Settings.this);
              if (rVal == JFileChooser.APPROVE_OPTION) {
                filename.setText(choose.getSelectedFile().getName());
                dir.setText(choose.getCurrentDirectory().toString());
                File file = choose.getSelectedFile();
               string myline = file.getAbsolutePath();

              }});

sbutton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             textField.setText(myline);

         }
         });

So I wan't to set textfield to myline and even after closing the JDialog, store it and display it next time you open JDialog.


Solution

  • If you intend for the Settings class to store the value of the settings then make sure you are using one instance of Settings and not creating a new Settings object when opening the dialog.