Java Pseudo-Hyperlink - java

So I am working on something in Java right now where I need to display lots of JTextComponents. I need to be able to turn words in any text component into a hyperlink of sorts, specifically, when these words are clicked, some method is called. Please note, I am not looking for a REAL hyperlink, just if you click on some text, a function is called. I believe JTextPane could solve this problem using components, but I can't seem to figure it out..
Yes, this question already exists, but the other one is a duplicate, and the one it links to does NOT answer this question.

If JEditorPane or JTextPane is a satisfactory JTextComponent, then a HyperlinkListener will be invoked to perform any desired action, as shown in this complete example.

A JTextArea could work for this too.
You could use a MouseListener and then translate the location of the click via viewToModel(Point pt).
You could then use javax.swing.text.Utilities.getRowStart(textArea, offset); if you need the start of the row selected, and getRowEnd(...) for the end.
You could have a Map<String, SomeLink> to see if a selected word is a linking word.
For an example of this, please check my answer here.

Related

Make String look like url in TableModel

Hello im making a program and using TableModel. I have 6 column filed with string's. I want the sixth column string make it look like URL. When i mean look like URL , i mean turn it into blue and be underlined. Is it possible to do that?
You need to write custom TableCellRenderer for your jtable. See this link.This may be helpful for you.
Sun had a very good tutorial (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange) on cerating jtable. just go through it before you start. hope this tutorial will help you.
But below is my suggestion.
("<html><b><u>link</u></html>");
<html><b>bold</b></html>
a similar kind of thing
However, I wouldn't recommend altering the data in your model just to effect display. Instead I would create a custom cell renderer which accomplishes this effect and set it on the table. You could either wrap your text in the HTML or manually set font color and style properties on the renderer to mimic html.
Now even if you were to include the url in the html you still can't click on it. There is no component in the table. You don't really want to go into edit mode when clicking on a url. You just want to open that link. To accomplish this you would add a mouse listener to the table itself. When you receive a click event, you would then programatically determine which cell it was over, go back to your model and get the url, and finally use other Java API calls to open that url.
I'm pretty sure you can simply create a string containing
<html>...</html>
and it will work. Just code your link inside the html tags as you would in html. You'll just have to add extra code if you want it to appear blue. I think:
<font color='blue'>
would do it

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

How to present a hierarchical String list with checkboxes in Java Swing?

So. The ideas have been a few.. no good ones.. So here is the problem i have incountered and can not figure out a solution for.
I need to have a txt file containing a hierarchical list of things to do, looking something like this:
Ask a question
Before question asked, check similar questions
Formulate the question so that the problem is clearly shown.
Post the question
Hold your thumbs that this might save your day.
The txt file should in the program fill a list shown in a JPanel and after each step have a checkbox to comfirm "I have been a good boy and done this".
The list can change over time and grow and shrink, so the solution needs a scrollbar.
The list should also contaion a capability to show more the one row.
The solution im working on is done in Jave compiler 1.6 and im using Swing for my layout.
Ideas i have had so far are:
Modified JTable... and the other solutions i am asshamed of..
And thats about it.. ..
JTree with custom renderer. The renderer should be a panle with checkbox and text area.
have look at JTree
have to override JTree Renderer and Editor (comcept is similair for JTable and JTree)
all data are stored in the DefaultTreeModel
examples about Tree Model,Tree Renderer Editor, Tree and here
Also consider org.netbeans.swing.outline.Outline, discussed here and illustrated below. It's not apparent in the screenshot, but the the Outline instance has been added to a JScrollPane.
I would say JTreeTable is a right choice. It will provide a lot of power and flexibility. Please see some very basic examples:
http://www.ibm.com/developerworks/java/library/j-5things14/index.html
http://www.java2s.com/Code/Java/Swing-Components/JTreeTablecomponent.htm
For any serious purpose please use a mature implementation provided, for example, by a SwingLab project:
http://swingx.java.net/
http://java.net/projects/swinglabs
Please also see the following answer:
Java Swing table tree

Java Library to load a document and tag parts of text

I am new to Swing development and I am creating a Java application where a user can load text documents and tag parts of text.
The tagged parts of text would then be highlighted and the user can click on the tagged text to edit/remove the tag. Is there any library/class that I can use to achieve this.
I have already looked at JTextArea. But to best of my knowledge it can only display plain text (correct me if I am wrong)
To be more clear of what I am looking for:
When you tag a question on Stack Overflow (SO), the tags are highlighted with a little box and highlighting around the tag. I am looking something similar in Java.
So, if the component that I add is say X, then X should be able to hold plain text as well as tagged text. The tagged text should look like the SO tags.
Edit
An example for clarification.
Original Text:
This is some sample text.
After tagging, say sample, this is what it should look like:
With Tagged Text
That x is basically a button to remove all tags associated with sample.
this can help you jeditorpane and jtextpane
There's a working example here that sets color using StyledEditorKit.ForegroundAction. It also illustrates related classes in StyledEditorKit.
My idea is, take a textfield,when we double clik on it set to editable then write tag and hit enter.. then update the tag, for highlighting use setBackground,setForeground with textfield
JTextField jt=new JTextField("tag");
set it as
jt.setEditabe(false);
add MouseListener to jt see this LINK mouse listener
jt.addMouseListener(this);
write the code in mouseClicked event,if we double click it will set as editable state
public void mouseClicked(MouseEvent e) {
int count=0;
count=e.getClickCount();
if(count==2)
{
jt.setEditable(true);
count=0;
}
}
then edit the tag...do rest of the code,i.e update TAG by hitting enter using key binding see this LINK

Java Android: Make a word (from a paragraph) into a button

I searched for some answers, but came up empty, maybe I'm not wording my question correctly.
Basically I want a hyper link, except it has a java on click listener attached to it, instead of an html link. I have some paragraphs of text and certain words are highlighted, I want the user to be able to click those words and it executes a function.
Any thoughts or website tutorials? Thanks.
You should use the Linkify class to achieve what you want. Here is a full tutorial that will help you

Categories