Switching from GUI to command line in Java?

My professor assigned a project in which the simulation is run through a GUI. To change it, we need to create a New menu item. We haven't learned how to get data from a GUI, and our book doesn't cover it at all.

What I'm trying to do is when the New command hits, focus switches back to the CMD prompt where System.out is. starts working again and prompts the user for input.

However, when I try to implement this, my program crashes. What can I do to fix this problem?

0


source to share


4 answers


It doesn't look like you are saving a link to your newly created GUI. As far as I remember, Java will garbage collect the FoxGui object (like any other object) if there are no references to that object. Try creating a global variable to keep a reference to your newly created FoxGui object. Something like...



FoxGui MyGUIRef;
public void actionPerformed(ActionEvent event) 
{ 
    System.out.println("Item: " + event.getActionCommand());

    // File Menu Controls
   if (event.getActionCommand().equals("New"))
   {
       MyGUIRef = runNew();
   }
}

//Now returns a reference to FoxGui
private FoxGui runNew()
{
     return new FoxGui(....)
}

      

0


source


Is System.out in a terminal window (not Java)? If so, I think it will be much more difficult than you think.

I would be tempted to redirect System.in/System.out to JTextPane in the GUI (this way it would be much easier to change focus, etc. I think you need to try to explain, you are doing a little better in your question and maybe send a stack trace when your program crashes.



Anyway, to do something when the "new" menu item is clicked, you would need to do:

menuItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       // Code here to be performed when the item is clicked
   }
});

      

0


source


You know? I found a real simple solution, JOptionPane. I just needed to find a good example. It will be good for what I want. Thanks for the help. I will mark everyone who helped.

0


source


I know this is a very late answer, but anywhere ...

There is only one way to do exactly what you want.

  • First you need to remember to launch the project from CMD with java -jar jarname.jar
  • Execute click action and execute system.in command

Information:

This is the only solution because the GUI never focuses on CMD, but if the GUI is launched from CMD you can easily use System.in.

Regards, Greg

0


source







All Articles