I’m trying to make a text editor with a JTextPane, but i’m having trouble with setting the selected texts color. Here is the best could come up with (but, obviously, not working):
JMenuItem button = new JMenuItem("Set Color");
toolbar.add(button);
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
Color c = JColorChooser.showDialog(frame,"Choose a color", getBackground());
textPane.getSelectedText().StyledEditorKit.ForegroundAction("color",c);
}
});
Any suggestions on how to get that to work? Or a better method of doing it?
Thanks
getSelectedText()just returns a normal string containing the selected text; you cannot use it to modify the attributes of the text.I would start by using
SimpleAttributeSetandStyleConstantsto generate the colour attribute, then apply it to the selected portion of your text: