8 Replies - 234 Views - Last Post: 17 August 2012 - 09:57 AM Rate Topic: -----

#1 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 305
  • Joined: 26-March 09

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)...??

This post has been edited by rishabhsharma: 15 August 2012 - 11:17 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Coloring Characters

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9027
  • View blog
  • Posts: 33,482
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

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:
	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

Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

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

	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();
			}
		}
	}


Was This Post Helpful? 1
  • +
  • -

#6 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 305
  • Joined: 26-March 09

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) ;
            }
     }

Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

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 :)
Was This Post Helpful? 0
  • +
  • -

#8 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 305
  • Joined: 26-March 09

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...
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1