How to display bold text in only parts of JTextArea?

No. What you’re looking for is JEditorPane This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text. JEditorPane textarea = new JEditorPane(“text/html”, “”); textarea.setText(“Here is some <b>bold text</b>”); EDIT: According to the javadoc I referenced above, JEditorPane also supports limited RTF. Don’t forget to change the … Read more

How to count the number of lines in a JTextArea, including those caused by wrapping?

You can use LineBreakMeasurer Class. The LineBreakMeasurer class allows styled text to be broken into lines (or segments) that fit within a particular visual advance. This is useful for clients who wish to display a paragraph of text that fits within a specific width, called the wrapping width.LineBreakMeasurer implements the most commonly used line-breaking policy: … Read more

Make JScrollPane control multiple components

You should use JScrollPane#setRowHeaderView to set the component that will appear at the left hand side of the scroll pane. The benefit of this is the row header won’t scroll to the left as the view scrolls to the right… The example deliberately uses line wrapping… import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FontMetrics; import … Read more

jTextArea as IO console

Okay, so this is my idea… The basic idea is we want to keep track of “user” input and “process” output. Basically what I’ve done is set it up so that when the process terminates, we calculate the current position of the caret in the document and mark that as the start position of the … Read more

How to set the orientation of JTextArea from right to left (inside JOptionPane)

and the scrollbar will be on the left scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); so the text inside it will start from the right textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); The text starts on the right side, but still gets append to the end as you type instead of being inserted at the beginning of the line. Update: I don’t know why it doesn’t work … Read more

ActionListener for a specific text inside a JTextArea?

Here try this small program, try to click at the start of student://, that will pop up a message Dialog import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextAreaExample extends JFrame { private JTextArea tarea = new JTextArea(10, 10); private JTextField tfield = new JTextField(10); private void createAndDisplayGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tarea.setText(“Hello there\n”); tarea.append(“Hello student://”); JScrollPane … Read more

Internal padding for JTextArea with background Image

Use a custom border that extends AbstractBorder. Something like this: Getting the exact shape & color is left as an exercise for the reader. 🙂 import java.awt.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.border.AbstractBorder; class TextBubbleBorder extends AbstractBorder { private Color color; private int thickness = 4; private int radii = 8; private int pointerSize = … Read more