Basic gui prob

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • N00bie
    New Member
    • Nov 2007
    • 2

    #1

    Basic gui prob

    im a beginner java user so forgive me for my poor code! :)... i have a simple gui with a textarea and txtbox the textarea is used for output and the textbox is used for input... i also have a few other classes which need to take the input... unfortunatly this gui cannot have any buttons.... my problem is this. I have a method called getInput which looks like this!

    private void getInput(KeyEve nt keyEvent){

    int keyCode = keyEvent.getKey Code();
    String keyText = KeyEvent.getKey Text(keyCode);
    if(keyText.equa ls("Enter")){
    userinput = input.getText() .toString();
    input.setText(" ");
    }
    }

    and another method that is called by other classes
    public String getInput(){
    //getInput();
    input.requestFo cusInWindow();
    getInput(keyEve nt);
    return userinput;
    }

    however when getInput is called by other classes it runs and returns an empty userinput which generates an error!....

    So my question is how do i get the user to enter the information into the textbox and press enter before the getInput() finishes

    i would be greatful for any help thanks :D
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by N00bie
    So my question is how do i get the user to enter the information into the textbox and press enter before the getInput() finishes

    i would be greatful for any help thanks :D
    GUIs don't work that way (in Java), i.e. you can't force the user to type anything;
    your code has to be notified if and when the user types something. It's called
    'event based programming'. If you look at the JTextField class API documentation
    you'd see that you can register 'listeners' to it. When something happens in that
    object it wakes up the listeners so they can respond to the event, e.g an enter
    key was pressed.

    You have to drastically turn your code 'inside out' for this.

    kind regards,

    Jos

    Comment

    • N00bie
      New Member
      • Nov 2007
      • 2

      #3
      thanks i will look that up

      Comment

      Working...