SkepticalTiger
SkepticalTiger

Reputation: 39

How to get variables from one GUI class passed into another?

I'm building an application which takes in inputs as strings and passes them to another class where my methods are stored to return as int/numbers. In my main GUI window I am able to display this to a textArea. I create another class that opens a new window when a button is clicked. I can get the method to display it, but am unable to have anything the user entered passed unless the new window is open first then I press another button for the result.

Ideally, what I want to happen is that when I press my 'Submit Pop' button, the new window opens and displays the result.

GUI class

//The declaration of my textArea results are displayed in the main window
                JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(22, 354, 354, 230);
        contentPane.add(scrollPane);
        
        JTextArea psuArea = new JTextArea();
        scrollPane.setViewportView(psuArea);
        PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
        System.setOut(printStream);
        System.setErr(printStream);
        
        
        JButton popUp = new JButton("Submit Pop");
        popUp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                pw ok = new pw();
                ok.newScreen(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
            }
        });

                //The button that calls my method and passes the variables entered to it.
        JButton btnAdvanced = new JButton("Advanced Results");
        btnAdvanced.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                pcp.displayC(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
            }
        });

Result window class

package pcbuilder;
import java.awt.EventQueue;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.JLabel;

public class pw {

    private JFrame framePU;
    pccalc pcp = new pccalc("");

    /**
     * Launch the application.
     */
    public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    pw window = new pw();
                    window.framePU.setVisible(true);
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public pw() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        framePU = new JFrame();
        framePU.setBounds(100, 100, 450, 348);
        framePU.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        framePU.getContentPane().setLayout(null);
        
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(182, 6, 61, 16);
        framePU.getContentPane().add(lblNewLabel);
        
        JTextArea psuArea = new JTextArea();
        psuArea.setBounds(6, 26, 438, 246);
        framePU.getContentPane().add(psuArea);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(22, 354, 354, 230);
        
        PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
        System.setOut(printStream);
        System.setErr(printStream);
    }
}

Method class

public void displayC(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
        
//When called in GUI class total wattage is correct. I may need to modify something in my Results or GUI class to have total wattage display correctly in the popup. 
        curWatt = cpu + cpuO + mobo + gpu + fastStore + slowStore + store + exStore + fan + sound;

        
        System.out.printf("%nBuild Name: %s", buildName);
        System.out.printf("%nCPU - %s %s: %d", cb, cl, cpu);
        System.out.printf("%nCPU OC Needed: %d", cpuO);
        System.out.printf("%nMotherboard: %d", mobo);
        System.out.printf("%nGPU - %s %s: %d", gb, gl, gpu);
        System.out.printf("%nSSD/NVME - %s: %d", fs, fastStore);
        System.out.printf("%nHDD - %s: %d", ss, slowStore);
        System.out.printf("%nUnused Sata/NVME - %s: %d", sa, store);
        System.out.printf("%nCD/DVD/BlueRay - %s: %d", fq, exStore);
        System.out.printf("%nFans - %s: %d", cf, fan);
        System.out.printf("%nOverhead for Sound: %d", sound);
        System.out.printf("%nTotal Estimated Wattage: %d%n", curWatt);
        
    }

Any ideas?

Edit: Thanks to the answer, I was able to get the code working. Here is the messy code I used to get it working.

public class pw {

test nt = new test();
private JFrame framePU;
JLabel lblNewLabel = new JLabel("New label");
JScrollPane scrollPane_1 = new JScrollPane();
JTextArea psuArea = new JTextArea();        
PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
JScrollPane scrollPane = new JScrollPane();
pccalc pcp = new pccalc("");



/**
 * Launch the application.
 */


public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {


            String in = n;
            String cpuBrand = cb;
            String cpuLine = cl;
            String gpuBrand = gb;
            String gpuLine = cl;
            String flash = fs;
            String platter = ss;
            String sata = sa;
            String cd = fq;
            String fan = cf;
            
            
            try {

                lblNewLabel.setText("A different label");

                pcp.buildName(in);
                if (pcp.cpuBrand(cb) == true) {
                    
                    pcp.amdLine(cl);
                    
                } else {
                    
                    pcp.intelLine(cl);
                    
                } 
                
                if (pcp.gpuBrand(gb) == true) {
                    
                    pcp.graphAmd(gl);
                    
                } else {
                    
                    pcp.graphNvidia(gl);
                    
                } 
                
                pcp.driveS(fs);
                pcp.driveH(ss);
                pcp.driveF(fq);
                pcp.driveA(sa);
                
                
                pcp.displayC(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
                framePU.setVisible(true);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Still a bit messy, but it's working the way I want and now I can move onto figuring out how to add a save feature.

Upvotes: 0

Views: 610

Answers (1)

Kevin Anderson
Kevin Anderson

Reputation: 4592

You passed all kinds of data into the newScreen method of the pw class. But you didn't do anything with any of it. Worse yet, with your current design, there's really nothing useful you can do with it, because the declarations of all your GUI components are hidden inside the initialize method, where newScreen cannot get at them.

First thing you need to do is pull the declarations of your GUI components out of initialize and into member variables at the top level of the pw class, where other class members (such as the newScreen(...) method) can access them:

public class pw {
    private JFrame framePU;
    private JLabel lblNewLabel = new JLabel("New label");
    private JTextArea psuArea = new JTextArea();
    private JScrollPane scrollPane = new JScrollPane();
    pccalc pcp = new pccalc("");
    // ....
    private void initialize() {
        framePU = new JFrame();
        framePU.setBounds(100, 100, 450, 348);
        framePU.setDefaultCloseOperation(
            JFrame.DISPOSE_ON_CLOSE);
        framePU.getContentPane().setLayout(null);
        
        lblNewLabel.setBounds(182, 6, 61, 16);
        framePU.getContentPane().add(lblNewLabel);
        
        psuArea.setBounds(6, 26, 438, 246);
        framePU.getContentPane().add(psuArea);
        
        scrollPane.setBounds(22, 354, 354, 230);
        
        PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
        System.setOut(printStream);
        System.setErr(printStream);
    }
}

Now newScreen can actually do something useful with the data that was passed to it---namely, get it displayed by the components of the pw window:

public class pw {
    private JFrame framePU;
    private JLabel lblNewLabel = new JLabel("New label");
    private JTextArea psuArea = new JTextArea();
    private JScrollPane scrollPane = new JScrollPane();

    //...

    public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
        final String txt =
           String.join(" ", "n=",n,"cb=",cb,"cl=",cl/* etc. */);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
//   We're already *in* a pw, so no need for a new one...
//                    pw window = new pw();
//                    window.framePU.setVisible(true);
                    lblNewLabel.setText("A different label");
                    psuArea.setText(txt);
                    framePU.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Upvotes: 1

Related Questions