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

Posted on 2003-03-03
Medium Priority
Last Modified: 2008-02-01


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.
Question by:anonp
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
  • 2
LVL 26

Expert Comment

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

LVL 92

Accepted Solution

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;

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

LVL 26

Expert Comment

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


Featured Post

Independent Software Vendors: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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
Course of the Month9 days, 20 hours left to enroll

762 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