Solved

Processing user input

Posted on 2003-11-03
6
286 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

747 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