[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding smiles and different fonts to a JTextArea

Posted on 2006-06-04
41
Medium Priority
?
934 Views
Last Modified: 2013-12-03
Im writting a chat client, and i wonder if its possible to add smiles in a JTextArea.
Also I wonder if its possible to alterate fonts like:

[b]<user1>[/b]text

where text between [b] its bolded while rest its normal.

Im asking this because I found out that setFont changes the font to all text in the JTextArea
0
Comment
Question by:brightwood
  • 12
  • 11
  • 5
  • +3
38 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 400 total points
ID: 16826673
Use JEditorPane instead of JTextArea. It supprts html 3.2. See about it here:

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JEditorPane.html
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16826674
not JTextArea but JtextPane is capable of showing images in it
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16826675
aah yes JEditorPane
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:hoomanv
ID: 16826677
both of them
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 400 total points
ID: 16826682
0
 

Author Comment

by:brightwood
ID: 16826732
Well, whats better between this two, for adding smiles (that are just some imgs i guess) and different fonts and colors ?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16826750
JTextPane is a subclass of JEditorPane

from the link I gave you:
you can embed images and components in a text pane. You can embed images in an editor pane, too, but only by including the images in an HTML or RTF file.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 400 total points
ID: 16826859
Have a look at all the samples here, pick up the one you need:

http://www.javaalmanac.com/egs/javax.swing.text/pkg.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16827030
Try


final String SMILEY = "\u263a";

(but you'll need a font that supports that character)
0
 

Author Comment

by:brightwood
ID: 16828574
Lets say I have this string

String test = "<user> I like it wild :)";

How can I make the "<user>" bold, replace ":)" with an image, and append it to a JEditorPane

Basically make "< >" and whats between them bold, and replace all things like ":)", ":D", ":p", etc with images.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 16828659
String test = "<b>user</b> I like it wild \u263a";

You need to set the JEditorPane

ep.setContentType("text/html");
ep.setEditable(false);
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16829087
try something like:

s = s.replaceAll(":D", "<img src='smiley.gif'/>");
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16830370
Keep the smiley-text to image-name mapping in something like a properties-file and load it on start-up. This way, you will know which smiley is to be replaced with which image, e.g.,:

:-) = smiley1.gif
:) = smiley1.gif
:-( = smiley2.gif
:( = smiley2.gif

so on and so forth.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16830377
0
 

Author Comment

by:brightwood
ID: 16832849
Well I did try:
test = test.replaceAll(":p", "<img src='C:\\Documents and Settings\\raven\\Desktop\\FrameDemo\\src\\framedemo\\10.gif'</img>");

I get in EditorPane the image thing, but the img isn't loaded from the patch I specify. And the path its 100% correct
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16832883
correct is
"<img src='C:\\\\Documents and Settings\\\\raven\\\\Desktop\\\\FrameDemo\\\\src\\\\framedemo\\\\10.gif'</img>"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16833299
Try

test = test.replaceAll(":p", "<img src='file:/C:/Documents and Settings/raven/Desktop/FrameDemo/src/framedemo/10.gif'></img>");
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16833513
Its taken care of if you follow the property-file approach.
0
 

Author Comment

by:brightwood
ID: 16836221
Ok ive decided to use JTextPane instead. Ive loaded the smiles into Styles and stuff and its working, but ive got a problem parsing the String.

Lets say my string looks like
String text = "hey:) you rock:D.really:p"

How can I split it in parts like:
"hey"
":)"
" you rock"
":D"
".really"
":p"
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16836286
an arbitrary approach :-/

search for :) and list all the indices
search for :D and list the indices
search for :p and list the indices

combine all these lists in a sorted order
.now you have all the indices where a smilie is there

start substringing
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16836304
I think regular expressions is a better way to achieve this
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16836602
String str = "hey:) you rock:D.really:p";
Pattern p = Pattern.compile("(:\\))|(:D)|(:p)");
Matcher m = p.matcher(str);
int i = 0;
while(m.find()) {
      System.out.println(str.substring(i, m.start()));
      System.out.println(str.substring(m.start(), m.end()));
      i = m.end();
}
System.out.println(str.substring(i, str.length()));
0
 

Author Comment

by:brightwood
ID: 16837834
I load .gif's like this

        s = doc.addStyle(":(", regular);
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
        ImageIcon smile2Icon = createImageIcon("images/2.gif",
                                            ":(");
        if (smile1Icon != null) {
            StyleConstants.setIcon(s, smile2Icon);
        }

They are displayed, and animated, but image its a bit messed up when it animates. It is a known problem or something ?

And one last thing. i develop a chat program. is there any way I could make the main window title blink when you receive a new message (and window don't have the focus). Exactly like it happends in yahoo messenger and msn.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16837885
You can add a Swing Timer to your frame as an instance variable and start it to make the title flash:


      timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                  if ("".equals.getTitle()) {
                        frame.setTitle(originalTitle):
                  } else {
                        frame.setTitle(""):
                  }
            }
      });
0
 

Author Comment

by:brightwood
ID: 16837964
Nope, changing the title don't make it flash.

by the way, I want to make it flash down in the taskbar.
0
 

Author Comment

by:brightwood
ID: 16837989
Or something like

(if frame dont have focus) {
     change title bar color
}

and a ActionListener that makes title bar color default when the frame gets focus
0
 
LVL 92

Expert Comment

by:objects
ID: 16838007
0
 

Author Comment

by:brightwood
ID: 16838043
Umm there is no way I can do this with just some code ?
0
 
LVL 92

Expert Comment

by:objects
ID: 16838151
not easily, you can try changing the frame icon or giving it focus to trigger the flashing.
otherwise you'll need to use jni
0
 

Author Comment

by:brightwood
ID: 16838298
Can you show me how to do that please?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16840703
java 1.6 supports system tray
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/mustang/systemtray/

also you cant change the frame's titlebar color thru java
look at this: http://www-128.ibm.com/developerworks/java/library/j-iframe/
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16840738
FlashWindowEx is the Win32 API call that controls the flashing of the taskbar buttons or window title bar
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/flashwindowex.asp

you can write a JNI to have this functionality in your chat app
0
 

Author Comment

by:brightwood
ID: 16846476
Think ill just forget about the title bar at this time until 1.6 is out.

But I just run into another issue. I develop my program using NetBeans. \

The structure is like this: http://img198.imageshack.us/img198/6767/desktop4hf.jpg

When I run the program from inside NetBeans it runs perfect, but when I build it and then I run it from the dist folder, I get a error that image files cannot be found.

My createImageIcon method look like this:

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = Client.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 16847642
you nedd to make the images available in your classpath (same as you do for classes)
0
 

Author Comment

by:brightwood
ID: 16847712
Sorry I don't get you.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16847834
Open 'Project Properties' window (right click on project name in Projects tab) and follow these instructions:
  In Categories view select Run
  In VM Options field append or enter this line '-classpath images'
0
 
LVL 92

Expert Comment

by:objects
ID: 16849904
> but when I build it and then I run it from the dist folder

your build also needs to copy the images to the dist folder
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16864101
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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