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
Related
I want to put a hyperlink and some other text in a swing component. This component should be able to fit all of the text in its horizontal space (i.e. no horizontal scrollbars) by wrapping by words, and only by characters when words are too long to fit across the entire component. Parts of this can be accomplished using certain components:
JEditorPanes support hyperlinks. However, they don't break on characters and do weird things when placed in JScrollPanes.
JTextAreas can wrap by words or characters, but do not support hyperlinking.
Is there some combination of these components, or some way I can get one to act like the other?
Some of the other SO questions that I've looked at (for reference):
Wrap long words in JTextPane (Java 7)
JEditorPane inside JScrollPane not resizing as needed
How can I add a clickable URL in a JTextArea?
Note: I am using java 8, and would prefer not to have to download anything, if possible.
Thanks in Advance!
Can you get a JEditorPane to break on characters if you add this to its stylesheet?
word-wrap: break-word;
If you want to elaborate on the "weird things" that might be holding you back from using it, we can try to address those too.
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 );
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.
Okay, I'll admit - the title is not the most descriptive/helpful but I couldn't really think of a better way to put it, which is probably also why I couldn't find an answer when I was searching.
Basically, I'm making a basic MS Notepad like text editor and I want to add two JButton's to make a JTextArea's font Bold and Italic. I need someway of indicating whether the text is currently bold and/or italic like in MS Office programs where the background of the buttons are orange when text is bold, italic or underlined - only I can't seem to change the background of the button. I think this is due to the fact that I am using the operating system's look and feel, but that information still doesn't solve my problem.
So does anyone have any suggestions on how I can provide some feedback as to whether the text is bold and/or italic through the JButton like in Microsoft Office Word? Thanks in advance.
Hmm...sounds like you'll want to make use of the JToggleButton class.
You can set the background color of a JButton object with the method setBackground.
So:
JButtonName.setBackground(Color.ORANGE);
In addition to David's and mre's comments, JButtons and by extension JToggleButtons are not opaque by default, so the background is not painted. In addition to setting the background color, you also need:
jButtonName.setOpaque(true);
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