Solved

Processing user input

Posted on 2003-11-03
6
291 Views
Last Modified: 2012-05-04
I have an applet with a jtextfield.  Each time the user enters a value the applet needs to make some calculations and display data back to the user.

I currently use the event keyevent where i call the even processInput (as shown below).  The problem is that it always seems to be 1 number behind.  I think i need to place it on the queue to execute but i cant figure out how.  Any help would be greatly appreciated.  thanks.



void processInput() {
    String current;
    long shares;
    NumberFormat cur = NumberFormat.getCurrencyInstance(Locale.US);

    current = jtf_shares.getText();

    try {
      jlbl_message.setText(null); //Reset the message label

      shares = Long.parseLong(current); //Parse the user input as a long value

      jb_execute.setEnabled(true); //If we get here the parse succeded so we can enable
      // the command button

      //Update the labels
      jlbl_trade_cost.setText(cur.format(shares * stock.getSharePrice()));
      jlbl_estd_income.setText(cur.format(shares * stock.getDividends()));
    }
    catch (Exception ex) { //Catch exceptions thrown for invalid number
      jlbl_message.setText(ex.toString() + ": For input string '" + current +
                           "'");
      jb_execute.setEnabled(false);
    }
  }
0
Comment
Question by:smgillis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9676323
When exactly do you want to do the calculation?
Did you want to re-calcs everytime a key is pressed? If so you should be calling processInput() on keyReleased(). Or instead of listening for KeyEvents then listen for changes to the textfields Document with a DocumentListener.
If you want it recalced after the user hits enter then use an ActionListener.
0
 
LVL 4

Accepted Solution

by:
vk33 earned 85 total points
ID: 9676354
Exactly! DocumentEvent is preferred when listening to documents changes:

JTextField field = new JTextField();
field.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent e) {}

      public void insertUpdate(DocumentEvent e) {
            try {
                  Document doc = e.getDocument();
                  String current = doc.getText(0,doc.getLength());
                        // do processing here....
            }
            catch (Exception ex) {}
      }

      public void removeUpdate(DocumentEvent e) {}
});

Regards!
0
 
LVL 92

Expert Comment

by:objects
ID: 9676414
try this:

field.getDocument().addDocumentListener(new DocumentListener() {
     public void changedUpdate(DocumentEvent e) {
         processInput();
     }

     public void insertUpdate(DocumentEvent e) {
         processInput();
     }

     public void removeUpdate(DocumentEvent e)
     {
         processInput();
     }
});
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:vk33
ID: 9676476
thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 9676494
Thats what I said?? And I think you'll find that code doesn't work completely.
0
 
LVL 4

Expert Comment

by:vk33
ID: 9676538
objects, I'm sorry, maybe I'm just the first, sometimes it's like a game...

Yes, I didn't post removeUpdate and changeUpdate implementations and omited exception handler just to make the code sample shorter. That's it...

Best regards!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question