Different text color in a JTextArea - java

Is it possible to have different rows in different colors in a textarea? Have you an example for that?

Not with a JTextArea. You can, however, use a JTextPane which allows for attributes to be applied to text. If you want examples, take a look at the Text Component Tutorial.

Related

Multiple fonts and styles in JTextComponent

I am looking for a way to print content in JTextComponent's text with multiple fonts and styles.
For example: first Title with bigger font and then some custom text in different style in one component
You can use a JTextPane for this. You can control font, color, bold, size etc.
Read the section from the Swing tutorial on Text Component Features for more information and working examples.
For a another simple example to get you started check out: Is there a way to filter / search for content in a HTMLEditorKit?

JTextArea - are 2 different highlight colors possible?

I want to highlight the text in a JTextComponent with two different colors. Is this possible with JTextArea (and if yes how?) - or do I need to use JTextPane or JEditorPane (if latter: which one is to recommend if the only thing I really want to 'style' are different background colors)?
thanks in advance

Swing: non input text field

I'm new in Swing, and don't want to learn it. I just want to write a simple frame divided into two equal parts: top and bottom. The top part of frame should be a simple immutable text field.
Problem: As I understand, to show text with beautiful font I should use JTextPane. But JTextPane:
Doesn't support vertical text alignment; (I haven't got any desire to write something like that)
I don't know how to switch off editing.
Quedtion I believe there is a simpler solution for my purpose. Is there?
The top part of frame should be a simple immutable text field.
Use a JLabel. It supports HTML which might help with your formatting.
I don't know how to switch off editing.
setEditable( false );

How to give different - 2 color to text written in JTextArea?

I want to change color of text written in JTextArea. For e.g. I am pasting Java code in JTextArea.
I want to give the different-2 color to Java keywords, variables and classes written in code.
Is it possible?
See Rob Camick's Message Console. It seems perfect for this use case.
Actually '2 color' might not be enough for this.
See How to Use Editor Panes and Text Panes for more details on the formatted text components that might replace JTextArea.
it's not possible
if you want to set different colors in the same area, i think JTextPane could help you
i'm not sure how to use that, but i found this
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

Scroll through JLabel held in JScrollPane

I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.

Categories

Resources