JTextComponent is the base class for swing text components. It tries to be compatible with the java.awt.TextComponent class where it can reasonably do so. Also provided are other services for additional flexibility (beyond the pluggable UI and bean support).
Questions tagged [jtextcomponent]
149 questions
38
votes
7 answers
How to change text color in the JTextArea?
I need to know how to do this:
Let's say: I have a code in the JTextArea like this:
LOAD R1, 1
DEC R1
STORE M, R1
ADD R4, R1,8
I wanted to change the color of LOAD, DEC, STORE and ADD to color BLUE
R1, R4 to color green
M to RED
numbers to…

Celine
- 475
- 1
- 7
- 7
15
votes
2 answers
How to use html tags in JTextArea
When I try to change color of a JTextArea using
textArea.setText(" Hi ")
, the text is shown is basically the whole text written inside setText method. But similar things work for JLabel, JButton etc.
How…
user1508381
13
votes
4 answers
Caret position is off in JTextArea
I am adding a JTextArea to a component with a layout manager that respects preferred size and location. However, the position of the displayed caret in the JTextArea becomes erroneous after typing a few "wide" letters (e.g. 'm'):
This is after…

Daniel Maly
- 317
- 5
- 13
13
votes
4 answers
How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
In all the examples that I can find that use a JTextArea, the height & width is known before constructing the JTextArea, and if the JTextArea would require more height, then it is put inside of a JScrollPane. Obviously, the height of JTextArea is…

Nate W.
- 9,141
- 6
- 43
- 65
11
votes
3 answers
how to visualize console java in JFrame/JPanel
I made a Java program using Swing libraries.
Now I would like to redirect my console outputs into a JFrame or JPanel.

Gianfra
- 1,119
- 4
- 17
- 33
10
votes
2 answers
How to determine which lines are visible in scrollable JTextArea?
How to determine number of the first visible line and the number of lines currently visible in scrollable JTextArea (JTextArea inside a JScrollPane)?

Ma99uS
- 10,605
- 8
- 32
- 42
10
votes
5 answers
Make parts of a JTextArea non editable (not the whole JTextArea!)
I'm currently working on a console window in Swing. It's based on a JTextArea and works like a common command line. You type a command in one line and press enter. In the next line, the output is shown and under that output, you could write the next…

Thomas Uhrig
- 30,811
- 12
- 60
- 80
9
votes
2 answers
Why doesn't JTextComponent.setText(String) normalize line endings?
It has recently come to my attention that Java text components use line feed characters (LF, \n, 0x0A) to represent and interpret line breaks internally. This came as quite a surprise to me and puts my assumption, that using…

predi
- 5,528
- 32
- 60
9
votes
3 answers
How can I set each character to a different color/background color in a JTextPane?
I've been searching for this for a while and so far all I've been able to come up with is how to create a style and apply it to a character like so:
StyledDocument doc = (StyledDocument) new DefaultStyledDocument();
JTextPane textpane = new…

JaredL
- 1,372
- 2
- 11
- 27
8
votes
1 answer
Why is this font so big in Java?
I’m trying to use OpenDyslexic as a font option in a Swing application. But surprisingly, OpenDyslexic looks much bigger than any other font at the same point size, even though it looks normally sized in other applications. I’ve tried a handful of…

yonran
- 18,156
- 8
- 72
- 97
7
votes
3 answers
Using Actions with DocumentListener
I'm developing an application where I want something to be triggered both by the user updating the contents of a JTextArea, or manually via pressing a JButton.
I have done the first part using a DocumentListener and putting the relevant code in its…

Luigi Plinge
- 50,650
- 20
- 113
- 180
7
votes
1 answer
Smart JScrollPane autoscrolling
I am attempting to implmement smart autoscrolling on a JScrollPane containing a JTextPane. The JTextPane is used for logging my app in color. However I'm running into a wall trying to do smart autoscrolling. By smart autoscrolling I don't mean…

TheLQ
- 14,830
- 14
- 69
- 107
7
votes
2 answers
jTextArea stops showing highlighting on text after losing focus
When my jTextArea is in focus it allows text highlighting, but it doesn't show the text selection when it loses focus. Is it possible to continue displaying the text highlighting even if the user moves focus to another component on the related…

user2689323
- 73
- 5
6
votes
2 answers
JTable, JTextArea or JEditorPane to highlight lines of code?
Update:
I've found a partial solution in this answer here, by adding the following code:
class CustomRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,…

Ashley
- 2,256
- 1
- 33
- 62
6
votes
2 answers
JTextComponent Keymap
I need to create a class derived from JTextComponent (from JTextPane, actually) in which at least one of the default key mappings is changed. That is, in my special JTextPane, I want the ">" keystroke to perform an action and NOT add that character…

Steve Cohen
- 4,679
- 9
- 51
- 89