views:

917

answers:

7

My professor assigned a project where a simulation is ran through a GUI. To edit it, we need to create a "New" menu item. We haven't learned how to get data from a GUI, and our book does not cover it at all.

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

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

A: 

Can you post the stacktrace you've got? ( The error message when it crashes ? ) Probably will be easier to help you from there

OscarRyz
A: 

Is the System.out in a terminal (non Java) window? If so, I think this would be much harder than you'd think.

I'd be tempted to redirect the System.in / System.out to a JTextPane on the GUI (That way, it would be much easier to change the focus etc. I think you need to try and explain what you're doing a little better in your question and perhaps post a stack trace when your program crashes.

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

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

I don't get an error. I'm running BlueJ (our book is written around it...BLEH) but to be more specfic, the JFrames the program has freezes. It doesn't change focus to the CMD prompt, but when I click to that window, it doesnt allow for the first input. It reads up to: print("System.out.print("Enter the depth: "); int depth = scan.nextInt();");But that's it...

Do you realize it is very hard to know what are you talking about, because we are not watching your screen. I would recommend you to go step by step. Until the point where you have the failure. Go an step back and try to know what had happened. OR you could post more data. Code, screenshots,
OscarRyz
A ( programmatic ) description of what are you attempting, and HOW are you trying to do such a thing. Etc. For instance, you don't say how are you changing the focus. Probably a video will do. Otherwise it will be only guessing. But cheer up!, You're not alone. :)
OscarRyz
A: 

The System.out window is in a Java window. Each menu button pressed prints a line to a Java window (not the terminal, I'm sorry...)

Here is the actionPerformed class (part of it):

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

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

and runNew() is this:

private void runNew()
{
        System.out.print("Enter the depth: ");
        int depth = scan.nextInt();
        System.out.println();
        System.out.print("Enter the width: ");
        int width = scan.nextInt();
        System.out.println();
        System.out.print("Enter the fox's creation probability (0.x): ");
        double fox = scan.nextDouble();
        System.out.println();
        System.out.print("Enter the rabbit's creation probability (0.x): ");
        double rabbit = scan.nextDouble();
        System.out.println();
        new FoxGui(depth, width, fox, rabbit);
    }

I can print to the Java window (which I call the terminal, I suppose it isnt?) but I can't get the keyboard to focus on it to input data. Also, I dunno how to print out a stackflow error from BlueJ...

Edit: I suppose it is the terminal window. The label of the window is BlueJ: Terminal Window.

When I go back to it to try to get some input, The JFrames lock up and I can't do anything to them. Maybe that's a better way to explain it.

Objects First with Java? I used that book in my first year of Uni. Good times!
Richie_W
You are using a terminal to get input / output. I don't think there is an easy way to do what you're wanting to do (Basically mixing GUI and CMDLine interfaces).
Richie_W
I didn't think so, but Objects First with Java doesn't cover JTextFields, or TextPanes...or anything useful.
Haha, you're right it doesn't! Good book though IMO. Anyway, instead of getting your input from the terminal I think you need to create a GUI interface :(
Richie_W
The problem with that is we didn't learn how to. I have no useful examples of how to do it!
Google? COme on man! There are literally hundreds of examples for you to look at.
Richie_W
A: 

It doesn't look like you're keeping the reference to your newly created GUI. As far as I remember, Java will garbage collect the FoxGui object (as well as any other object) if there are no references to that object. Try creating a global variable to store the 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(....)
}
Dalin Seivewright
What's a FoxGui?
Michael Myers
To be honest, I have absolutely no idea... I could have sworn it was in the question before though. After looking at other answers I think it may have been in my mind after seeing 'fox' in the answer by Eric. The point of references/garbage collection thankfully doesn't lose its meaning though!:)
Dalin Seivewright
Ok, I see it now ("new FoxGui(depth, width, fox, rabbit);"); I guess that's one hazard of people adding answers instead of editing the question. :)
Michael Myers
A: 

I was hoping it would work, but it doesn't.

Now, when I get rid of the System.in lines it works perfectly. However, that defeats the purpose of the function. But at least I know where the error lies...you really can't start a GUI and then go back to the terminal to read data can you?

Yes you can. Post your code (all of it).
Richie_W
A: 

You know what? I found a real simple solution, JOptionPane. I just needed to find a good example. It'll work fine for what I want. Thanks for all the help. I'll checkmark everyone who helped.

Yup yup, good luck with the assignment!
Richie_W
Good!... :) I'm glad!
OscarRyz