简体   繁体   中英

Passing variable set in one Jframe from one to another

I have one JFrame called User where I declare a variable called id and set it to a certain value depending on some conditions.

I need to use this variable in a second JFrame called output.

This is the code I have

class InputScreen extends javax.swing.JFrame {
public int id = 0;

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id = 1;
        System.out.println(id);
    }
    elseif (condition){
             id = 2;
             System.out.println(id);
    }
    elseif (condition){
             id = 3;
             System.out.println(id);
    }
    else{
    System.exit(0);
    }

I used a constructor in the frame Output but it doesn't seem to work.

public class Output extends javax.swing.JFrame {
int rule;

public Output(int Id){
    rule = Id;
    initComponents();
}

public Output() {
    initComponents();
    conn = MySqlConnect.ConnectDB();
}

Updated Code Frame - Input

class InputScreen extends javax.swing.JFrame {
public int id = 0;

public int getID(){
    return input_rule_id;
}

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id = 1;
        System.out.println(id);
    }
    elseif (condition){
             id = 2;
             System.out.println(id);
    }
    elseif (condition){
             id = 3;
             System.out.println(id);
    }

Form - Output

private void formWindowActivated(java.awt.event.WindowEvent evt)       {                                     

    Input InSc = new Input();
    InSc.getId();

 }

All primitive data-types will be passed by the value. Use an object wrapper to pass the value by the reference. For example AtomicInteger

class InputScreen extends javax.swing.JFrame {
    private AtomicInteger id = new AtomicInteger(0);

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id.set(1);
        System.out.println(id);
    }
    else if (condition){
             id.set(2);
             System.out.println(id);
    }
    else if (condition){
             id.set(3);
             System.out.println(id);
    }
      else{
      System.exit(0);
    }
}

public class Output extends javax.swing.JFrame {
AtomicInteger rule;

public Output(AtomicInteger Id){
    rule = Id;
    initComponents();
}

public Output() {
    initComponents();
    conn = MySqlConnect.ConnectDB();
}
}

Parameter passing The most simple way would be passing Output as a parameter to InputScreen . Then you might just call a method Output.setId(id) within your InputScreen.submitButtonActionPerformed() logic. This way however is a bit problematic, when the application grows.

Observer Pattern A better way would be realizing a IdChangedListener as part of an Observer pattern as a proxy to the ActionListener you already have implemented. When your ActionListener fires, you call all IdChangeListeners that have registered as Observers to InputScreen . In Output you provide a method setId(id) to provide access. In the place you instantiate the two JFrames you implement an IdChangeListener , add it to InputScreen and when it fires you call Output.setId() .

Anyway, you see a lot of work (and code) for just one event. Also this approach has its limitations concerning application size and dynamic.

In Memory EventBus The up-to-date approach would be using a in memory EventBus. This eliminates the hard-wiring of the components within your UI and reduces the code size. However, it limits the reuse of components because they will be automatically react to some events. What if you have them more than once? Listening to different Events?

You need to think about where you want to use which approach. I would suggest using an EventBus for event propagation between the concrete components of your application. Use Observer pattern on medium size components while use parameter passing for small size or very static ones.

You can use getters and setter. Define a getter method in your Frame from where you want to pass the id variable. eg

public int getID(){
      return id;
}

In your other frame you can just use this method to return the result. int id = getID();

Another work around for this problem is making the variable globe and static so it can be imported in the other frame and be used.

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