Solved

Substitute for JTextPane in Java1

Posted on 2002-04-28
8
261 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
Comment Utility
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
Comment Utility
You can, of course, change public class StringCenterApplet extends JApplet to public class StringCenterApplet extends Applet
0
 

Author Comment

by:SteelFist
Comment Utility
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
Comment Utility
The principle would be exactly the same if you were to use TextArea. You would have to override its paint() method.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:SteelFist
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Modeling a class in java 5 31
recursion example 16 65
thymeleaf natural templating vs JSP 2 21
Java Jpanels and Jframe 8 18
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now