Changing Font Family and Size thru JComboBox

Hi! I'm doing an HTML Editor that mimics the Microsoft Word interface (FontFamily and FontSize selection appears on a JComboBox) but having problems implementing it.

The StyledEditorKit.FontFamilyAction(...) works fine when implemented thru an ActionListener added to a JMenuItem or a JButton.

I'm having problems implementing the same action when you select items on the JComboBox. Here' part of my code:

String[] fontFaces = {"Arial","Courier New","Times New Roman","Verdana"};

JComboBox fontFace = new JComboBox(fontFaces);

fontFace.setToolTipText("Font Face");

fontFace.setSelectedIndex(2);

fontFace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox cb = (JComboBox)e.getSource();

//this should have a switch between list items and

//perform the corresponding action.

}

});

toolbar1.add(fontFace);

Hope someone can help! Thanks!

[1000 byte] By [nguba] at [2007-9-26 1:36:46]
# 1

Hi,

SimpleAttributeSet set=new SimpleAttributeSet();

StyledDocument doc=yourTextPane.getStyledDocument();

int start=yourTextPane.getSelectionStart();

String selected=yourTextPane.getSelectedText();

/*********/

fontFace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox cb = (JComboBox)e.getSource();

//what u wanted

if(cb.getSelectedIndex==0)

{

StyledConstants.setFontFamily(set,"Ariel");

try{

doc.setCharacterAttributes(start,selected.length(), set, false);} catch(Exception e){}

}

if(cb.getSelectedIndex==1)

{

try{

StyledConstants.setFontFamily(set,"Courier New");

doc.setCharacterAttributes(start,selected.length(), set, false);} catch(Exception e){}

}

-

-

-

-

}

});

Note:

This works if u have JTextPane as ur Editor;

Thanks

Sasivarnan

sasivarnan at 2007-6-29 2:22:43 > top of Java-index,Archived Forums,Swing...
# 2

Sasivarnan,

Thanks. Your code worked fine! Thanks for the help. This is what I did with my code...

String[] fontFaces = {"Arial","Courier New","Times New Roman","Verdana"};

JComboBox fontFace = new JComboBox(fontFaces);

fontFace.setToolTipText("Font Face");

fontFace.setSelectedIndex(2);

fontFace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

SimpleAttributeSet set = new SimpleAttributeSet();

int start = textPane.getSelectionStart();

String selected = textPane.getSelectedText();

JComboBox cb = (JComboBox)e.getSource();

try

{

StyleConstants.setFontFamily(set,(String)cb.getSelectedItem());

htmlDoc.setCharacterAttributes(start,selected.length(), set, false);

}

catch(Exception exp){}

}

});

toolbar1.add(fontFace);

Thanks again!

Nino

nguba at 2007-6-29 2:22:43 > top of Java-index,Archived Forums,Swing...
# 3

Hi again!

Maybe you can also help me with another problem. I'm trying to find a way to implement Copy/Paste actions that does not only copy and paste the selected text but also the text formatting applied to it as well.

Just wondered if you can also help me with this one since you know something about StyleConstants and AttributeSets... I posted this before as another topis but still no replies.

Looking forward to your next reply. Thanks!

Nino

nguba at 2007-6-29 2:22:43 > top of Java-index,Archived Forums,Swing...