This post has been edited by rishabhsharma: 15 August 2012 - 11:17 AM
Coloring Characters
Page 1 of 18 Replies - 234 Views - Last Post: 17 August 2012 - 09:57 AM
#1
Coloring Characters
Posted 15 August 2012 - 11:06 AM
Hello frnds, I am building a Typing Test program, but I am stuck at a point...I am using JTextArea for users to input data and showing the Target Text. I want a method in JTextArea, so that I get power to color only some characters in it(rather coloring whole text). Can anybody please suggest that method(is there any such method)...??
Replies To: Coloring Characters
#2
Re: Coloring Characters
Posted 15 August 2012 - 11:28 AM
You will want to look at using a JEditorPane and HTML instead of a JTextArea.
#3
Re: Coloring Characters
Posted 15 August 2012 - 12:21 PM
There is a possibility to extend JTextArea and override the paintComponent method. You would then paint a rectangle on top of the existing paint. The rectangle should be painted with a transparent color.
It would require some serious calculations to find the correct place to draw the rectangle! FontMetrics could be used, but I think it will be very hard going this way.
It would require some serious calculations to find the correct place to draw the rectangle! FontMetrics could be used, but I think it will be very hard going this way.
#4
Re: Coloring Characters
Posted 15 August 2012 - 12:32 PM
Wait there must be an easier way.....
You say it is a Typing Test program, so the JTextArea must be read only. This means you can set the selection and selectioncolor. If you selected the word with yellow, then it would be highlighted.
Example:
Then you will just have to make sure the user cant select text themselves
Edit: You could also use the Highlighter object to highlight text. It could be useful if you want to highlight more than one place.
You will still have the problem with the user highlighting parts himself.
You say it is a Typing Test program, so the JTextArea must be read only. This means you can set the selection and selectioncolor. If you selected the word with yellow, then it would be highlighted.
Example:
private class MyTextArea extends JTextArea {
public MyTextArea() {
super("Test text for testing", 1, 1);
setEditable(false);
setSelectionColor(Color.YELLOW);
highlightPosition(0, 5);
}
private void highlightPosition(int start, int end) {
setSelectionstart(start);
setSelectionend(end);
}
}
Then you will just have to make sure the user cant select text themselves
Edit: You could also use the Highlighter object to highlight text. It could be useful if you want to highlight more than one place.
You will still have the problem with the user highlighting parts himself.
This post has been edited by CasiOo: 15 August 2012 - 12:47 PM
#5
Re: Coloring Characters
Posted 15 August 2012 - 12:52 PM
This should do it using the highlight technique.
The user selection could be prevented by making the JTextArea not focusable
The user selection could be prevented by making the JTextArea not focusable
private class MyTextArea extends JTextArea {
private Object highlightTag;
public MyTextArea() {
super("Test text for testing long long long long long long long long long", 1, 1);
setEditable(false);
setSelectionColor(Color.YELLOW);
setLineWrap(true);
setWrapStyleWord(true);
setFocusable(false);
try {
highlightTag = getHighlighter().addHighlight(0, 0, DefaultHighlighter.DefaultPainter);
}
catch (BadLocationException e) {
}
highlightPosition(40, 50);
}
private void highlightPosition(int start, int end) {
try {
getHighlighter().changeHighlight(highlightTag, start, end);
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
}
#6
Re: Coloring Characters
Posted 16 August 2012 - 10:20 AM
@Casio : I have made an example program against yer guidance....but when I click inside or anywhere outside the JTextField component, the color from over the text disappears. What shall I do to retain the color...?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextA extends JFrame
{
TextA()
{
setSize(1250,740) ;
setTitle("JTextArea") ;
GridBagConstraints gbc = new GridBagConstraints() ;
this.setLayout(new GridBagLayout()) ;
JTextArea area = new JTextArea(8,100) ;
String str = "My heart goes zoooooooooooooommmmmm" ;
area.setText(str) ;
JScrollPane pane = new JScrollPane(area) ;
area.setEditable(false) ;
area.setSelectionstart(6) ;
area.setSelectionend(12) ;
area.setSelectionColor(Color.YELLOW) ;
gbc.gridx = 0 ; gbc.gridy = 0 ;
this.add(pane, gbc) ;
}
public static void main(String[] args)
{
new TextA().setVisible(true) ;
}
}
#7
Re: Coloring Characters
Posted 16 August 2012 - 11:44 AM
I did set the area to be not focusable.
When the JTextArea is not focusable, you can't change it's selection, BUT if you do it using highlights (as I showed above) then you will be able to do it
When the JTextArea is not focusable, you can't change it's selection, BUT if you do it using highlights (as I showed above) then you will be able to do it
#8
Re: Coloring Characters
Posted 17 August 2012 - 09:53 AM
@CasiOo : but when I set area.setFocusable(false) ; then it doesn't show any selection.....
It's not working....you can check it out...
It's not working....you can check it out...
#9
Re: Coloring Characters
Posted 17 August 2012 - 09:57 AM
Yes it does if you are using highlights as I said in my previous post.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|