I have developed a code in Java where I have embedded HTML content into a JEditorPane by setting its content type to text / html.
However this content spills over to the second page and so on. I know that the JEditorPane has a print() method that prints the contents of the JEditorPane and I know that the print() method of the JEditorPane prints all the contents of the JEditorPane and even prints multiple pages.
What I want is that the top, left, right and bottom margins should be set to 0.
If I would simply leave the work of setting the margins to 0 at run-time by changing the print settings before printing, it would just not work.
Due to this I had to implement the Printable interface. But when I used the Printable interface it allows to print only the first page of the JEditorPane.
When I searched a lot for this solution I got this link on Google;
http://download.oracle.com/javase/tutorial/displayCode.html?code=http://download.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java
But the problem here is that I am using html contents in the JEditorPane. I dont know how to integrate the above solution provided in the link with a JEditorPane that consists of HTML Code
If you need real WYSIWYG use articles about paginated printing from here
http://java-sl.com/articles.html
If you need just print JEditorPane's content paginated use this
http://java-sl.com/JEditorPanePrinter.html
Related
Is there a way to detect if a (synthatically correct) HTML rendered in a JLabel has rendered succesfully, and is displaying all intended Glyph(s)
I have the following HTML String:
<html><h3>Title</h3><ul><li><b>S</b>tuff</li><li><b>S</b>taff</li><li><b>S</b>tiff</li></ul></html>
That renders:
TitleStuffStaffStiff
However, in an application, the same HTML inside a JLabel rendered as:
As it can be seen, nothing is bold, my custom HTML parser can check for HTML issues, and it detects that String as correct, it then parses "correctly", and renders the Glyph(s), however, the bold Glyph(s) are being replaced by their default plain Glyph(s).
Is there a way to programatically detect that the Glyph was not used, and search for the Glyph, and replace it?
Also worth noticing:
Similar question(s) have not yielded significant result(s). I can confirm that the HTML is always correct, and even using the "test HTML", the issue occurred.The Font contains the bold faceOther HTML markup is being rendered correctly elsewhere(p, i, h, etc.)The HTML is from other supplier(s), and should not be altered/handled/defined
JLabel text by default is already bold. You cannot double bold it, you can only unbold it and rebold it.
Try:
<html><body style="font-weight: normal"><h3>Title</h3><ul><li><b>S</b>tuff</li><li><b>S</b>taff</li><li><b>S</b>tiff</li></ul></body></html>
I am implementing SWT Browser in RCP application for displaying HTML pages.
For Printing we use JavaScript function - htmlBrowser.execute("javascript:print()");
This works fine but my problem is that I want a my own Header with Logo and Footer (some text)
on print pages. please help me with suitable example
The only possible solution I can think of.
Open a new Browser
Get the text (browser.getText()) from the parent Browser
Wrap it in your header and footer html
Set the text (browser.setText()) on newly opened Browser
Hide it - shell.setVisible(false) (Only if you don't want to show the popup)
And invoke browser.execute("javascript:print()");
I suspect printing the content after hiding the Shell. But, you can give it a try!!
I have html file and I have the label name now I need to identify the html object.
Can you please help me to identify the object.
I am using jsoup to parase,
I could not attaching the screen shot,
The page has top row with label below are html object
program, study, study status, study manager (all are labels and below html obj)
text box dropdown, drop down, text box (html objec)
When working with HTML, it pays to be aware of the structure of the HTML elements, and not how they are rendered on-screen. In your case, you will need to find a way to identify the elements you seek in the HTML code, and and then use one of the Document#getElement(s|)By(.+) methods to find it.
I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
Neither JTextPane nor JTextField isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is the JEditorPane or its descendant, JTextPane.
The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style hrefs.
If you want to get fancy programmatically, you can access the DocumentModel and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.
EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.
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.