简体   繁体   中英

How to open new jFrame dialogue and return to main interface?

I tried this

private void botaoConfIOMouseClicked(java.awt.event.MouseEvent evt) {                                         
ConfigurarIO popup = new ConfigurarIO();
popup.setVisible(true);

botaoConfIO.setEnabled(false); //this line to avoid multiple dialogues
setIO=popup.getConfig();  //i need to get this boolean from the dialogue "ConfigurarIO"
//part of the program only to make my logic from the setIO 
if(setIO[0]==false){
    jToggleButton1.setEnabled(false);
    jToggleButton1.setText("Saída");
}
else{
    jToggleButton1.setEnabled(true);
    if(jToggleButton1.isSelected()) jToggleButton1.setText("Pino 1 ON");
    else jToggleButton1.setText("Pino 1 OFF");

} }

And this is the dialogue

public class ConfigurarIO extends javax.swing.JFrame {

boolean[] inOut=new boolean[8];
boolean ok=false;
/** Creates new form ConfigurarIO */
public ConfigurarIO() {
    initComponents();
}

public boolean[] getConfig(){
    return inOut;
}

public boolean getOK(){
    return ok;
}

public void setOK(){
    ok=false;
}
//the logic was emited
private void botaoOKMouseClicked(java.awt.event.MouseEvent evt) {                                     
dispose();
ok=true;
System.out.println(ok);
}    

The problem is that the setIO is not modified by the second interface and, If I set this to make a loop broken only by the "ok" boolean, the window with the setting interface doesn't open. This is a very explored problem but I am new to Netbeans and I couldn't find it on Google. Thanks for the attention

Print screen: http://4.bp.blogspot.com/-B7VWmPelJek/T2ysJV8PJcI/AAAAAAAABqQ/0waWxxEEHkw/s320/temp.png

You haven't said whether a frame is required for some reason, or whether a dialog would do, or whether whatever it is needs to be modal.

The reason the frame doesn't show up if you loop is that you're on the Swing dispatch thread (since you are in a routine that responded to a mouse click), and until it returns, it isn't going to update the screen.

You cannot just call a method on the "frame dialog" to get a value until you know that the dialog has set the value. I would pass my calling class to the dialog as a parameter on the constructor, and then have the dialog code call a method on the calling class when it's all done. If you need to know when this happens, then you'll have to treat it as an event in your calling class; I can't guess what you need for that without knowing more about what you're trying to do overall.

If you need to wait until the dialog is done, and don't need the user to be able to do anything until it is done, then what you want is a "modal" dialog, and I recommend looking at JOptionPane and its various dialog options for what you want to do. THEN the call from your class can be synchronous, ie, you can call the dialog and, when the call completes, the dialog is all done. Then you don't need to pass the calling class to the frame, since it doesn't need to notify you that it's done; you know it's done when your call completes, and you can call a method such as you have already done to get the value that you want.

Incidentally, your subclass-of-JFrame constructor doesn't call super(); I recommend you do that...

rc

// we will make this modal=true, to block access to the parent frame
public class ConfigurarIO extends javax.swing.JDialog {

For more details, see:

  1. How to Make Dialogs
  2. How to Use Modality in Dialogs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM