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
Related
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.
I want to display multiple-line text for each item in combobox. Words should go to the next line automatically if the exceed the width of combobox. How to achieve this?
String meanings;// array which contains pretty long sentences
JComboBox<String> meaningsComboBox=new JComboBox<String>(meanings);
Maybe wrapping each value into html will help you(this trick work with JLabels), example here.
In another way, I think, you need make custom renderrer, example here
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
I have questions according to my final project in my IT Faculty.. I have to make A simple text editor (like notepad) without using JTextArea (GUI Java), I have to make my own JTextArea..
Idea/Topic= Own Text Editor
Algorithms= doesnt have any idea yet
Data Structure= Rope(Member of Binary Trees)
Requirement= Typing without JTextArea...
Is that possible to make that? Can a panel (or something else like that) has a listener or make a listener to do that?
simply thinking, First step I just want to try for typing on a panel, absolutely algorithms needed but i dont get right algorithms so far.. Is that possible?
My thinking is, for text that im typing is stored to a tree.. Is that Rope(Member of Binary Trees) for my data Structure?
Thanks for helping...
It's possible.
Use as you mentioned JPanel attaching KeyListener and MouseListener.
You need a model (Document). For simplest case it could be e.g StringBuilder where you can add content and show the StringBuilder content.
You need caret position (int field) to indicate where the edit should happen.
You need Font field to keep all the font info to be used in the editor.
Override paintComponent() and use FontMetrics to measure the text and calculate necessary width/height. For the start I would create a text area without line wrap.
Implement viewToModel/modelToView methods. They should calculate position of caret for given x,y and x,y for given caret position.
im trying to implement a Chat feature in my application. i have used 2 JEditorPane. one for holding chat history and the other for sending chat to the previous JEditorPane.
the JEditorPane is text/html type.
the problem i am having is when i put more than one space between characters it is automatically removed by the parser because it is HTML!
how can i make it so, that the spaces are not stripped?
example: hello world
becomes: hello world
also i am having to parse the html tags so the new messages can be added to the history window.
is there a better option than using JEditorPane? if i used JTextPane would it be easier to implement?
i would like the chat boxes/panes to be able to handle bold, URL embedding for now.
thank you and look forward to your guidance.
EDIT: im trying to replace " " with a relavent char.
newHome[1] = newHome[1].replace(" ", newChar)
what should be the newChar value?
EDIT: im trying:
newHome[1] = newHome[1].replaceAll(" ", " ");
but it is not producing the results. any ideas?
EDIT: #Thomas - thanks! for some reason i can post a note to your answer.
Using HTML markup is a quick way to get simple text formatting done in a Swing text component. However, it's not the only way.
A more sophisticated method is to use a javax.swing.text.StyledDocument to which you can attach different "styles" (hence the name). A style is basically a set of attributes, for instance, whether the text should be in bold or italics or what Color it should have.
JTextPane provides a number of convenience methods to deal with styles, and it is a subclass of JEditorPane which means it should integrate rather seamlessly into your existing code. As an example, to mark a portion of the text within a JTextPane as bold, you could use something like this:
JTextPane textPane = new JTextPane();
Style bold = textPane.addStyle("bold", null);
StyleConstants.setBold(bold, true);
textPane.setText("I'll be bold.");
textPane.getStyledDocument().setCharacterAttributes(8, 4, bold, true);
Similarly, you could define a second style that e.g. uses a blue, underlined font and which you could use to display hyperlinks.
Unfortunately, the downside is that you will have to take care of the mechanics of the links yourself. Although you can use the existing infrastructure of javax.swing.event.HyperlinkListener et al., you will be responsible for detecting mouse clicks. The same goes for hovering and changing the Cursor into a hand-symbol etc.