java JFrame update amends another JFrame

坚强是说给别人听的谎言 提交于 2020-01-30 11:12:05

问题


I have a screen (JFrame) that picks up some of its initial population from a file. However, if the file is for some reason absent or incorrectly populated then many fields are blocked as non-editable and user is forced to click a settings button, which generates another JFrame screen. If the user then updates the file correctly I want the original screen to be re-populated with the new file data, can this be done?

So I have an action listener for the settings button, which calls Java class "settings". "settings" has a button "Done" which will activate the database/file updates, successful updates/ will unblock the original frame

    btnSettings.setText("Settings");
    btnSettings.setFont(font4);
    btnSettings.setBounds(new Rectangle(15, 515, 140, 40));
    btnSettings.setToolTipText("Default Settings");
    btnSettings.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            settingsPerformed();
        }
    }) ;

private void settingsPerformed() {

    JFrame settings = new Settings();
    settings.setVisible(true);
}

and then a new class for settings

public class Settings extends JFrame implements ActionListener {

private.....         
public Settings() {

    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(450, 340));
    this.setTitle("Default Settings");

    this.setBackground(new Color(255, 247, 214));
    this.setResizable(true);
    this.setFont(font1);

    pnlSettingsData.setBounds(new Rectangle(10, 10, 405, 285));
    pnlSettingsData.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
    pnlSettingsData.setName("Settings");
    pnlSettingsData.setLayout(null);  


    btnDone.setText("Done");
    btnDone.setFont(font3);
    btnDone.setBounds(new Rectangle(100, 250, 73, 20));
    btnDone.setToolTipText("Click when ready for updating");
    btnDone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    doneActionPerformed(evt);
                    setVisible(false) ;
            }
    });


    this.getContentPane().add(pnlSettingsData, null);            
    setDefaultCloseOperation(HIDE_ON_CLOSE);
    setLocation(150,200);
    //pack();
    setVisible(true);
}


private void doneActionPerformed(ActionEvent evt) { 

    // include here the data base updates      
}   

public void actionPerformed(ActionEvent e) {
}

}


回答1:


You could ...

  1. Use a modal JDialog to collect the information from the user and when it's closed, you'd be able to reload the file from the current frame.
  2. Allow the first frame to register an action listen to the second frame, which when called (presumably when the user clicks the accept button), you would then reload the file and update the UI


来源:https://stackoverflow.com/questions/15304579/java-jframe-update-amends-another-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!