?
Solved

Make JTextField display positive# in blue and negative# in red?

Posted on 2003-03-03
3
Medium Priority
?
320 Views
Last Modified: 2008-02-01

Hi,

I want to customize JTextField so that it will display positive numbers in blue and negative numbers in red.  I can't simply rely on the focus events because I sometimes change the value inside the PlainDocument directly.  I was thinking about extending JTextField and customizing its behavior upon a document change event.  However, I am not sure about the details, and the code is complex to trace through.

Any ideas?  Thanks.
0
Comment
Question by:anonp
  • 2
3 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 8062601
Set a custom document. in the custom document, check the value and set appropriate color.

Siva
0
 
LVL 92

Accepted Solution

by:
objects earned 225 total points
ID: 8062602
No need to subclass JTextField, just use a DocumentListener that has access to the JTextField.

public class MyListener implements DocumentListener
{
   private JTextField field;

   public MyListener(JTextField tf)
   {
      field = tf;
      field.addDocumentListener(this);
   }

   // add document listener that change
   // text field colour as appropriate
}

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 8062612
Set a custom document. in the custom document, check the value and set appropriate color.

Siva
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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 Month9 days, 19 hours left to enroll

569 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