Solved

Processing user input

Posted on 2003-11-03
6
292 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

628 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