Setting the font and style of jeditorpane - java

I am currently developing an application for this company as my last year of studies project, and in this application there's this function that allows you to print a paper, so i used the jeditorpane obviously, with the Charles Bell HTMLEditorKit for the predefined functions like setting the font and printing, etc... and there's this pre-written text that i have to load from a text file and then the program is supposed to add some text inside the file automatically, and the user also is allowed to add more if he needs to, or change the font or style or whatever.
So here is the problem, since the predefined text should be already aligned with the font already set and some other properties already in place, and since the txt file doesn't allow that, i tried to use a docx file, which makes the text incomprehensible in the editorpane, i also tried the docx4j api, but then i wouldn't have the HTMLEditorKit menu and functions, (or maybe i could but i don't know how) and the execution of the app becomes rather slow with this api. I also tried this metaphase editor kit which i found it to be really useful with a lot of functions to offer, but i couldn't load a text file to it's textpane.
So my question(s) to be exact is(are):
1)How can i use the charles bell HTMLEditorKit and load a docx file into the edtiorpane in question?
2)Or how can i use the docx4j api with the HTMLEditorKit with the functions menu (style, print, font...) or maybe another predefined menu?
3)Or even better, how can i still use the metaphaseEditorPane with its full of functions menu and at the same time load an existing docx file in its textpane?
Sorry for the long question and any help would count, i would really need and appreciate it.

See Adding custom shapes to JTextpane and saved to new Word document for references to two projects which use docx4j to provide rich text editing of docx within Swing.

I found finally the solution after days of searching and testing, It turns out to be very simple actually.
All i had to do is use a metaphase editor panel, (which has a great toolbar by the way) create a DocxEditorkit and then set the editorkit of the metaphase editor panel's textpane to this DocxEditorKit, although the metamorphose works with HTML text pane, it miraculously worked...
DocxEditorKit DEK=new DocxEditorKit;
MetaphaseEditorPanel MEP=new MetaphaseEditorPanel;
MEP.getHTMLTextPane().setEditorKit(DEK);
//Try and Catch blocks of course to read the file
DEK.read(new FileInputStream(PathToFile), MEP.getHTMLTextPane().getDocument(), 0);
getContentPane.add(MEP, BorderLayout.CENTER);
And that was it... Who thought it would be as simple as this.

Related

How to display image on textpane?

I am working on a note taking application which is supposed to include image ,audio ,video along with text
So im including a feature to add links(like in evernote )which should display an icon on clicking which that file should open
My question is how write to and read from a file so that when a link is encountered an imageicon mus be displayed in textpane similary a link must be written in the file
I am already able to implement text links i want to know how to do the same with images(image links)
You can Use JEditorPane to use the functionality of both text and Images in java . This would also enable you to add support for custom text formats to the components of your application.
For details Refer : http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
As an alternative to JEditorPane: you might want to check out Flying Saucer, which can render XML/XHTML. If you use a suitable XML model internally, you might be able to render your content directly via CSS.

Insert text to a generated pdf document

I have a library which generates pdf document with images.
I want to be able to add text after each image. What is the syntax for that? How to insert text into pdf documents?
I have to use the library I have, not another one.
First of all, mkl is correct, have a look at the specification for all of the details. PDF is an exact language, if you make mistakes they will routinely be punished severely once you open the PDF in viewers.
Secondly, when you think about putting text on the page, don't forget that besides the text operators to draw the text on the page, you'll also have to specify the font to use to draw this text. Which will include making sure there is a font resource included in the PDF file if your library doesn't automatically handle all of that for you.
If you want to cut corners (I shiver while writing this) and perhaps don't read the specification as thoroughly, try this.
1) Create a PDF file that looks more or less like what you want.
2) Use a tool such as pdfToolbox from callas (http://www.callassoftware.com/callas/doku.php/en:download) or Browser from Enfocus (http://www.enfocus.com/en/products/browser). Both of these tools allow you to investigate the low-level structure of a PDF file, including looking at the actual page description code. This will show you how fonts are embedded (if you have to do it yourself that could be very handy) and how text is rendered on the page (and how you set the font, size, color etc... to use).

Homework assignment using Java/Swing

I've got an assignment in a class about user interfaces and usability testing. I have to do something that I'm sure I can figure out how to do programatically, but I have no experience with swing so I have no idea what components to use to do the job. My background is in C# so I'm fumbling at times trying to find the right component to use in NetBeans.
Based on the description below, can anybody recommend what kind of text field will do the job? I was thinking jFormattedTextField (based on the name) but I can't seem to figure it out.
Thanks
Submit a Java program that will run on the Linux installation in the general lab (EN-2036).
Provide the following functionality:
load the output of the last command (from a file) into a text viewing area that
allows the user to browse the data
bold all login names only (not the whole line)
bold all occurrences of a user-specified login name
bold all occurrences of a user-specified set of login names
Use JEditorPane or JTextpane, these allow you to add style to text inside them
Tutorial Link

Looking for a swing (or other) text editor component that has document selectors

I am creating a java app that needs to display multiple documents worth of plain text. I have been playing around with JEditorPane (for each document to display text) with JTabbedPane (for document selection) but I think I would prefer to use something better (if it exists).
Is there an existing class that creates a dead simple editor with document selector functionality built in?
Nothing is built-in to do this, but JDesktopPane might be a better option for selection rather than a JTabbedPane.
For plain text, I would tend to prefer JTextArea "that displays multiple lines of text and optionally allows the user to edit the text." It is described in the article How to Use Text Areas. The append() method is particularly convenient. The articles Text Area Scrolling and Message Console are also very helpful.

Making words different colors in JTextField/JTextPane/?

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.

Categories