Sooo...
I just started designing the GUI for an application that I'm writing..
And, well, I can't get either the awt.TextArea OR the swing.JTextArea to work properly.
Here are snapshots of both, along with the problems I am experiencing.

If anyone can resolve these problems, please let me know..
I wanted to use JTextArea but it produces errors and functions terribly.
Both examples are enclosed in a JScrollPane, the JTextArea makes use of the Scrollbars, the TextArea does not (though it works well otherwise, besides displaying its OWN scrollbars).

Pay no attention to the sloppiness, the JLabels, etc, as I've just begun putting the GUI together. ;)

Swing.JTextArea -- Produces errors during runtime, JScrollBar works, but widgit functions terribly
[IMG]http://i12.photobucket.com/albums/a248/CudmoreMB/RandForums/dsktop_snapshot_JTextArea.jpg[/IMG]

Awt.TextArea -- Works welll, but isn't displaying its own ScrollBars properly, nor does it make ues of the JScrollPane
[IMG]http://i12.photobucket.com/albums/a248/CudmoreMB/RandForums/dsktop_snapshot_3.jpg[/IMG]

And yes, I've called the...

JSP.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JSP.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

And the...

new TextArea("", 5, 5, TextArea.SCROLLBARS_BOTH);

Dani AI

Generated

Short expert note: was right about structure — a Swing text component belongs inside a JScrollPane — but when that wiring is correct and the control still paints or behaves oddly, the JVM itself is a frequent culprit. In this thread ultimately resolved the problem by switching to a newer runtime; that kind of JVM/peer bug is exactly the class of issue seen in older 1.4-era releases. (daniweb.com)

Context from the vendor: the 1.4.x AWT/Swing lineage contains several documented fixes for TextArea/scrolling and related painting/peer problems (the project bug database lists a TextArea scrollbar bug that was fixed in the 1.4.2 line). If an app shows strange scrollbar or repainting behavior, the first quick isolation step is to try it with a newer JRE/JDK (or the vendor’s patched build). (docs.oracle.com)

Quick, practical checklist to reproduce/isolate:

  1. From a shell run java -version and which java to see the runtime actually used.
  2. Add a tiny in-app version check so the app prints the runtime when it starts:
    System.out.println(System.getProperty("java.version"));
    System.out.println(System.getProperty("java.vendor"));
  3. Build a minimal SSCCE that only creates a frame, a single JTextArea wrapped by a JScrollPane and run it with both runtimes (old vs new) to confirm whether the JVM is the cause.

If the small test reproduces the bug on one JVM but not another, prefer the newer JVM or ship with an explicit java binary path; don’t mix production-critical UI on an old, unpatched JRE.

Also follow Swing best practices while testing: construct/show UI on the Event Dispatch Thread and avoid mixing AWT heavyweights with Swing lightweights (AWT TextArea vs JTextArea can introduce peer/z-order/painting issues on older runtimes). Use the EDT startup pattern and, when troubleshooting, force the cross-platform L&F to rule out L&F/peer interaction:

SwingUtilities.invokeLater(() -> createAndShowGUI());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

(docs.oracle.com)

If the newer JVM fixes it, keep a small reproducer and the java -version output when filing a bug with your distro or the JVM vendor — that makes diagnosis and patching far faster.

Recommended Answers

All 5 Replies

Create scrollbars that with swing constants that suit you, then setWrapStyleWord(true) to the text area. Then add the JTextArea TO THE JSCROLLBAR. (Then the scrollbar to the container)

JTextArea textarea = new JTextArea(params);

JScrollPane test = new JScrollPane(textarea,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

content.add(test);

Yep. That's pretty much what I had:

JTextArea output = new JTextArea("");
//output.setEditable (false); // leave true while testing

JScrollPane JSP = new JScrollPane (output);
JSP.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JSP.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
JSP.setAlignmentX(0.0f);

add (JSP);

Did you add the scrollpane to the tabbedpane and then the tabbedpane to the container? Remember, there is no need to add the textarea to the container since it alread has a 'container' and the same with the scrollpanes if you want them on the tabbed pane

:cool: There was nothing wrong with my code.

It was the JRE/JDK..
I installed version 1.5 (I was using 1.4.2) and it works perfectly now.

Sorry for the confusion, but if anyone else encounters this problem, and if they read this, then they'll know what to do..

Note to all:
Suse 10.1 Linux; java-1.4.2; GUI problems

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.