Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

JTextComponent and BoldAction??? What is the wrong in the fallowing code snippet?

Thread view: 
eeyimaya - 24 May 2004 11:28 GMT
Hello,

I just want to make the selected text in JTextComponent bold.
I give "font-bold" as the action and nothing occurs?
If I give "cut-to-clipboard" it works...
What is the wrong??

Thanks much...
Ertan.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class DeneFrame extends JFrame
{
private JTextComponent jc;
private Hashtable commands;

public DeneFrame()
{
 getContentPane().setLayout(null);
 jc = new JTextArea();

 getContentPane().add(jc);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 commands = new Hashtable();
 Action[] actions = getActions();

 for (int i = 0; i < actions.length; i++)
 {
  Action a = actions[i];
  commands.put(a.getValue(Action.NAME), a);
 }

 JScrollPane scroller = new JScrollPane();
 JViewport port = scroller.getViewport();
 port.add(jc);

 JPanel panel = new JPanel();
 panel.setLayout(new BorderLayout());
 panel.add("Center", scroller);
 panel.setSize(300,50);
 panel.setLocation(100,100);
 getContentPane().add("Center", panel);

 JButton jb = new JButton("bold yap");
 Action a = getAction("font-bold");
 //Action a = getAction("cut-to-clipboard");//if this line uncommented and
                                            //above line is commented then
                                            //it works...i.e cuts the selected
                                            //text
 if (a!= null)
 {
  jb.setActionCommand("font-bold");
  jb.addActionListener(a);
 }
 else
  jb.setEnabled(false);

 jb.setSize(100,50);
 jb.setLocation(0,0);
 getContentPane().add(jb);

 pack();
 setSize(800,600);
 show();
}

public Action[] getActions() {
  Action[] defaultActions = {
   new StyledEditorKit.BoldAction(),
  };
  Action[] a = TextAction.augmentList(jc.getActions(), defaultActions);
 return a;
 }

protected Action getAction(String cmd) {
 return (Action) commands.get(cmd);
 }

public static void main(String[] args) {
 new DeneFrame();
}
}
Christian Kaufhold - 24 May 2004 12:14 GMT
> I just want to make the selected text in JTextComponent bold.
> I give "font-bold" as the action and nothing occurs?
> If I give "cut-to-clipboard" it works...
> What is the wrong??

Using JTextArea.

Christian
Anthony - 24 May 2004 16:42 GMT
You are using a JTextArea.  Text Area is a "plain" text component that
requires all of the text to be the same font.  Cut and Paste features
will work fine.

To allow for fone changes use a JTextPane as the Text Component
change the following line:

jc = new JTextArea();

to

jc = new JTextPane();

Good Luck!

A

> Hello,
>
[quoted text clipped - 84 lines]
>  }
> }
Anthony - 24 May 2004 16:46 GMT
You need to use a JTextPane instead of a JTextArea.  JTextAreas are
"plain" text, which means that although it can display text in any
font, all of the text is in the same font.  Cut and Paste functions
will work with all JTextComponents (including JTextArea)

just change the line

jc = new JTextArea();

to

jc = new JTextPane();

A.
eeyimaya - 25 May 2004 06:56 GMT
THANKS TO ALL REPLIES...

> You need to use a JTextPane instead of a JTextArea.  JTextAreas are
> "plain" text, which means that although it can display text in any
[quoted text clipped - 10 lines]
>
> A.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.