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.