JTextPane: change the style of a text using a JMenu

Hi,

I have the following problem:

I use a JTextPane and a StyledDocument to insert styled text and icons into the JTextPane using the follwing code:

Create the styles:

/**

* Method to add Styles to a document

* @param doc the document to add the styles

*/

protectedvoid addStylesToDocument(StyledDocument doc)

{

//Initialize some styles.

Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

//define some standards

Style regular = doc.addStyle("regular", def);

StyleConstants.setFontFamily(def,"Arial");

StyleConstants.setFontSize(def, 14);

//for messages

s = doc.addStyle("message_out", regular);

StyleConstants.setBold(s,true);

StyleConstants.setForeground(s, Color.RED);

//for a Icon

s = doc.addStyle("smily_grin", regular);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

ImageIcon smily_grin = createImageIcon(PATH_SMILE,"*grin*");

if (smily_grin !=null)

StyleConstants.setIcon(s, smily_grin);

}

I can insert styled text and images using the following code:

//get the document

StyledDocument doc = textPane.getStyledDocument();

//for messages

doc.insertString(doc.getLength(),head,doc.getStyle("message_out"));

//for icons

doc.insertString(doc.getLength()," ", doc.getStyle("smily_grin"));

Now i want to be able to insert icons and change the style of the text using a JMenu or a JPopupMenu.

But how can i make this?

Best regards.

Michael

[2291 byte] By [heissma] at [2007-11-26 12:25:39]
# 1
Read the JTextPane API. Follow the "Using Text Components" link to the Swing tutorial which has a working example.
camickra at 2007-7-7 15:30:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

I know this tutorial, but i want to realize it as shown in the tutorial "Text Component Features" with action events. (Associating Text Actions with Menus and Buttons)

But i havent figured out how to insert an image, because the StyledEditor kit only has some predefined actions for modifying the style of a text.

Best regards,

Michael

heissma at 2007-7-7 15:30:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

Write your own Action by extend TextAction. Simple example:

class SelectAll extends TextAction

{

public SelectAll()

{

super("Select All");

}

public void actionPerformed(ActionEvent e)

{

JTextComponent component = getFocusedComponent();

component.selectAll();

}

}

camickra at 2007-7-7 15:30:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks for help, but i realized that i have no idea how my problem could be solved.

I made the following things:

1) Created a Class, refering to your example

2) Created a new Action object with my own class

3) Added the action event to my JTextPane

//add the custom actions

Object key = action.getValue(Action.NAME);

inputPane.getActionMap().put(key, action);

And now i simply do not know what to do :(

When i execute the code and klick the button i have associated with the action i get a NullPointerException because the component returned by getFocusedComponent in the actionPerformed method of my extended TextAction class is null.

Best regards,

Michael

heissma at 2007-7-7 15:30:32 > top of Java-index,Desktop,Core GUI APIs...
# 5

> I know this tutorial, but i want to realize it as shown in the tutorial "Text

> Component Features" with action events. (Associating Text Actions with Menus and Buttons)

Then create an Action and add the Action to a JMenuItem. Thats what the tutorial does.

I gave you a sample Action. Try getting my Action to work with a menu item. Once you understand the process, then write your own Action.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-7 15:30:32 > top of Java-index,Desktop,Core GUI APIs...