下面列出了怎么用javax.swing.text.StyledEditorKit的API类实例代码及写法,或者点击链接到github查看源代码。
private static JButton createFontStyleBoldButton(DrawingEditor editor) {
JButton button = new JButton();
button.setIcon(ImageDirectory.getImageIcon("/toolbar/attributeFontBold.png"));
button.setText(null);
button.setToolTipText(BUNDLE.getString("buttonFactory.button_fontStyleBold.tooltipText"));
button.setFocusable(false);
AbstractAction action
= new AttributeToggler<>(editor,
AttributeKeys.FONT_BOLD,
Boolean.TRUE,
Boolean.FALSE,
new StyledEditorKit.BoldAction());
action.putValue(ActionUtil.UNDO_PRESENTATION_NAME_KEY,
BUNDLE.getString("buttonFactory.action_fontStyleBold.undo.presentationName"));
button.addActionListener(action);
return button;
}
private static JButton createFontStyleItalicButton(DrawingEditor editor) {
JButton button = new JButton();
button.setIcon(ImageDirectory.getImageIcon("/toolbar/attributeFontItalic.png"));
button.setText(null);
button.setToolTipText(BUNDLE.getString("buttonFactory.button_fontStyleItalic.tooltipText"));
button.setFocusable(false);
AbstractAction action
= new AttributeToggler<>(editor,
AttributeKeys.FONT_ITALIC,
Boolean.TRUE,
Boolean.FALSE,
new StyledEditorKit.BoldAction());
action.putValue(ActionUtil.UNDO_PRESENTATION_NAME_KEY,
BUNDLE.getString("buttonFactory.action_fontStyleItalic.undo.presentationName"));
button.addActionListener(action);
return button;
}
private static JButton createFontStyleUnderlineButton(DrawingEditor editor) {
JButton button = new JButton();
button.setIcon(ImageDirectory.getImageIcon("/toolbar/attributeFontUnderline.png"));
button.setText(null);
button.setToolTipText(BUNDLE.getString("buttonFactory.button_fontStyleUnderline.tooltipText"));
button.setFocusable(false);
AbstractAction action
= new AttributeToggler<>(editor,
AttributeKeys.FONT_UNDERLINE,
Boolean.TRUE,
Boolean.FALSE,
new StyledEditorKit.BoldAction());
action.putValue(ActionUtil.UNDO_PRESENTATION_NAME_KEY,
BUNDLE.getString("buttonFactory.action_fontStyleUnderline.undo.presentationName"));
button.addActionListener(action);
return button;
}
/** Creates new form HTMLEditor */
public HTMLEditor(EditableResources res, String htmlText) {
initComponents();
this.res = res;
htmlComponent = new com.codename1.ui.html.HTMLComponent();
htmlComponent.setBodyText(htmlText, "UTF-8");
final CodenameOneComponentWrapper wrapper = new CodenameOneComponentWrapper(htmlComponent);
uiPreview.add(java.awt.BorderLayout.CENTER, wrapper);
wysiwyg.setText(htmlText);
source.setText(htmlText);
Listener l = new Listener();
wysiwyg.getDocument().addDocumentListener(l);
source.getDocument().addDocumentListener(l);
JButton b = jToolBar1.add(new StyledEditorKit.BoldAction());
b.setText("<html><body><b>B</b></body></html>");
JButton i = jToolBar1.add(new StyledEditorKit.ItalicAction());
i.setText("<html><body><i>I</i></body></html>");
JButton u = jToolBar1.add(new StyledEditorKit.UnderlineAction());
u.setText("<html><body><u>U</u></body></html>");
jToolBar1.add(new InsertImageAction());
}
@Override
public void removeNotify () {
setEditorKit (new StyledEditorKit ());
if (hyperlinkListener != null) {
removeHyperlinkListener (hyperlinkListener);
}
scrollPane = null;
super.removeNotify ();
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean subscript = (StyleConstants.isSubscript(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSubscript(sas, subscript);
setCharacterAttributes(editor, sas, false);
}
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
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);
}
}
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sas, strikeThrough);
setCharacterAttributes(editor, sas, false);
}
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
Robot robot = new Robot();
robot.waitForIdle();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}
public static void main(String[] argv) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("4242228 Test");
JScrollPane sourcePane = new JScrollPane();
final JTextPane htmlEditor = new JTextPane();
final JTextPane sourceEditor = new JTextPane();
final JScrollPane editorPane = new JScrollPane();
tabPane = new JTabbedPane();
htmlEditor.setText(" ");
htmlEditor.setEditorKit(new HTMLEditorKit());
sourceEditor.setText(" ");
sourceEditor.setEditorKit(new StyledEditorKit());
frame.setLayout(new BorderLayout());
editorPane.getViewport().add(htmlEditor);
tabPane.addTab("Editor", editorPane);
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tabPane.getSelectedComponent() == editorPane) {
htmlEditor.setText(sourceEditor.getText());
} else {
sourceEditor.setText(htmlEditor.getText());
}
}
});
sourcePane.getViewport().add(sourceEditor);
tabPane.addTab("Source", sourcePane);
tabPane.setTabPlacement(SwingConstants.BOTTOM);
htmlEditor.setDocument(new HTMLDocument());
frame.add(tabPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
});
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
tabPane.setSelectedIndex(i % 2);
}
frame.dispose();
}
});
}