I would like to display the HTML text in a Java SWT Label.
Below is my code for creating a label.
Label theLabel = new Label(controls,SWT.WRAP);
theLabel.setSize(100,500);
theLable.setText("<html><ol><li>Hello</li><li>welcome</li></ol></html>");
When I run the application as Eclipse Application I get the output as:
<html><ol><li>Hello</li><li>welcome</li></ol></html>
What is the mistake? Why I am not getting the html formatted output in my label? I am using Eclipse plugin with a view.
To show HTML with SWT you will have to use the Browser widget instead.
Browser browser = new Browser( parent, SWT.NONE );
browser.setText( "<html><ol><li>Hello</li><li>welcome</li></ol></html>" );
If you don't mind the extra dependency on org.eclipse.ui.forms you can also use FormText. But be aware that the control does only understand a subset of HTML (<p>, <b>, <li>, <img>, <br>, <span>) to render simple formatted text.
Related
I'm developing an eclipse plugin , and at some point in my plugin , a jframe is opened , and inside the jframe, there is a button . I have added a mouselistener to the button , and when pressed , I want some code to be added to the editor at caret point . but, I get null pointer exception here:
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage();
It appears you don't have an active page. Maybe your Swing based code is displaying a separate window?
Use IWorkbenchWindow.getPages() to get an array of IWorkbenchPage containing all the pages and look through the pages for the one containing the editor you want.
How can I draw a horizontal line in java gwt, something similar to the '< hr >' tag in HTML? I tried it by using
com.google.gwt.user.client.Element bottomLine = DOM.createDiv();
but that somehow doesn't work in IE...
You can use the HTML widget to add whatever html you want inside your page
HTML html = new HTML("<hr style=\"width:100%;\" />")
rootPanel.add(html); // or add it inside another widget
Or you can use css on Panel and define the border-bottom property (if you have a panel that spans the entire page).
Document.get().createHRElement()?
I have some amount of informations to be displayed in Dialog Box. It comes like Title, then under it text; Title, then under it text. Like wise, there are 4 titles and 4 descriptions to be displayed. It should come like this
Title One
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
Title Two
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried.
TextView msg = new TextView(this);
msg.setText("<html><u>Message</u></html>")
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);
//rest of the code
However this trick didn't work. What happened is, all the HTML tags showed up as they are! And the text is not clear! Seems like it mixed with the background of the default colour of AlertBox, black. How can I solve this issue? Please help!
PS: Or am I using the wrong method? Wrong dialog box?
You will need to use Html.fromHtml() to use HTML tags in TextView as:
msg.setText(Html.fromHtml("<u>Message</u>"))
And you also see all HTML tags supported by TextView.
As it turns out, you don't actually need any extra TextViews to do this. Simply include the HTML in your alert's "setMessage()" call (which replaces the "setView()" call in your question) and pass it the html-formatted string. Be sure to only use <b>, <u>, and <i> in your formatting, though because those are the only tags it supports. If you're using a String resource for the text in your alert, call getResources().getText(R.id.yourHtmlString) rather than getResources().getString(R.id.yourHtmlString), though, or the tags will be completely stripped from the String.
If you want to add a link and make it clickable,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
If you need to add more complex HTML, with CSS and META, you can add a WebView to the dialog, like this:
String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",
null);
yourAlertDialog.setView(yourCustomWebView);
This way, you can display fully formatted HTML pages in your dialog.
Try this,
Font color,
String source = "<b><font color=#ff0000> Loading. Please wait..."
+ "</font></b>";
Font underline,
String source = <u>Message</u>
msg.setText(Html.fromHtml(source));
In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0) for compatibility with older versions.
I need to set a label to have some special characters, I'm trying:
Label label = new Label();
label.setText("•");
label.setText("♦");
label.setText("★");
I'm not seeing the characters rendered though (firefox 17). The output html looks like this:
<div class="gwt-Label"></div>
Is there a different way we need to set the text to those characters?
I do no think it is possible with Label widget. You should be using HTML class which extends Label.
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendEscaped("★");
HTML widget = new HTML();
widget.setHTML(builder.toSafeHtml());
RootPanel.get().add(widget);
Also ensure best practice of using SafeHtmlBuilder class.
You should make sure your source code uses UTF-8 encoding. If you are using Eclipse, you can set the default encoding in Windows -> Preferences.
The short version of this is: if you do absolutely everything in your GWT project and deployment using UTF-8 encoding, then all your special characters should work as expected.
If you have lots of existing files to convert, the JDK contains a little tool to convert your files for you.
You can use html codes: ★ for star(★), for bullet () and so on.
http://www.quackit.com/html/html_special_characters.cfm
I want to display a regular XML or any other file in text format in JEditorPane..I don't want to display the content in html page... the content should be exactly the same as in file with line break..the XML file is located on local system
Do you need syntax highlighting?
or just show plain text?
If syntax try to use this
http://java-sl.com/xml_editor_kit.html
If not just use JTextArea or normal JEditorPane with default editor kit. And call setText() to set your content.