phema
phema

Reputation: 69

How can I switch from JFrame to java class?

I've been trying to figure out how to go from a JFrame to my java class.

This was my code so far: (It only opens the main..which is the JFrame)

Process process = Runtime.getRuntime().exec("cmd.exe /c start java -jar \C:\Users\Admin\Documents\NetBeansProjects\Program\dist\Program.jar");
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

The main used to be the class, making it easy for me to use setvislble() for the frame, but now I need it backwards and the main is now the JFrame. I need to know how to open the class file. (Also if anyone knows how to switch besides using CMD that would be great!)

I've tried used this plus a combination of other variations but it won't work.

java -cp "C:\Users\Admin\Documents\NetBeansProjects\Program\dist\Program.jar class"

Upvotes: 1

Views: 169

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

JFrame button, if my understanding is correct, does not handle infinite loops well.

The idea is not to block the EDT, but there are certainly correct ways to repeat an action (until told otherwise) in Swing, as well as ways to do long running tasks. Here is some advice I commonly give:

Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See Concurrency in Swing for details and the fix.

But back to the matter at hand..

..getting the user to input information in the GUI, then sending that information into a text file. The class reads the text file, gets the information and does its things..

OK that is 10 different kinds of wrong.

Let's say the GUI is called GUI.class, the other class is called Worker.class.

Worker.class

This class might have a no-args constructor as well as a constructor that accepts an InputStream (for the text file, if it is finally needed).

The Worker class might have a main(String[]) that creates an instance of a Worker object. It might use either:

  1. An InputStream for the constructor of the Worker, from which it would configure the fields needed for the work to be done.
  2. A no-args constructor for the Worker instance, then use setter methods for configuring it. (Maybe prompting the user through the command line to supply the information.)

Once the main has done that, call execute() on the Worker instance and call getter methods for the results, then write the results to System.out.

GUI.class

The GUI can also use a Worker instance. It would probably use the no-args variant then bind the setter methods to the input controls in the GUI. Once the user clicks the Execute button, execute() the process, then query the getter methods to display the results to the user (still in the GUI).

Upvotes: 3

Related Questions