关闭 JDialog 后保存 setText

发布于 2024-12-15 03:44:10 字数 1448 浏览 6 评论 0原文

我有一个 JDialog 作为“设置窗口”。我选择一个保存文件路径,然后单击名为“保存”的按钮。它存储路径并将其显示在 JTextField 上。我的问题是,当我关闭名为“Settings”的 JDialog 并再次打开它时,JTextField 不显示最新的路径。 我认为它与 JDialog 有关,并且它不存储 setText 变量。如何将新文本存储在 JTextField 中?

这是我的代码片段:

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);

         }
         });

所以我不想将文本字段设置为 myline ,即使在关闭 JDialog 后,也要存储它并在下次打开 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-12-22 03:44:10

如果您打算让 Settings 类存储设置值,请确保您使用的是 Settings 的一个实例,并且在打开对话框时不要创建新的 Settings 对象。

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.

就像说晚安 2024-12-22 03:44:10

像这样在侦听器之外声明 myline 对象

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);

         }
         });

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);

         }
         });
晨敛清荷 2024-12-22 03:44:10

您可以设置主类的 JFileChooser 实例变量,以便它记住最后一个目录位置。您还可以根据选择器中的当前文件初始化文本字段。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文