Handsken
Handsken

Reputation: 674

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.

Upvotes: 1

Views: 820

Answers (3)

Jim
Jim

Reputation: 22646

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.

Upvotes: 1

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9307

You can make the JFileChooser instance variable of your main class so that it remembers the last directory location. You can also initialize your text field based on current file in the chooser.

Upvotes: 0

Pratik
Pratik

Reputation: 30855

declare the myline object outside of the listener like this way

private string myline = "":
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();
                myline = file.getAbsolutePath();

              }});

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

         }
         });

Upvotes: 1

Related Questions