Reputation: 37
I'm typing Text in a JTextPane (it will be converted in MS/Word document later) but I don't know how to set a few sentences in superscript.
So far, my test code works pretty well. I can specfified words in bold, Italic, even to change the font face or sizes. For doing I first select the words then I click on a JButton to execute Action Classes such as :
Action action = new StyledEditorKit.ItalicAction(); action.actionPerformed(null);
However I don't know how to do for superscript.
package test;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.swing.JLabel;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.JScrollPane;
import java.awt.Font;
public class Test_HTMLKit extends JFrame
{
private static final long serialVersionUID = 1L;
protected JTextPane MyText;
protected JComboBox Fonts, Sizes;
protected JButton See,
Bold,
Finish,
SuperScript,
Italic;
public HTMLEditorKit htmlKit = new HTMLEditorKit();
public HTMLDocument htmlDoc = null;
public static Style mainStyle = null;
protected String fontList[];
protected String fontSizes[] = {"8","10","12","14","18","24","48"};
public Test_HTMLKit()
{
getContentPane().setLayout(null);
this.setSize(753,490);
Fonts = new JComboBox();
Fonts.setBounds(25, 27, 187, 20);
getContentPane().add(Fonts);
See = new JButton("See HTML code ");
See.setBounds(43, 253, 130, 23);
Finish = new JButton("Exit");
Finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
Finish.setBounds(43, 287, 130, 23);
Sizes = new JComboBox();
Sizes.setBounds(299, 27, 62, 20);
JLabel lblFontFamlies = new JLabel("Font Families");
lblFontFamlies.setBounds(74, 11, 99, 14);
JLabel lblFontSizes = new JLabel("Font sizes");
lblFontSizes.setBounds(299, 11, 99, 14);
Bold = new JButton("Bold");
Bold.setBounds(25, 58, 67, 23);
Italic = new JButton("Italic");
Italic.setBounds(25, 106, 67, 23);
SuperScript = new JButton("SuperScript");
SuperScript.setBounds(25, 144, 105, 23);
MyText = new JTextPane();
JScrollPane Scroll = new JScrollPane(MyText);
Scroll.setBounds(269, 106, 310, 184);
getContentPane().add(See);
getContentPane().add(Finish);
getContentPane().add(Sizes);
getContentPane().add(lblFontFamlies);
getContentPane().add(lblFontSizes);
getContentPane().add(Bold);
getContentPane().add(Italic);
getContentPane().add(SuperScript);
getContentPane().add(Scroll);
JLabel lblNewLabel = new JLabel("????");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
lblNewLabel.setBounds(162, 148, 46, 14);
getContentPane().add(lblNewLabel);
this.setLocationRelativeTo(null);
initialize();
setVisible(true);
}
private void initialize()
{
// fill up the combo box
fontList = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int pos = 0;
for (int i = 0; i < fontList.length; i++)
{
String wk = fontList[i];
Fonts.addItem(wk);
if (wk.equals("Garamond")) pos = i;
}
Fonts.setSelectedIndex(pos);
for (int i = 0; i < fontSizes.length; i++)
{
String wk = fontSizes[i];
Sizes.addItem(wk);
if (wk == "12") pos = i;
}
Sizes.setSelectedIndex(pos);
MyText.setBorder( BorderFactory.createLineBorder(Color.RED) );
initStyle() ;
// MyText.setText("<html><head></head><body>Maître Corbeau sur un arbre, perché <br>Tenait en son bec un fromage.<br>" +
// "Maître Renard par l'odeur, alléché,<br> lui tint à peu près ce langage. ");
MyText.setText("<html><head></head><body>I would love that my code works <br>but unfortunatly there is a case.<br>where it does not work<br>that means with superscripts</body></html>");
définirListeners();
}
private void définirListeners()
{
Fonts.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) { clickOnFonts();}
});
Sizes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {clickOnSIzes();}
});
Bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.BoldAction();
action.actionPerformed(null);
}
});
Italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.ItalicAction();
action.actionPerformed(null);
}
});
See.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
displayHTMLode();
}
});
SuperScript.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// I dont' know what I have to do ....!!!
}
});
}
public boolean testSelection()
{
if (MyText.getSelectedText() == null ) return false;
// int selStart = MyText.getSelectionStart();
// int selEnd = MyText.getSelectionEnd();
return true;
}
public void clickOnFonts()
{
if(!testSelection()) return;
String curFontName = (String) Fonts.getSelectedItem();
Action FontFamily = new StyledEditorKit.FontFamilyAction(curFontName, curFontName);
FontFamily.actionPerformed(null);
// restorerZoneSelectionnée();
}
public void clickOnSIzes()
{
if(!testSelection()) return;
String wk = (String) Sizes.getSelectedItem();
int curFontSize = Integer.parseInt(wk);
Action FontSize = new StyledEditorKit.FontSizeAction("" + curFontSize, curFontSize);
FontSize.actionPerformed(null);
// restorerZoneSelectionnée();
}
private void displayHTMLode()
{
try
{
OutputStream writer = new ByteArrayOutputStream();
htmlDoc = (HTMLDocument) MyText.getDocument();
int le = htmlDoc.getLength();
htmlKit.write(writer, htmlDoc, 0, le) ;
String texteFinal = writer.toString();
System.out.println(texteFinal);
JOptionPane.showMessageDialog(this, texteFinal );
return;
}
catch (Exception ex) { System.out.println(ex.toString());}
return;
}
public void initStyle()
{
MutableAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setBold(attributes , true);
StyleConstants.setFontSize(attributes, 14);
StyleConstants.setFontFamily(attributes, "Garamond");
StyleConstants.setItalic(attributes, false);
StyleConstants.setAlignment(attributes,StyleConstants.ALIGN_RIGHT );
StyleConstants.setUnderline(attributes, false);
StyleConstants.setRightIndent(attributes, 0F);
StyleConstants.setLeftIndent(attributes, 0F);
StyleConstants.setForeground(attributes, Color.RED);
StyleConstants.setSubscript(attributes, false);
StyleConstants.setSuperscript(attributes, false);
StyleConstants.setLineSpacing(attributes, 1);
StyleConstants.setSpaceBelow(attributes, 1F);
StyleConstants.setSpaceAbove(attributes, 1F);
MyText.setEditorKit(new StyledEditorKit());
MyText.setContentType("text/html");
htmlDoc = (HTMLDocument) MyText.getStyledDocument();
htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), attributes, true);
MyText.setDocument(htmlDoc);
}
public static void main (String[] arg)
{
new Test_HTMLKit();
}
}
Upvotes: 0
Views: 516
Reputation: 37
I think I have found a more simple solution by looking at the source code for StyledEditorKit. I created an extension of this class, et based on its code, I developed the implementation for Superscript, I will do later for subscript. This works very well. Thanks anyway for your answer. Here is my code
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.TextAction;
public class MCB_StyleEditorKit extends StyledEditorKit
{
private static final long serialVersionUID = 1L;
private static final Action[] defaultActions =
{
new FontFamilyAction("font-family-SansSerif", "SansSerif"),
new FontFamilyAction("font-family-Monospaced", "Monospaced"),
new FontFamilyAction("font-family-Serif", "Serif"),
new FontSizeAction("font-size-8", 8),
new FontSizeAction("font-size-10", 10),
new FontSizeAction("font-size-12", 12),
new FontSizeAction("font-size-14", 14),
new FontSizeAction("font-size-16", 16),
new FontSizeAction("font-size-18", 18),
new FontSizeAction("font-size-24", 24),
new FontSizeAction("font-size-36", 36),
new FontSizeAction("font-size-48", 48),
new AlignmentAction("left-justify", StyleConstants.ALIGN_LEFT),
new AlignmentAction("center-justify", StyleConstants.ALIGN_CENTER),
new AlignmentAction("right-justify", StyleConstants.ALIGN_RIGHT),
new BoldAction(),
new ItalicAction(),
new SuperscriptAction(),
// new StyledInsertBreakAction(),
new UnderlineAction()
};
public Action[] getActions() {
return TextAction.augmentList(super.getActions(), this.defaultActions);
}
public static class SuperscriptAction extends StyledTextAction
{
private static final long serialVersionUID = 1L;
public SuperscriptAction()
{
super("font-superscript");
}
/**
* Toggles the bold attribute.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean superscript = (StyleConstants.isSuperscript(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSuperscript(sas, superscript);
setCharacterAttributes(editor, sas, false);
}
}
}
}
Upvotes: 0
Reputation: 6075
There is no builtin feature to do so. You can however create it. You can try one of the followings:
Upvotes: 0