1

I am looking for the paint method implementation for a Java TextArea component (java.awt.TextArea).

I have read through the source code for the class as well as its super class (java.awt.TextComponent), but have not found a public void paint(Graphics g) method implementation, which I think means the class would be using the default Component#paint(Graphics) implementation, which doesn't make sense. Am I missing something here? How is a TextArea component painted?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51
  • Since you've got the source code, why not have a look at the `Component#paint(Graphics)` method to see exactly how a `JTextArea` can be painted without defining its own `paint` method? – Nate W. Aug 24 '11 at 21:34
  • @Shakedown I did look at the [java.awt.Component](http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Core/AWT/java/awt/Component.java.htm) source and the `paint` method is empty. – Jeff Lockhart Aug 24 '11 at 21:42
  • Whoops, I'm sorry, I meant `JComponent`, not `Component`. – Nate W. Aug 24 '11 at 23:53

2 Answers2

2

TextArea is an AWT component, not a Swing component. It's thus what's called a heavyweight component, which means it's in fact implemented by a native component of the underlying platform/OS (i.e. a Windows/Gnome/Motif component, depending on the OS), called the peer of the component. The painting is thus done by the platform's native widget, and not by the component.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Ahh, so that's what the peer it references is. That makes sense. `JTextArea` and `JTextComponent` also do not contain a `paint` method. Are they also painted by the platform as well? – Jeff Lockhart Aug 24 '11 at 22:09
  • No. These are lightweight components. Read http://java.sun.com/products/jfc/tsc/articles/painting/index.html for explanations. – JB Nizet Aug 25 '11 at 07:07
  • IUUC, painting is delegated, e.g. [`javax.swing.plaf.TextUI`](http://download.oracle.com/javase/6/docs/api/javax/swing/plaf/TextUI.html) for `JTextComponent`. – trashgod Aug 25 '11 at 11:34
1

AWT Components is dinosauruses from last milenium, and only by back-compactible are there and still exists, please/better would be to change that to the Todays JComponents, all starts with "J" here is list of JComponents with tutorials, but for Swing's JComponents is there paintComponent(Graphics g) instead of paint(Graphics g)

method paint is still there but for deepest painting in XxxXxxUI, for example MetalButtonUI, but not for painting Image/Lines/Text ... and just try to avoid a similar examples from year 2000 and another very old examples, that's really wrong implemntation for Custom Painting in Java6's Swing,

here is your required tutorial and Java6 API

on this Forum are plenty threads about Painting Something in JComponents

EDIT: if you want to paint something then look for JLabel (is transparent by defalut), that best JComponent for 2D Graphics, examples for that here, and with paintComponent() method only

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I understand `AWT Components` are outdated by the newer `Swing Components`. I'd be just as content being able to see `JTextArea`'s `paint` method (which is just as elusive). I'm looking to write my own `TextArea` class for use on the Amazon Kindle's Java 1.4.2 based KDK, as the KDK's implementation is somewhat broken. I want to use the Java API's implementation for a reference. – Jeff Lockhart Aug 24 '11 at 22:02