How to set a tooltip on a portion of String text? - java

what I want is to set a tooltip to a portion of text inside a Jtextpane document.
What I am doing now is to insert the first portion of text with jTextPaneDoc.insertString into the jtextpane. And adding the second part also with the same insertstring method but I want the second portion to have a tooltip.
I know I can easily do that by adding the second text portion into a jtextfield and set the tootltip on the jtextfield but this solution result to alignment issue. And also I would not be able to select the entire text as the jtextfield will not be selectable with the text.
The picture shows that the string text and the jtextfield are not aligned
Is there a way to add a tooltip to something not a component (like string) in Java?
Thanks to help

Related

Create a JLabel dynmaiclly

Hiļ¼Œthere is a problem in my java application, the text in my Jbutton is too long so it only show"..." on the button. Now I want to add some component to help show the actual text on the button.
What method can I use to solve such problem rather than adjusting the text font or button size?
Thank you
Instead of a label, you should add a tooltip to the button:
jb.setToolTipText("The full text of the button");
A small downside is that this tooltip will also be shown if the button text is fully visible, and in that case the tooltip doesn't provide any additional information, which will be confusing. I don't know off my head how to solve this, but it's definitely possible.

Ghost text in jTextField swing

Have a problem with GUI.
How to add text in jTextFiled that will disappear when I click on this field?
Have no idea how to realize this. jTextField has only one field for this property.
Thank you!
You can use the Text Prompt class which adds this functionality to any text field.
You have a property to control when the prompt is displayed.
The class basically adds a child (JLabel) to the text field which is painted when the text field doesn't have any text.

I want to update a particular image on TextArea(swing) ,when I enter a particular Alphabet on my keyboard. How?

I am trying to print particular images(which include some drawn symbols) on textarea when I type a particular Alphabet on my keyboard. How?
You can't use a JTextArea since it can only display text.
You can use a JTextPane as it supports the display of Icons.
Check out: Auto Replace Smiles. It shows how to replace the two text characters :) with an Icon.
You are asking me to do your homework, but still. I will give you only the pseudo code, you will have to write it on your own.
Write a JFrame.
Add a JLabel to the JFrame (using a label is easier than using a text pane, and it is impossible to use a text area).
Add a Key Listener to either the JFrame or the JLabel depending on your application.
In the handler write code to add a ImageIcon to the JLabel when the key you want is pressed or whatever.
Cheers!

How to set columns in a JTextArea to show text

I'm using (in a JPanel with GridLayout) some JTextAreas (with Editable=false) to show some text after a query to a database XML.
JTextArea obj = new JTextArea();
obj.setColumns(37);
obj.setText(r.getProtocolloList().get(i).getOggetto());
The problem is that this text can be quite long and it is showed all in a single line so that the user has to scroll the horizontal JScrollPane to read the rest.
I thought that setting the columns the line would be restricted so that the text would be showed in different lines. But nothing happens.
Is there a way to get that?
Thanks
Use the setLineWrap(); method:
obj.setLineWrap(true);

Changing the font of text being typed in a JTextArea

I am working on a chat application and I want the user to be able to change their font while typing messages.
I tried using the setFont() method but it changes the font for the entire text within the JTextArea component and when the text is sent to the display area, another JTextArea component, the font changes do not appear. Instead it displays the text in the font of that set in the display area.
What I want to do is to change the font for either highlighted textarea or for the marker only and when the text is sent to the display area, the font changes should be evident.
JTextArea supports simple plain text with simple styling only.
I'm having same problem, but seems the answer is that you should use JEditorPane or JTextPane instead of JTextArea.
Editor Panes and Text Panes

Categories