Search code examples
javaswingprocessbuilder

Calling a function that already works for a radiobutton so it prints the details


Having a tiny issue where not sure how to call a specific function to print its details. I created a Radio button that checks the Total physical Memory on a PC, also have one for GPU and both work just fine.

Now I am lost on how to call that same function so it prints in the bigger window when I do a system scan of specific system properties.

if (isWindows()) {

        jTextArea1.setText(header + "User Name : " + name
                + "\nOperating System :" + jComboBox1.getSelectedItem()
                + "\nSelected Gamer Ability : " + this.jComboBox4.getSelectedItem()
                + "\nSelected Age Group :" + this.jComboBox5.getSelectedItem()
                + "\nSystem Version : " + System.getProperty("os.version")
                + "\nSystem Architecture : " + System.getProperty("os.arch")
  PROBLEM PART  + "\nSystem Total Ram : " + this.jRadioButton2......
                + "\nScan ID : " + n + "\n \n")

 }



private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt)       {                                              
    String filepath = "..\\Checker\\src\\batchchecker\\memory.bat";
    try 
    {
        Process p = Runtime.getRuntime().exec(filepath); // filepath
        p.waitFor();
        InputStream in = p.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = -1;
        while ((c = in.read()) != -1) 
        {
            baos.write(c);
        }
        String response = new String(baos.toByteArray());
        jRadioButton2.setText(evt.getActionCommand());
        JOptionPane.showMessageDialog(null, "" + evt.getActionCommand()
            + response);

        }
        catch (Exception e) 
        {
            e.printStackTrace(System.err);
        }
    }
}

As you can see from above code, my radio does what it needs to and tested. I am just not sure how to call the same result into the bigger picture where it actual prints all the details along with the rest. The line of code is + "\nSystem Total Ram : " + this.jRadioButton2......


Solution

  • Seems like you just need to move your implementation to a separate method that can be called from both your actionPerformed() method and your other call. For example:

    public String findMemoryDetails() {
        // ... put code here
    }
    

    Then call it here:

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String memDetails = findMemoryDetails();
        JOptionPane.showMessageDialog(null, "" + evt.getActionCommand() + memDetails);
    }
    

    And here:

    + "\nSystem Total Ram : " + findMemoryDetails()