Scroll through JLabel held in JScrollPane - java

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.

Related

How can I prevent JTextPane to grow horizontally...?

I am building an application with several tabs containing JPanels with JTextPanes. While testing, I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that? Also, how can I force that the text, once the line has been completed, jumps to the next line on the JTextPane?
Thanks!
UPDATE: I opted for the first option that Julien Gauthier mentioned, which is using JTextArea and enabling the Wrapping. It worked neatly. Given that the size of my text boxes (or areas) is more than enough for the limit I established as text capture (800 characters), I did not need to add Scrolls to the text areas. And also, I did not had any trouble with my save-text-to-file functions with JTextAreas instead of JTextPanes. Thanks a lot for the help!
I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that?
Add the text pane to a JScrollPane and then add the scroll pane to the frame. Then the text will wrap and scrollbars will appear when required.
Read the section from the Swing tutorial on Text Component Features for a working example of a text pane.
You have several possibilities :
You can first replace the JTextpane by a JTextArea, and use jTextArea.setLineWrap(boolea wrap).
You can use an editorKit with your JTextPane, wich will wrap the text. Please look this example : http://java-sl.com/tip_html_letter_wrap.html
You can use a JScrollPane, but the result will be ugly with long sentences.
Good luck.

Hyperlink in swing component with character wrap (combination of JEditorPane and JTextArea)

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.

How can I make it so a JLabel only displays the first line of text?

Take a look at this picture:
This is a JLabel with a height that is not allowed to vary. However when there is too much text to fit in its given size it attempts to display both lines. I do not want this, how do I make it so that only the first line up to the "in new" is displayed?
Why not make it scrollable? This way you can maintain the preferred size, and at the same time display the info when it's needed. You just have to put the JLabel inside a JScrollPane:
JScrollPane scroller = new JScrollPane(yourJLabel,
JScrollPane.VERTICAL_SCROLLABAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLABAR_AS_NEEDED);
The solution was to not use html (ie, "<html>" inside of the string) for it was what was causing the lines to warp when too long. All bolding and monospacing had to be done without using html.

How to draw line in JTextArea and change color of a specific words

I'm currently developing JTextArea with a Notepad++ like function (currently indentation function finished).
Now I'm trying to add a function which in my JTextArea will show a dotted line for an indentation with same level, and change specific color of a word.
Thanks in advance :)
You will not able to achieve your target with JTextArea as it is a plain text support component.You have to use a Styled text supported JEditorPane or JTextPane.And try to use a HTML document and achieve it.Look at
Styled supported components
and
some more examples

Replacing smiles with images in Swing text area

I am designing a chat system..i am making use of JText area to display chat, and JTextField to enter text.
My question is how to recognize smiley's like ":)" and replace it by corresponding image
on text area? i found no method which will append image on text area..Please help.
http://java-sl.com/tip_autoreplace_smiles.html
You'll have to use a read-only JEditorPane to display HTML instead of a JTextArea in that case.
JTextArea was made to display only multiple rows of text, but correct me if I'm wrong. To display Images you could use the JEditorPane control that will allow you to use html, with simple <img /> tags which will point to a image.
Regarding how to recognize smileys you could create a file / list of common patterns that you would like to support and then simple check if the text contains the pattern with the .contains , or even Regular Expressions.
Update
And with the JEditorPane you will also be to do addition things like say scan for emails or links and automatically convert them so the user can click them, always a nice feature to have.

Categories

Resources