保存组件(JTextField,JTextArea)在windowClosing上的文本

时间:2016-02-23 18:21:22

标签: java swing intellij-idea properties windowlistener

我正在使用IntelliJ IDEA创建表单应用程序。到目前为止,我已经获得了用户界面,一切都按照我的意愿运作。除外:我想在退出时保存Textfields的内容并在启动时加载它们。

我对表单的新手并不熟悉Java,但我认为我非常接近解决方案。我不知道我的想法是错的,但首先是一些代码:

...

/**
 * Created by  on 22.02.2016.
 */
public class MainWindow {
    public MainWindow() {
        ...
        some listeners
        ...
    }

    private void logToInfoArea(String text){
        ...
    }
    private void logToResultArea(String text){
        ...
    }

    private void saveConfig(){
        FileOutputStream out;
        try {
            out = new FileOutputStream("appConf");
            appConf = new Properties();
            appConf.setProperty("pathQuery", pathQuery.getText());
            appConf.setProperty("pathMapping", pathMapping.getText());
            appConf.setProperty("pathOntology", pathOntology.getText());
            appConf.setProperty("queryText", queryText.getText());
            appConf.store(out, "[update]");
            out.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void run() throws Exception {
        ...
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Prototyp");
        frame.setContentPane(new MainWindow().mainfield);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                //loading of the config should be here

/*                FileInputStream in;

                System.out.println("UI loaded.");
                defaultConf = new Properties();
                try {
                    in = new FileInputStream("defaultConf");
                    defaultConf.load(in);
                    in.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

                Properties appConf = new Properties(defaultConf);

                try {
                    in = new FileInputStream("appConf");
                    appConf.load(in);
                    in.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }*/
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("UI closing.");
                saveConfig();
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static Properties defaultConf;
    private static Properties appConf;

    private JButton execute;
    private JPanel mainfield;
    private JTextField pathQuery;
    private JTextField pathMapping;
    private JTextField pathOntology;
    private JRadioButton radQueryText;
    private JRadioButton radQueryFile;
    private JTextArea consoleLog;
    private JTextArea result;
    private JTextArea queryText;
}

正如您所看到的,我知道该怎么做,但我的问题是,如何从WindowListener诱导的方法访问创建的框架" windowOpened"和" windowClosing"?我知道我在静态上下文中并且所有组件都是非静态的,但我不知道如何到达触发Listener的实例。因为如果我获得对实例的访问权限,我想我可以把它放进去。像:

saveConfig(instance);

然后在saveConfig方法内部:

appConf.setProperty("pathMapping", instance.pathMapping.getText());

正确?

真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

  

如何从WindowListener

访问创建的框架

来自WindowEvent。您可以使用getWindow()getSource()方法。