View Full Version : Calling Java Gurus: Java AWT based Rich Text/HTML display widget needed
echarcha
October 13th, 2003, 11:51 AM
All Java gurus,
I need your help.
Dont ask me why or how or what about our product using JDK1.1.8 for compilation. We are bound by some business decisions and rules which forces us to use JDK1.1.8 which implies that we cannot use the rich JFC/SWING GUI widgets.
Now, my problem is this -
I need to show some HTML text (with simple tags like Bold, Underline, Font color, etc) in a screen in our application. Its not an applet and so we cannot launch a separate browser window. We need to show this rich formatted/HTMl text within our application.
Does any of you know any websites or have any sample code whereby I can implement a small panel/text area which can show this formatted text.
Please help as soon as you can.
IA
October 13th, 2003, 11:59 AM
Sunit,
Search on http://groups.google.com/groups?hl=en&group=comp.lang.java
I am sure you will get the solution for your problem.
echarcha
October 13th, 2003, 12:15 PM
Originally posted by IA
Sunit,
Search on http://groups.google.com/groups?hl=en&group=comp.lang.java
I am sure you will get the solution for your problem.
Idrees bhai ,
Thanks but I did not find what I was looking for :( Sheeeet.. working with old compilers to support some folks who might not have upgraded to latest java plugin really sucks.
IA
October 13th, 2003, 12:30 PM
Sunit,
If it helps
You cann't display multi-color text in TextArea. You can only display it
in JTextPane. JTextPane is port of swing. The code is something like:
JTextPane textPane = new JTextPane();
textPane.setText(aString); // aString is what you want to display;
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet att = new SimpleAttributeSet();
att.addAttribute("$fg", Color.red);
doc.setCharacterAttributes(position1, width1, att, true);
att.removeAttribute("$fg");
att.addAttribute("$fg", Color.blue);
doc.setCharacterAttributes(position2, width2, att, true);
// position# is position in the string, width# is the length of string
// you want to that color.
read more on Source (http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=6b57ku%24lr7%40lotho.delphi.com&rnum=2&prev=/groups%3Fq%3DRich%2BText%2BDisplay%2Bin%2BAWT%2Bgroup:comp.lang.java.*%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.java.*%26selm%3D6b57ku%2524lr7%2540lotho.delphi.com%26rnum%3D2)
echarcha
October 13th, 2003, 12:56 PM
I cannot use any JFC/SWING classes :( :(
IA
October 13th, 2003, 01:05 PM
bhai mere AWT TextArea dose not support the features you are trying to provide so the only solution you have now is either to change your client to JFC/Swing or keep researching on the technique.
echarcha
October 13th, 2003, 01:06 PM
Originally posted by IA
bhai mere AWT TextArea dose not support the features you are trying to provide.
Achha text area forget karo. Anything..I guess a Canvas can help..
IA
October 13th, 2003, 01:08 PM
Yes if you use a canvas then it is possible but then you have to do all the drawing stuff. let me know if you need the code for that i will PM it to you.
echarcha
October 13th, 2003, 01:12 PM
Originally posted by IA
Yes if you use a canvas then it is possible but then you have to do all the drawing stuff. let me know if you need the code for that i will PM it to you.
IA,
overriding the piant() method is the solution. But the main part is to decipher the simple HTML and then do the painting. If you have ideas about this then it would help a lot.
IA
October 13th, 2003, 01:16 PM
As i told you in PM i have not worked with AWT for a quite while so i need to go back to the books/forums. Let me know if you need this very quick.
echarcha
October 13th, 2003, 01:30 PM
Originally posted by IA
As i told you in PM i have not worked with AWT for a quite while so i need to go back to the books/forums. Let me know if you need this very quick.
Bhai agar aap ke paas thoda time ho to yeh kar dalo.. I will need it within next 10 days for sure.
IA
October 13th, 2003, 01:39 PM
Try this and let me know. change the code as per your requirement.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class SampleFrame extends Frame {
SampleFrame (String title) {
super (title);
MyWindowAdapter adapter = new MyWindowAdapter (this);
addWindowListener (adapter);
}
public void paint (Graphics g) {
Color c1 = new Color (255, 100, 100);
Color c2 = new Color (100, 255, 100);
Color c3 = new Color (100, 100, 255);
g.setColor (Color.red);
g.drawString ("This is a test", 10, 40);
}
}
class MyWindowAdapter extends WindowAdapter {
SampleFrame frame;
public MyWindowAdapter (SampleFrame frame) {
this.frame = frame;
}
public void windowClosing (WindowEvent e) {
frame.setVisible (false);
}
}
public class AppletFrame extends Applet {
Frame f;
public void init () {
f = new SampleFrame ("A Frame Window");
f.setSize (300, 200);
f.setVisible (true);
f.show ();
}
public void start () {
f.setVisible (true);
}
public void stop () {
f.setVisible (false);
}
public void paint (Graphics g) {
Color c1 = new Color (255, 100, 100);
Color c2 = new Color (100, 255, 100);
Color c3 = new Color (100, 100, 255);
g.setColor (Color.red);
g.drawString ("This is an applet", 10, 20);
g.drawLine (20, 150, 400, 400);
}
}
echarcha
October 13th, 2003, 01:41 PM
Thanks for your code.
Actually the code you posted is the easy part.
Deciphering that <B>Bold</B> means that the word Bold need to be in Bold and then changing the paint method is what I need help with.
IA
October 13th, 2003, 01:43 PM
check your pm.
echarcha
October 13th, 2003, 03:04 PM
I am sorry that I did not tell you the problem in details.
I need a special area to display text just like a HTML browser does. We dont have to show any graphic images but just text which is either bold or colored.
Now part one of the problem -
If a text file is present with simple HTML tags in it, we need to interpret this text file and make the appropriate words Bold or Italic etc.
So the first part deals with interpreting the text file which is nothing but HTML formatted text
Now part two of the problem -
Once we know that any word with <b> and </b> around it means bold, then we can call an appropriate paint method to draw a word which is highlighted/bold.
So second part of the solution would be to draw the text as per the HTML tags.
In short we need to build a special panel or Canvas which mimics the rendering capabilities of a browser.
IA
October 13th, 2003, 03:07 PM
did you check my last 2 pms?
echarcha
October 13th, 2003, 03:46 PM
Originally posted by IA
did you check my last 2 pms?
I did.. I will call you.
Just wanted you to know that part one is quite important because we have to interpret the HTML which comes from a file. The file may change with different content. So we cannot predict that the same HTML tags will be at the same locations.
Anyway, lets talk about it either today evening (late) or tomorrow.
vBulletin® v3.7.2, Copyright ©2000-2013, Jelsoft Enterprises Ltd.