?
Solved

Java Text listener

Posted on 2009-05-01
14
Medium Priority
?
431 Views
Last Modified: 2012-06-27
I need some assistance finding out how to take information received from a textfield adding the value to a total and displaying it without a button
0
Comment
Question by:nbham29
  • 7
  • 4
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24281141
What would activate getting the total?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24281152
If moving away from the field, then see

http://www.exampledepot.com/egs/java.awt.event/FocusEvents.html
0
 

Author Comment

by:nbham29
ID: 24281230
I was thinking some kind of listener might activate but I am not sure. All I know is that I wanted it to update a JLabel like a checkbox or a radio button does
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:nbham29
ID: 24281261
I have found some code that use a textListener and TextEvent  recorded  the event everytime something was entered but I don't exactly know how to get that to a variable and add it to total and the label.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24281279
How would it know when to stop listening and start acting?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 180 total points
ID: 24281333
Try the code i posted. You of course would implement focusLost
0
 
LVL 92

Accepted Solution

by:
objects earned 195 total points
ID: 24284513
Use a DocumentListener, something like the following.
It'll need some tweaking, but should give you the general idea.
Yell if you need any help

DocumentListener dl = new DocumentListener() {
   public void changedUpdate(DocumentEvent e) {
      updateLabel(e.getDocument());
   }

   public void insertUpdate(DocumentEvent e) {
      updateLabel(e.getDocument());
   }

   public void removeUpdate(DocumentEvent e) {
      updateLabel(e.getDocument());
   }

   private void updateLabel(Document doc) {
       try {
          int value = Integer.parseInt(doc.getText());
          label.setText(Integer.toString(total + value));
       } catch (Exception ex) {
       }
   }
};

textField.getDocument().adDocumentListener(dl);

0
 

Author Comment

by:nbham29
ID: 24286426
will this work with combo boxes  and radio boxes in the project as well.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24286788
A FocusListener will, yes
0
 
LVL 92

Expert Comment

by:objects
ID: 24288143
> will this work with combo boxes  and radio boxes in the project as well.

no use an ActionListener for those

0
 

Author Comment

by:nbham29
ID: 24291230
label.setText(Integer.toString(total + value));
will this portion work with a double
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24291338
You need

label.setText(Double.toString(total + value)); 

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24291510
> label.setText(Integer.toString(total + value));

depends, do you want it displayed as a double or as an integer
if you want to display it as a double then see here for how to convert it to a string

http://helpdesk.objects.com.au/java/how-to-format-a-double-as-a-string

let me know if you need any help

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24306675
nbham29, could  you post for the benefit of future visitors, how you used a DocumentListener?
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
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 …
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…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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