Solved

Substitute for JTextPane in Java1

Posted on 2002-04-28
8
273 Views
Last Modified: 2008-02-26
Hi.

I want to write an Applet to write text with different colors in it, i know how to do it with JTextPane, but the browser is supporting Java 1 only therefor does not display the JTextPane at all, is there any idea how to write lines with different colors in an applet? or something to replace the JTextPane to Java 1 object?
thanks
0
Comment
Question by:SteelFist
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 6976871
Essentially, it's necessary to override the applet's paint() method. The following code, although primarily intended to demonstrate centering a string in the applet, shows how to do this:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class StringCenterApplet extends JApplet {
  // maybe get this from an applet parameter
  String stringToCenter = "Testing centering of string";

  public void init() {
    try {
      this.setSize(new Dimension(400,300));
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void paint(Graphics g){
    Point where = CenterString.centerString(g,getSize(),stringToCenter);
    g.drawString(stringToCenter,(int)where.getX(),(int)where.getY());
    g.setColor(Color.RED);
    g.drawString(stringToCenter,(int)where.getX(),(int)(where.getY() + 30));
  }

  //encapsulate for re-use
  static class CenterString {
    public static Point centerString(Graphics g,Dimension d,String s){
      Point p = new Point();
      FontMetrics fm = g.getFontMetrics();
      double lineWidth = fm.stringWidth(s);
      double lineHeight = fm.getHeight();
      return new Point((int)(d.width - lineWidth) / 2,(int)(d.height - lineHeight) / 2);
    }
  }
}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
  <HEAD>
  </HEAD>
  <BODY>
    <applet
      code="StringCenterApplet.class"
      codebase="."
      width="200"
      height="200"
    >
    </applet>

  </BODY>
</HTML>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6976874
You can, of course, change public class StringCenterApplet extends JApplet to public class StringCenterApplet extends Applet
0
 

Author Comment

by:SteelFist
ID: 6977052
Thanks for your reply, but what you wrote using the drawString method, which just draws the string in the color you mantioned, i am looking for substitute for JTextPane, same as TextArea but with different colors.
Because i want to print a document or give people write a form there for example, if i want to print a java source code in the apple, and color several words in the document.

i know how to do it with JTextPane, but again, it's not showing in the applet.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 86

Expert Comment

by:CEHJ
ID: 6977490
The principle would be exactly the same if you were to use TextArea. You would have to override its paint() method.
0
 

Author Comment

by:SteelFist
ID: 6978034
Thanks for your reply, but what you wrote using the drawString method, which just draws the string in the color you mantioned, i am looking for substitute for JTextPane, same as TextArea but with different colors.
Because i want to print a document or give people write a form there for example, if i want to print a java source code in the apple, and color several words in the document.

i know how to do it with JTextPane, but again, it's not showing in the applet.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6978049
Are we getting this right - you want to create something like code syntax coloring using a TextArea in an applet < Java 1.2?
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7992339
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: CEHJ
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8048131
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question