How to show <b> and <i> in jtextarea java - java

My requirement is to add ""string"" and ""string"" tags only not formatting in the screen using shortcuts ctrl+b and ctrl+i
I used the below code to add italic and bold tags
static void addBold1() {
int end = 0;
String text = ta_description.getText();
String selectionStart = ta_description.getSelectedText();
int index = ta_description.getText().indexOf(selectionStart, ta_description.getSelectionStart());
int len = selectionStart.length();
end = index + len;
ta_description.insert("<b>", index);
ta_description.insert("</b>", end + 3);
}
Where ta_description is the textarea. With this i can add bold tags when the textarea is opened but when i reopen the screen bold tags are not coming. I dont need editor and text pane. I need to solve this using jtextarea. Suggest some idea.

This is achieved by using cdata tags at the start and end as like below
<![CDATA[ <b> பெங்களூர் 28-4-2014</b> <i> தினத்தந்தி</i>]]>

Related

Get start position of character styled texts in a paragraph in Aspose Words for Android

It has being parsed Ms Word documents with Aspose Words for Android below code. All of paragraphs in the document have inline character styled texts seperatelly. I've text and style of them but are there any way to get start position of them in its paragraph string like String.indexOf() ? It may be convert to string, but style control is not possible in this case.
Document doc = new Document(file); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName()
// Start position of the different styled text in its paragraph.
int runStartPosition; // ?
}
}
}
You can calculate length of text in runs before the styled run. Something like this.
Document doc = new Document("C:\\Temp\\in.docx"); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
int runStartPosition = 0;
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName();
System.out.println(runStartPosition);
}
// Position is increased for all runs in the paragraph.
// Note that some runs might represent field codes and are not normally displayed.
runStartPosition += run.getText().length();
}
}

Two extra lines added to edit text when retrieving html String

I save my formatted edit text in sqlite database in html then after retrieving it two extra lines are added to the edit text
So I started like so:-
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
String text = Html.toHtml(e);//convert to String
Then after I inserted text into the database .I retrieved it back to edt this way:-
Spanned sp = Html.fromHtml(text);//convert text to spanned
edt.setText(sp);//setting to the edittext
It was retrieved successfully however 2 extra lines where added to edt at the end of text each time why you think that occurs?.
try this
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
/* String text = Html.toHtml(e);//convert to String
Spanned sp = Html.fromHtml(text); //convert text to spanned */
edt.setText(e);//setting to the edittext
I finally found a solution using code from an answer in this question Remove extra line breaks after Html.fromHtml()
.Its all about removing extra html whitspaces but using it for spannable
String like so :-
Spanned sp = Html.fromHtml(subnote);
int h = sp.length();
// loop back to the first non-whitespace character
while(--h >= 0 && Character.isWhitespace(sp.charAt(h))) {
}
sp= (Spanned) sp.subSequence(0, h+1);
edt.setText(sp);

Select single word or string (doubleclick) in order to edit

I'm testing the functions of text editor and I would like to be able to select text (either a word or a string between 'p' tags) and then edit it by clicking a button (B for bold or I for Italic). Much like the editor used here at Stackoverflow. This will add an html B tag so that it shows as bold.
I know the following Xpath leads to a string of text but I can't find how to select this text or how to select a word in this text.
This is part of the page source (text editor is in an iframe)
<html>
<head>
<body>
<p>
This is a line of text to test Bold
</p>
<p>
This is a line of text to test Italic
</p>
</body>
Where this
driver.findElement(By.xpath("/html/body/p[1]")
leads to a text:
This is a line of text to test Bold
I would like to select the word 'Bold' (or the entire sentence if this is easier) and then click a button above the text to make it Bold
Try this
Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();
What you could do is to use an <html> Tag within JLabels for instance.
What I have in mind looks like the following:
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><b>" + text + "</b></html>";
label.setText(formattedText);
}
}
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><i>" + text + "</i></html>";
label.setText(formattedText);
}
}
Unforunately, I do not know how your text editor looks like, but this is the way I would approach the problem.
Anyways, in a larger scale you can still use HTML tags to format the text (e.g., in a TextArea). In order to get the highlighted/selected items you might want to have a look at this thread how to get highlighted text in jtextarea.
So you could try something like this:
// retrieve selected text from text area
String selectedText = textArea.getSelectedText();
// get whole text
StringBuffer text = new StringBuffer(textArea.getText());
// remove the selected text
text.remove(textArea.getSelectionStart(), textArea.getSelectionEnd());
// make the selected text, e.g., bold.
selectedText = "<b>" + selectedText + "</b>";
// add the bold text to the whole text
text.insert(textArea.getSelectionStart(), selectedText);
//eventually apply outer <html> tags
String result = "<html>" + text.toString() + "</html>";
// set new text (with bold element) to the text area
textArea.setText(result);
Please note that I have not tried this, so you might tweak some things in order to make it work.

How to make some letters in string BOLD

I'm looking for an easy way to make some letters in a label bold.
I have a string like this:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
and a label:
Label s = new Label(r);
and I need to make the "y" and "sin(x), cos(x)" bold. I tried using HTML, but it didn't work (maybe i was using it wrong). If I set font for that label, then the whole label is bold (including those outputs) which is not what I need.
You can use html style in JLabels and in some other Java components. If you start your text with <html> and end it with </html>, the html code in your JLabel will be rendered.
This should resolve your issue:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
Try using HTML (you have to start the string with <html> and end it with </html>):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
Explanation! <html> tells Java to render HTML (Hyper Text Markup Language), and <strong> tells HTML that the text inside is of strong importance, normally represented in bold.

How to set font color for selected text in jTextArea?

I have to set defined color(like red) for selected text in jTextArea. It is like highlighting process in text area (jTextArea). When I select particular text and click on any button it should change in predefined color.
I can change jTextArea to jTextPane or JEditorPane if there is any solution.
Styled text (with a color attribute for characters) is available as StyledDocument, and usable in JTextPane and JEditorPane. So use a JTextPane.
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
StyledDocument doc = textPane.getStyledDocument();
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
if (start == end) { // No selection, cursor position.
return;
}
if (start > end) { // Backwards selection?
int life = start;
start = end;
end = life;
}
Style style = textPane.addStyle("MyHilite", null);
StyleConstants.setForeground(style, Color.GREEN.darker());
//style = textPane.getStyle("MyHilite");
doc.setCharacterAttributes(start, end - start, style, false);
}
Mind: the style can be set at the creation of the JTextPane, and as the outcommented code shows, retrieved out of the JTextPane field.
First of all you can not do this using JTextArea because it is a plain text area.You have to use a styled text area like JEditorPane.see here.You can use a HTMLDocument and do what you want.See here

Categories