Syntax colouring using Swing - java

Is it possible to have something like a JTextArea which will color some keywords based on some mappings I have ?

Here are a few other choices that you could try
http://ostermiller.org/syntax/editor.html
http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html
http://fifesoft.com/rsyntaxtextarea/
http://code.google.com/p/jsyntaxpane/

I am not sure, because I believe JTextArea only directly supports a single font in a single color.
According to SyntaxHighlighter class article, JTextPane is the way to go.

Related

Changing the font of a text in a JTextPane dynamically

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

How to change the String Style

Can we change the String Style in java from normal to bold...
Example: I wish to change the String "Name" from normal Style to bold Style. Any default
method included in java?
The question is a bit vague, but in Swing you can use HTML in order to do format the way your strings appear in components:
jLabel1.setText("<html><b>Bold text</b></html>");
jButton1.setText("<html><b><i>Bold and Italic text</i></b></html>");
A string is just the representation of some text. Applying some style information depends on the GUI framework you use: Swing, AWT, JSP, etc...
It depends where do you want to change its style ?
If you are using Swings' components or AWT or something like that then surely you can do it.
A String object in Java just holds the data. It does not have any styling associated with it. Display styles are associated with the presentation components. You sure are missing something.
If you are using a GUI System like a JLabel, JTextField etc, then you can go two ways:
eg:
JLabel myLabel = new JLabel("<html><b>My Text");
or
Font F = new Font("Calibre",Font.BOLD,12);
myLabel.setFont(f);
Hope that answers you Question.
There are two ways,
Either create a font by yourself ,
something like, Font abc = new Font("Arial",font.BOLD,15)
and for any of the components you can use the setFont() method to set the font for the component.
See the javadoc for more
Use html,
Java supports html formating on all of its swing components.
Here is How to Use HTML in Swing Components.
Any default method included in java?
So I suppose for you its better to use a font, while setting the style via html means, if only for that text ,its not default for the component.
This is a generalized answer, for more specific answer,ask more specific question :)

JEditPane inside another JEditPane in swings(java)

I've a JEditPane "jep" to write in it like a document..
I want to add another JEditPane inside this 'jep'. is it possible to add? if so how? Please help me..
Below is an image showing the requirement. The whole image is JEditPane, on the leftside i need another box(yellow box in image) which i can write someting in it (like EditPane).. please give some hints.. thank you..
A JLayeredPane with the jep in the lowest (back-most) layer would allow you to float the other JEditorPane above it, by adding it to a higher layer. However, you would need to compute where the top pane is to be located and explicitly position it there yourself.
How to Use Layered Panes tutorial
Take a look at this
http://java-sl.com/Pagination_In_JEditorPane_HF.html
Adding headers and footers in paginated content of JEditorPane. You can use the same approach calculating necessary position of your internal JEditorPane.
An alternative approache is to create a custom tag in HTMLEditorKit like described here
http://java-sl.com/custom_tag_html_kit.html
and use JEditorPane rather than button.

Looking for a way to highlight specific words in textareas?

Hi i'm looking for a way to highlight specific words in text kind of like how a text editor might work with syntax highlighting. The highlighting will consist of the text being different colours and/or different styles such as italic, bold or regular.
In order to narrow focus, how this might be achieved using Java Swing components.
There are most probably a number of ways of doing this but one that is efficient in dealing with multiple highlighted words and large amounts of text.
Any feedback is appreciated. Thanks.
You can use the Highlighter that is available through JTextComponent's setHighlighter().
Have a look at Java's JEditorPane class: it does what you want.
I would use a JTextPane. Its easier to use than a JEditorPane as you don't have to know or worry about HTML. The link you where given to the Swing tutorial covers both components.
You probably need something like RSyntaxTextArea. I personally like it because:
- it's easy extendable
- it comes with really useful plugins
- opensource & free & maintained
- it supports bunch of languages like: C, Java, Ruby, Php, HTML, CSS, Sql and so on.

Java Swing: what class can be used to implement this?

What Swing class can I use to implement something like this?
Add To List http://img22.imageshack.us/img22/3260/swingwidget.jpg
EDIT: Hmm..for some reason I cannot add the image here. Well, here's the link:
Basically, I need a list table, where each column can be of different type of gui (i.e. plain text, check box, or drop-down menu).
EDIT I have re-publish the image for you ;)
You would use a JTable to implement it. Your data will be stored in a TableModel. The DefaultTableCellRenderer and DefaultTableCellEditor should do what you need, but you can also customize the rendering/behavior if necessary.
More info on renderers/editors: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender
Definitely JTable.
JTable allow you to define what render will have each column. That way you can add checkboxes, combos, textfields etc. to it.
Here's an screenshot:
alt text http://img43.imageshack.us/img43/9430/jtable.png
You can read more about it here: How to use Tables
JTable will do this for you, you're going to need to understand the MVC pattern pretty well to acheive this as you will need a custom model and renderer, but once you get the hang of it its not too hard.
David gearys book "Graphic Java Vol.2" has an excellent section on JTable, whilst this book is now fairly old I still personally think thats the best explanation of JTable I've seen.

Categories