1

I am wishing to draw a number onto a JTextField by overwriting the paint method. So that when the user edits the text field the number doesn't disappear. However, at the moment, the number isn't appearing at all, I have tried:

public void paintComponent(Graphics g) {  
     super.paintComponent(g);
     if(number != 0){
            g.setColor(Color.RED);
            g.drawString(String.valueOf(number),0,0);
     }  
}  

Any ideas, is this even possible?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ben Kneebone
  • 77
  • 2
  • 11
  • `if(number != 0){` Do something else (e.g. different color) if `number==0`! – Andrew Thompson Apr 23 '12 at 14:10
  • If your question has been answered, or if it is no longer valid, please 'tick' to choose the most appropriate answer so everyone knows that the problem has been resolved. Thanks. – wattostudios May 14 '12 at 13:45

4 Answers4

3

Try to play with Y position in the g.drawString(String.valueOf(number),0,0); call. E.g. use getHeight()/2

StanislavL
  • 56,971
  • 9
  • 68
  • 98
2

..when the user edits the text field the number doesn't disappear.

As pointed out by @mKorbel, there is no need to override a JTextField in order to get red numbers, simply configure it using the public methods. OTOH..

g.drawString(String.valueOf(number),0,0);

If this is really all about numbers, perhaps the best approach is to use a JSpinner with a SpinnerNumberModel, and set a custom SpinnerUI.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

maybe there no reason override paintComponent() for JTextField, instead of use

  • JTextField.setBackground()

  • JTextField.setForeground()

  • JTextField.setFont()

  • JTextField.setHorizontalAlignment(javax.swing.SwingConstants.LEFT)

  • some hacks are possible by put there Html colored or special formatted text

EDIT

maybe this question is about

  • filtering KeyEvents in the Document / DocumentListener

or

  • JFormattedTextField with Number Formatter
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    I was about to up-vote that before I saw *"put there Html formatted text"* - would not look very good in a plain text component like `JTextField`. ;) Any chance of an edit, clarifying? – Andrew Thompson Apr 23 '12 at 13:45
  • @Andrew Thompson done, but didn't claryfied your comment somehow, an answer could be for you in cases that you want to hightlighting possitions from --> to from Caret, InputMask, Document – mKorbel Apr 23 '12 at 13:54
  • I was simply referring to components like `JEditorPane` which support (HTML) formatted text.. I actually think the use-case needs a configured text field. +1, for (now) the first 4 points. – Andrew Thompson Apr 23 '12 at 13:55
  • @Andrew Thompson initially I want to answering [I retract everything](http://stackoverflow.com/a/10363871/714968), but I needed to check if sleeping before, hehehe [I have own FanClub too](http://meta.stackexchange.com/questions/129983/my-account-has-repeated-serial-upvotes-whats-going-on), in reversed form :-) – mKorbel Apr 28 '12 at 22:30
  • FanClub (dismissive) Peh! [I have my own hate club](http://stackoverflow.com/users/418556/andrew-thompson?tab=reputation). It will be interesting to see if the script automatically detects & removes those 19 down-votes. ;) – Andrew Thompson Apr 28 '12 at 22:47
  • @Andrew Thompson you must ***** some real developer, there are a bunch of coders with real errors in her / his non_inflatable ego :-) – mKorbel Apr 28 '12 at 22:56
1

Why don't you just add a small JLabel to the front of the JTextField? The JLabel could contain the number, and because it isn't editable it will always be there no matter what the user changes in the JTextField. You could also format the JLabel to make it red by calling setForeground(Color.RED);. This might be a much simpler solution?

For example, instead of doing this...

JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);

You might do something like this...

JPanel panel = new JPanel(new BorderLayout());

JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);

JLabel label = new JLabel("1.");
label.setForeground(Color.RED);
panel.add(label,BorderLayout.WEST);

Which adds a red JLabel to the left of the JTextField, and because you're using BorderLayout for the JPanel then it automatically makes the JLabel the smallest it can possibly be.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Sorry, to clarify, I mean that instead of adding a JTextField to your JPanel, you would actually add a JLabel and a JTextField. Please refer to my edited answer. If you can't get the JLabel to appear, you must be overwriting it with something else. For example, calling JPanel.add(label,BorderLayout.CENTER) followed by JPanel.add(textfield,BorderLayout.CENTER) will overwrite the label and only show the textfield. You'd need to ensure they aren't overwriting each other by setting thel to different locations, as per my edited code. Hope this helps. – wattostudios Apr 23 '12 at 13:57