Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Processing user input

Posted on 2003-11-03
6
Medium Priority
?
293 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 340 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

704 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