Help - parsing returned field from getText()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepdave
    New Member
    • Feb 2009
    • 1

    Help - parsing returned field from getText()

    Cobol/Assembler Dinosaur here taking Java. (first post - Hello All)
    I'm trying to tell if the user has input any text. My own feeble effort below:
    How do I do this ? Hope this enough info. thanks
    [code=java]
    string sandname;
    boolean done = false;
    .....
    try
    {
    while (!done)
    {
    sandname = " ";
    sandname = sandwichField.g etText();
    if (sandname != " ")
    done = true;
    else
    throw new NumberFormatExc eption();

    }
    ...
    catch(NumberFor matException e)
    {
    promptLabel.set Text("Enter a sandwich name.");
    hiddenBox.setSt ate(true);
    sandwichField.s etText("");
    sandwichField.r equestFocus();
    }[/code]
    Last edited by Nepomuk; Feb 9 '09, 11:04 PM. Reason: Please use [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi deepdave! Welcome to bytes.com!

    It's great to have you here!

    When you post, please always keep to the Posting Guidelines and if you should post code, please post it in [code] ... [/code] tags. In this case, I added them for you.

    Now, about your question - there are several errors in your code.
    [code=java]try
    {
    while (!done)
    {
    sandname = " "; // You don't have to do that, although it's not wrong.
    sandname = sandwichField.g etText();
    if (sandname != " ") // You can't compare Strings like that. Use sandname.equals (" ") instead for this comparison
    done = true;
    else
    throw new NumberFormatExc eption(); // If you throw an Exception, it will jump out of the loop for good. So either change the value of done or throw an Exception - doing both makes no sense.
    }
    ...
    }
    catch(NumberFor matException e) // This whole thing is outside of the loop, so it will be called only once. Mind you, it shouldn't be.
    {
    promptLabel.set Text("Enter a sandwich name.");
    hiddenBox.setSt ate(true);
    sandwichField.s etText("");
    sandwichField.r equestFocus();
    }[/code]
    Now, even if you correct those errors, it will still loop constantly until something is in the Textbox and then it'll finish - even if the name is just one character long (which it will be, if you can't type at ultraspeed). The best solution would be to add an "OK" button and only check the Textbox if that button is pressed. So, a very simple version without event-handling would look like this in pseudocode:
    Code:
    while(!OKButton.isPressed()) {}
    String sandname = sandwichField.getText();
    That will work, even though it isn't an ideal solution. If you want to write a better version (and are using Swing and not AWT, which I would strongly suggest you do), check this tutorial on writing and using Listeners.

    Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

    Greetings,
    Nepomuk

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by deepdave
      Cobol/Assembler Dinosaur here taking Java. (first post - Hello All)
      I'm trying to tell if the user has input any text. My own feeble effort below:
      How do I do this ? Hope this enough info. thanks
      Swing and AWT don't work like that; they have one active thread running, the EDT (Event Dispatch Thread). Most (if not all) Swing (and AWT) components fire an event when something interesting happens. Those events are received by event 'listeners'. A listener first has to be registered at the component. (it can be removed again later if you want).

      An event listener is nothing but an implementation of a certain interface; for an example have a look at the 'ActionListener ' interface; it has a method 'actionPerforme d' that is called when an ActionEvent is fired. The ActionEvent contains details about the particular event.

      There are a lot more event types but lets stick to the ActionEvent for now. A JTextField (your example) fires an ActionEvent when the user presses the 'enter' key. This is exactly what you want: you don't want to keep on waiting and checking if and when the user types something; you're just interested when the user says 'I'm done typing' which is the ActionEvent that is fired when the user types the enter key. You can use other threads that do useful things, there is no need to pay attention to the JTextField where the user is typing ...

      Any event object (so also an ActionEvent object) always contains the source component that fired the event. That can come in handy when you register your event listener at different components. I normally don't do that because it complicates the logic in the event listener itself (it has to figure out which object fired the event etc.)

      Below you'll find a fragment of a small class that implements the ActionListener interface. If can be registered at your JTextField. The example class does the registering itself when an object is constructed. When the user presses the 'enter' key it fetches the text in the JTextField and prints it. Here it is:

      Code:
      public class TextPrinter implements ActionListener {
         private JTextField field; // registered at this field
         
         public TextPrinter(JTextField field) {
            field.addActionListener(this); // register
            this.field= field; // remember the field 
         }
      
         // called when an event is fired:
         public void actionPerformed(ActionEvent ae) {
      
            String text= field.getText();
            
            System.out.println("text in field: "+text);
         }
      }
      Play with it a bit to get a bit of feeling with this 'event driven' programming.

      kind regards,

      Jos

      Comment

      Working...