Multiple foreground colors in AWT TextArea?

R

Richard Maher

Hi,

I would like to be able to specify a different color for each line in an AWT
TextArea. As this doesn't seem possible, can someone please advise me of the
easiest way of achieving this sort of with standard AWT widgets?

I started to read up on 2D graphics and TextAttributes and AttributedStrings
but it all began to look complicated :-(

I have a console (TextArea a present but can change) that's logging status
messages and I'd like to have some green/red etc.

Cheers Richard Maher
 
Q

Qu0ll

Richard Maher said:
Hi,

I would like to be able to specify a different color for each line in an
AWT
TextArea. As this doesn't seem possible, can someone please advise me of
the
easiest way of achieving this sort of with standard AWT widgets?

I started to read up on 2D graphics and TextAttributes and
AttributedStrings
but it all began to look complicated :-(

I have a console (TextArea a present but can change) that's logging status
messages and I'd like to have some green/red etc.

Does it have to be AWT? There are Swing components which permit colouring.

If you are limited to AWT then you may have to use a Canvas and handle the
coloured text rendering explicitly.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
N

Nigel Wade

Richard said:
Hi,

I would like to be able to specify a different color for each line in an AWT
TextArea. As this doesn't seem possible, can someone please advise me of the
easiest way of achieving this sort of with standard AWT widgets?

I started to read up on 2D graphics and TextAttributes and AttributedStrings
but it all began to look complicated :-(

I have a console (TextArea a present but can change) that's logging status
messages and I'd like to have some green/red etc.

If you are not restricted to AWT you could do it in Swing with a JTextPane and a
StyledDocument. I've just been playing with my own StyledDocumentAppender (for
log4j), using different styles for the different log levels.
 
R

Richard Maher

Hi Qu0ll,

Qu0ll said:
Does it have to be AWT? There are Swing components which permit colouring.

If you are limited to AWT then you may have to use a Canvas and handle the
coloured text rendering explicitly.

Stuck with AWT at the moment as it's all I know(ish). Although the interface
is vary basic: -

/**
* Copyright Tier3 (c) Software. All rights reserved.
*
* Display "Welcome" dialogue after successful User authorization.
*
* @author Richard Maher
* @version 1.0
*
*/

package tier3Client;

interface Tier3UserDialog {

boolean doCredentials();

String getUsername();

String getPassword();

boolean isWelcomeRequired();

void doWelcome(byte[] t3IdBuf);

void doConsole(byte[] t3IdBuf,
Tier3Logoff sourceSession,
String codeHost,
int portNum);

void updateRcvd(int bytes);

void updateSent(int bytes);

void showRefCnt(int pages);

void closeConsole();
}

And there is an Applet parameter that let's you specify a GUI Toolkit (Of
which there is only AWT at the moment :) See below. *The rendezvous method
for async send completion* functionality shaping up to be just too sexy for
words!
-Qu0ll (Rare, not extinct)

Regards Richard Maher
/**
* Copyright (c) Richard Maher. All rights reserved.
*
*/

function Tier3Client(application,
codeBase,
port,
maxBuf,
hostCharSet,
sslReqd,
guiToolkit,
idleTimeout,
verbosity)
{
if (arguments.length < 4) {
throw new Error("Insufficient arguments for Tier3Client");
}

this.facPrefix = "T3$";
this.application = application;
this.codeBase = codeBase;
this.port = port;
this.maxBuf = maxBuf;

if (hostCharSet == undefined) {
this.hostCharSet = "ISO-8859-1";
} else {
this.hostCharSet = hostCharSet;
}
if (sslReqd == undefined) {
this.sslReqd = "N";
} else {
this.sslReqd = sslReqd;
}
if (guiToolkit == undefined) {
this.guiToolkit = Tier3Client.GUIAWT;
} else {
this.guiToolkit = guiToolkit;
}
if (idleTimeout == undefined) {
this.idleTimeout = 0;
} else {
this.idleTimeout = idleTimeout;
}
if (verbosity == undefined) {
this.verbosity = Tier3Client.FATAL;
} else {
this.verbosity = verbosity;
}

var appletId = "Tier3__" + application + "_Applet";

try {
var idTaken = document.getElementById(appletId);
}
catch (err) {};

if (idTaken != null) {
throw new Error("Tier3 Client already registered for " +
this.application);
return;
}

var archiveName = "tier3Client.jar";
var className = "tier3Client/Tier3Application";

var appletParams = [{"name":"archive",
"value":archiveName },
{"name":"codebase",
"value":codeBase },
{"name":"code",
"value":className },
{"name":"mayscript",
lue":"true" },
{"name":"scriptable",
ue":"true" },
{"name":"codebase_lookup",
"false" },
{"name":"APPLICATION",
"value":application },
{"name":"PORT",
"value":port },
{"name":"MAXBUF",
"value":maxBuf },
{"name":"HOSTCHARSET",
"value":this.hostCharSet},
{"name":"SSLREQD",
"value":this.sslReqd },
{"name":"GUITOOLKIT",
"value":this.guiToolkit },
{"name":"IDLETIMEOUT",
"value":this.idleTimeout},
{"name":"VERBOSITY",
"value":this.verbosity }];
var startParam = 0;

var objectTag = "<object classid=";

if (/Internet Explorer/.test(navigator.appName)) {
objectTag = objectTag +
'"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
} else {
objectTag = objectTag +
'"java:' + className + '.class" type="application/x-java-applet
" ' +
'archive="' + codeBase + archiveName + '"';
startParam = 1;
}

objectTag = objectTag + ' width= "0" height= "0" id="' + appletId +
'">';

for (i=startParam; i<appletParams.length; i++){
objectTag = objectTag + '<param name ="' + appletParams.name +
'" ' +
'value ="' + appletParams.value +
'">';
}

objectTag = objectTag + "</object>";

var appletDiv = document.createElement("div");
appletDiv.innerHTML = objectTag;

try {
document.body.appendChild(appletDiv);
this.chan = document.getElementById(appletId);
}
catch(err) {
alert("Tier3 unable to load applet for " + this.application +
": -\n" + err.description);
this.chan = null;
};

if (this.chan == null) {
throw new Error("Tier3 was unable to initialize the applet for " +
this.application);
} else {
try {
if (!this.chan.isAuthorized()) {
throw new Error("Tier3 User authentication unsuccessful for
" + this.application);
}
}
catch(err) {
this.chan = null;
throw new Error("Tier3 unable to load applet for " +
this.application);
}
}

Tier3Client.applications[this.application] = this;

return this;
}

Tier3Client.MAJVERS = 1;
Tier3Client.MINVERS = 0;
Tier3Client.GUIAWT = 1;
Tier3Client.FATAL = 0;

Tier3Client.errorPage = "Tier3Error.html";
Tier3Client.logoffPage = "Tier3Logoff.html";

Tier3Client.launder =
function(jsobject) {
return jsobject;
};

Tier3Client.prototype = {

send:
function(msgBody, callback, async)
{
if (arguments.length < 2) {
throw new Error("Insufficient arguments for send(msgBody,
callback)");
}

if (typeof callback != "function") {
throw new Error("The 'callback' parameter must be a
function");
}

var noWait = true;
if (arguments.length > 2) {
if (typeof async != "boolean") {
throw new Error("The 'async' parameter must be a
boolean");
}
noWait = async;
}

var chan = this.chan;
var callbackArgs = new Array();
var responseCnt = 0;
var i = 0;

var msgCandidate =
{
msgSlotId : -1,
msgSeqNum : -1,
chan : chan,
callback : callback,
callbackArgs : callbackArgs,

dispatcher :
function(responseMsg,
msgSlotId,
msgSeqNum)
{
this.responseCnt++;
this.msgSlotId = msgSlotId;
this.msgSeqNum = msgSeqNum;
callbackArgs[0] = responseMsg;

try {
callback.apply(this, callbackArgs);
}
catch (err) {
throw new Error("Error calling callback
routine: -\n" + err.description);
}
},

getMsgSeqNum :
function() {
return this.msgSeqNum;
},

getResponseCnt:
function() {
return this.responseCnt;
},

rendezvous :
function() {
return chan.rendezvous();
}

};

for (i=3; i<arguments.length; i++) {
callbackArgs[i - 2] = arguments;
}

return chan.send(msgCandidate, msgBody, noWait);
}

};

Tier3Client.applications = {};
 
R

Richard Maher

Hi Nigel,

Nigel Wade said:
If you are not restricted to AWT you could do it in Swing with a JTextPane and a
StyledDocument. I've just been playing with my own StyledDocumentAppender (for
log4j), using different styles for the different log levels.
Oh well looks like it's monochrome for me and AWT :-(

Good to know JTextPane can do it. (Pretty useful thing to do isn't it?) I'll
try and read up on that for the future. (But I'll be skipping the log4j
bloatware)

Cheers Richard Maher
 
A

Arne Vajhøj

Richard said:
Stuck with AWT at the moment as it's all I know(ish).

Swing is not that much more difficult than AWT.

Some things are even easier.

Arne
 
R

Richard Maher

Hi Lew,

Lew said:
Harsh words for something so utile.

Perhaps. (Coming from someone who's just got an AccessControlException when
trying to setLevel(Level) on a Logger from an unsigned Applet :-( ) Makes
sense I suppose if the named loggers and there levels are static.

I dunno; maybe the technology plateaued at System.out.println() and the rest
has been a functional-creep to nowhere?
Regards Richard Maher
 
L

Lew

Lew wrote ...
Richard said:
Perhaps. (Coming from someone who's just got an AccessControlException when
trying to setLevel(Level) on a Logger from an unsigned Applet :-( )

Sounds like an applet problem rather than a logger one.
Makes sense I suppose if the named loggers and there levels are static.

I don't understand.
I dunno; maybe the technology plateaued at System.out.println() and the rest
has been a functional-creep to nowhere?

Nope, that's not it.
 
R

Richard Maher

Hi Lew,

Lew said:
Lew wrote ...



Sounds like an applet problem rather than a logger one.

Do you have and example somewhere of an unsigned applet setting the Level on
a named java.util.logging.Logger that doesn't result in an
AccessControlException?
I don't understand.

I was trying to understand the rationale behind the apparent restriction.
Perhaps preventing me from masking out other logs? But if it's a bug in my
code rather than a sandbox restriction then I'm happy to change!
Nope, that's not it.

Regards Richard Maher
 
R

Roedy Green

I would like to be able to specify a different color for each line in an AWT
TextArea. As this doesn't seem possible, can someone please advise me of the
easiest way of achieving this sort of with standard AWT widgets?

My PrettyCanvas class does that.

See
https://wush.net/websvn/mindprod/fi...path=/com/mindprod/jdisplay/PrettyCanvas.java

You can download the complete source package at
http://mindprod.com/products1.html#JDISPLAY

The basic idea is you feed it tokens which have font, colour, and text
fields. It renders them on an AWT Canvas using Graphics.drawString.

The other way to do it is to wrap your strings in HTML markup and feed
them to a JTextPane or a JTextEditorPane.

see http://mindprod.com/jgloss/jtextpane.html
http://mindprod.com/jgloss/jeditorpane.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"You can have quality software, or you can have pointer arithmetic; but you cannot have both at the same time."
~ Bertrand Meyer (born: 1950 age: 59) 1989, creator of design by contract and the Eiffel language.
 
L

Lew

Richard said:
when trying to setLevel(Level) on a Logger from an unsigned Applet :-( )

Lew wrote
Richard said:
Do you have and example somewhere of an unsigned applet setting the Level on
a named java.util.logging.Logger that doesn't result in an
AccessControlException?

No, but I've seen lots of examples of both java.util.logging Logger and log4j
Logger (which is where we started when you mentioned "log4j bloatware" - not
sure why we're talking the other library now) that run just fine outside of
applets. Ergo one concludes that the problem, dear Brutus, lies not in our
logging libraries but in our applets.
 
Q

Qu0ll

Lew said:
Lew wrote



No, but I've seen lots of examples of both java.util.logging Logger and
log4j Logger (which is where we started when you mentioned "log4j
bloatware" - not sure why we're talking the other library now) that run
just fine outside of applets. Ergo one concludes that the problem, dear
Brutus, lies not in our logging libraries but in our applets.

I gave up trying to get either java.util.logging or log4j to work in an
applet context. I couldn't see a way around the exceptions when trying to
do anything useful. In the end I wrote a simple logging API that suited my
needs.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
R

Richard Maher

Hi Qu0ll,

Qu0ll said:
I gave up trying to get either java.util.logging or log4j to work in an
applet context. I couldn't see a way around the exceptions when trying to
do anything useful. In the end I wrote a simple logging API that suited my
needs.

I had a minimal POC running with log4j but the size of the JAR coupled with
it still wanting more classes from the web-server put me off. (I have since
found the codebase_lookup applet parameter that looks like it could've
helped)

Anyway Logger seems to be doing what is says on the tin as long as it's
anonymous (and I haven't tried any sort of custome formatting yet)
Cheers Richard Maher
 
A

Arne Vajhøj

Qu0ll said:
I gave up trying to get either java.util.logging or log4j to work in an
applet context. I couldn't see a way around the exceptions when trying
to do anything useful. In the end I wrote a simple logging API that
suited my needs.

log4j works in applet context.

It is only jul that has some security checks that disallow most things.

Arne
 
A

Arne Vajhøj

Lew said:
Lew wrote



No, but I've seen lots of examples of both java.util.logging Logger and
log4j Logger (which is where we started when you mentioned "log4j
bloatware" - not sure why we're talking the other library now) that run
just fine outside of applets. Ergo one concludes that the problem, dear
Brutus, lies not in our logging libraries but in our applets.

In the applet "strictware" !

:)

Arne
 
A

Arne Vajhøj

Richard said:
Do you have and example somewhere of an unsigned applet setting the Level on
a named java.util.logging.Logger that doesn't result in an
AccessControlException?

You can't do that from an applet with jul.

But it is not a problem for log4j.
I was trying to understand the rationale behind the apparent restriction.
Perhaps preventing me from masking out other logs? But if it's a bug in my
code rather than a sandbox restriction then I'm happy to change!

SUN put an explicit security check in the jul code.

Arne
 
A

Arne Vajhøj

Richard said:
Perhaps. (Coming from someone who's just got an AccessControlException when
trying to setLevel(Level) on a Logger from an unsigned Applet :-( ) Makes
sense I suppose if the named loggers and there levels are static.


The fact that jul has access problems in applets does not
make log4j bloatware.

Arne
 
Q

Qu0ll

Arne Vajhøj said:
log4j works in applet context.

It is only jul that has some security checks that disallow most things.

Yes, you're probably right. If I remember I didn't use log4j in my applets
because of the extra JAR that needed to be downloaded and I was trying to
keep my applet as tiny as possible. It's a pity you can't use jul in an
applet.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
A

Arne Vajhøj

Qu0ll said:
Yes, you're probably right. If I remember I didn't use log4j in my
applets because of the extra JAR that needed to be downloaded and I was
trying to keep my applet as tiny as possible. It's a pity you can't use
jul in an applet.

You can use jul in an applet, but only with anonymous logger
and a few other restrictions.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top