0

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) {
}

}

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 3
    Check out also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/928711). Consider using modal JDialog instead (with appropriate owner). – Guillaume Polet Mar 08 '13 at 22:48
  • @GuillaumePolet +1 That's what I was about to suggest :) – Eng.Fouad Mar 08 '13 at 22:49
  • In your example, the second frame WON'T block the first, this is the role of JDialog, see [How to use Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for more details – MadProgrammer Mar 08 '13 at 22:50

1 Answers1

2

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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366