Solved

Processing user input

Posted on 2003-11-03
6
287 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now