aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JColorChooser and StyledEditorKit foreground Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JColorChooser and StyledEditorKit foreground" Watch "JColorChooser and StyledEditorKit foreground" New topic
Author

JColorChooser and StyledEditorKit foreground

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i have an applet that has HTMLEditorPane (StyledEditor basically). i want to enable users to select color of the fonts from the ColorChooser.
now, colorchooser can be initiated by either,
1. JColorChooser.showDialog() or
2. JColorChooser.cretaeDialog()
my requirement wants,
1. a user pops up a color dialog and selects a color via "custom color" option in my menu
2. presses OK on the color dialog
3. then the selection in the HTML editor becomes of that color
the problem i found is,
1. how i can override OK button listener in such a manner that sets the foreground of the StyledEditor to the color lastest choosen?
this is a problem because- to change font color in Styled Editor we have to use StyledEditor.ForegroundAction and that accepts Color argument. in my application we have to use JColorChooser.createDialog() as i want to override the default OK button listener thing.
but when i use createDialog() i have to pass the listener into that which should be something like-
JColorChooser.createDialog(parentFrame, "Color Chooser", true, new StyledEditorKit.ForegroundAction("Color", myColorChooser.getColor(), null);
i have to do this each time the "custom color" option is choosen. (basically i have put this on MenuItem's action for "custom color" option).
but if i do this then upon poping up a color dialog the default color (Say it is C) gets selected and so the ForegroundAction() gets that color. now, even if we change a color (say C1) in the dialogbox and press OK it will change the color to be the previously selected color (that is C in this case), right?? now, if we repeat this procedure, next time the default color is C1 hence even if we change the color on color dialogbox to be say C2 and press OK the font color would be C1....so on and so forth...
so the bottom line- "i get the 'latest-1' color selection from the color dialog everytime instead 'latest'"

i hope you understand my problem. i have not posted questions to swing forum as i am not a great user of swing so far...
i have a solution to this- "override the StyledEditorKit.ForegroundAction class" and that will work but i wanted to know if there is any other way of doing this...
regards
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
all right,
here is a part of code where i do this custom color thing...

hope this helps.
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
other alternative would be- "if we can find a way to fire StyledEditorKit.ForegroundAction".
if we can do that then we i can use JColorChooser.showDialog() which returns me the latest color selection and then fire ForegroundAction with that color so that selection in the EditorPane becomes of that color...
regards
maulin
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Maulin,
In the code u pasted above - why r u creating a color chooser and making it visible. I think the foll. part should be remove -
cChooser = new JColorChooser(); cChooser.setVisible(true);

The code to show the color chooser dialog should be in action as it is in the next few lines. But i have ques re. those also. Why r u not getting the latest color selected by the user after the call to the setVisible(true); Also it is not at all necessary to provide a default action. In short why r u using create dialog method. why not 2 use showdialog method.
If i would have 2 code i would do the foll way :-
item = new JMenuItem("Custom Color");
subMenu.add(item);
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// first get the color at the current caret position
Color initialFG = StyleConstants.getForeground(textPane.getCharacterAttributes());

Color fgColor = JColorChooser.showDialog(
frame, // applet
"Foreground",// title
initialFG);// initial color

if(fgColor == null){
// user cancelled selection
return;
}

MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, fgColor);
textPane.setCharacterAttributes(mas, false);
}
});

NOTE : For simplicity, the above code doesn't consider i18n issues.
Hope this helps.


The best teams have no specialists, only general contributors with special skills
 
 
subject: JColorChooser and StyledEditorKit foreground