Java GUI with scrolling text and images - java

How do I create a GUI with a scrolling text area and underneath small paragraphs of text there are corresponding images?
I am currently using a JTextArea with a scroll-bar to display the text and it works great, but I want to display the corresponding images underneath each small paragraph of text.

You can use javax.swing.JTextPane
Check TextSamplerDemo
You can find plenty of examples here:
http://www.java2s.com/Code/Java/Swing-JFC/TextPaneSample.htm

The easiest way to do this, would be to use JEditorPane with HTMLEditorKitYou can use HTML text formnatting including graphics, just like a webpage.

Related

Insert ICON/PNG inside TextArea

Without resorting to wrapping a GWT TextArea in a separate DIV, how to add a search icon/magnifier icon inside the text area?
No, Not possible.Text area is not a normal Widget.
GWT's TextArea maps directly to a HTML TextArea thats why you can only set text to it. The only thing you can do ist create a Grid/VerticalPanel/HorizontalPanel and put in each cell a separate TextArea.
Google Groups
Similar Question
Use a Composite wrapping a FlowPanel, put a text area and an image inside, then in CSS position the icon absolutely so it's over the text field.

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.

Java wrap text around image

How to wrap text around an image in Java? Or how to accomplish CSS float in Java?
I am loading a HTML text with some image tags into JTextPane component.
Example:
text text text text <img src="image.ext"> text text text
Example2: http://www.bbc.co.uk/news/world-us-canada-11882019 (text left, image right something similar)
And i need the image to be wrapped with a text.
Tried using css... float: right; (Java doesn't support that feature.. need it..)
Tried all image alignments(left, right etc.) (no luck..)
P.S. PHP programmer..
The Swing tutorial on Using Text Components, shows how to use a JEditorPane to display HTML with an image.
I've implemented something like this (text flow around images and tables) for SoftChalk project (you can see the trial from http://softchalk.com/) but it's not trivial. It required a lot of changes and took significant time to implement and embed this in HTMLEditorKit extension.
I had to extend a lot of views (BlockView extension and completly change layout rows algorithms in all the possible containers - BODY, DIVs, Table cells, list items etc.

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