JTextPane <br oder <p> statt \n

Status
Nicht offen für weitere Antworten.

Capasso

Bekanntes Mitglied
Hi,

ich habe nen JTextPane das HTML darstellt. Wenn man enter drückt wird leider ein Zeilenumbruch (\n) eingefügt. Ich bräuchte aber ein <br> oder ein <p></p> statt \n.

Habe folgenden Quelltext im Netz gefunden.
Bei der Anwendung wird ein <p></p> eingefügt wenn man enter drückt. Allerdings ist mir völlig schleierhaft an welcher Stelle das geschieht.

[highlight=Java]

public class HTMLDocumentEditor extends JFrame implements ActionListener{

private HTMLDocument document;
private JTextPane textPane = new JTextPane();
private boolean debug = false;
private File currentFile;

/** Listener for the edits on the current document. */
protected UndoableEditListener undoHandler = new UndoHandler();
/** UndoManager that we add edits to. */
protected UndoManager undo = new UndoManager();

private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();

private Action cutAction = new DefaultEditorKit.CutAction();
private Action copyAction = new DefaultEditorKit.CopyAction();
private Action pasteAction = new DefaultEditorKit.PasteAction();
private Action boldAction = new StyledEditorKit.BoldAction();
private Action underlineAction = new StyledEditorKit.UnderlineAction();
private Action italicAction = new StyledEditorKit.ItalicAction();

private Action insertBreakAction = new DefaultEditorKit.InsertBreakAction();
private HTMLEditorKit.InsertHTMLTextAction unorderedListAction
= new HTMLEditorKit.InsertHTMLTextAction("Bullets", "&lt;ul&gt;&lt;li&gt; &lt;/li&gt;&lt;/ul&gt;",HTML.Tag.P,HTML.Tag.UL);
private HTMLEditorKit.InsertHTMLTextAction bulletAction
= new HTMLEditorKit.InsertHTMLTextAction("Bullets", "&lt;li&gt; &lt;/li&gt;",HTML.Tag.UL,HTML.Tag.LI);
public HTMLDocumentEditor(){
super("HTMLDocumentEditor");
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument)editorKit.createDefaultDocument();
// Force SwingSet to come up in the Cross Platform L&amp;F
try {
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// If you want the System L&amp;F instead, comment out the above line and
// uncomment the following:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&amp;F: " + exc);
}
init();
}

public static void main(String[] args){
HTMLDocumentEditor editor = new HTMLDocumentEditor();
}

public void init(){

addWindowListener(new FrameListener());

JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu colorMenu = new JMenu("Color");
JMenu fontMenu = new JMenu("Font");
JMenu styleMenu = new JMenu("Style");
JMenu alignMenu = new JMenu("Align");
JMenu helpMenu = new JMenu("Help");

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(colorMenu);
menuBar.add(fontMenu);
menuBar.add(styleMenu);
menuBar.add(alignMenu);
menuBar.add(helpMenu);

JMenuItem newItem = new JMenuItem("New", new ImageIcon("whatsnew-bang.gif"));
JMenuItem openItem = new JMenuItem("Open",new ImageIcon("open.gif"));
JMenuItem saveItem = new JMenuItem("Save",new ImageIcon("save.gif"));
JMenuItem saveAsItem = new JMenuItem("Save As");
JMenuItem exitItem = new JMenuItem("Exit",new ImageIcon("exit.gif"));

newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveAsItem.addActionListener(this);
exitItem.addActionListener(this);

fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.add(exitItem);


JMenuItem undoItem = new JMenuItem(undoAction);
JMenuItem redoItem = new JMenuItem(redoAction);
JMenuItem cutItem = new JMenuItem(cutAction);
JMenuItem copyItem = new JMenuItem(copyAction);
JMenuItem pasteItem = new JMenuItem(pasteAction);
JMenuItem clearItem = new JMenuItem("Clear");
JMenuItem selectAllItem = new JMenuItem("Select All");
JMenuItem insertBreaKItem = new JMenuItem(insertBreakAction);
JMenuItem unorderedListItem = new JMenuItem(unorderedListAction);
JMenuItem bulletItem = new JMenuItem(bulletAction);
cutItem.setText("Cut");
copyItem.setText("Copy");
pasteItem.setText("Paste");
insertBreaKItem.setText("Break");
cutItem.setIcon(new ImageIcon("cut.gif"));
copyItem.setIcon(new ImageIcon("copy.gif"));
pasteItem.setIcon(new ImageIcon("paste.gif"));
insertBreaKItem.setIcon(new ImageIcon("break.gif"));
unorderedListItem.setIcon(new ImageIcon("bullets.gif"));

clearItem.addActionListener(this);
selectAllItem.addActionListener(this);

editMenu.add(undoItem);
editMenu.add(redoItem);
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(clearItem);
editMenu.add(selectAllItem);
editMenu.add(insertBreaKItem);
editMenu.add(unorderedListItem);
editMenu.add(bulletItem);

JMenuItem redTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Red",Color.red));
JMenuItem orangeTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Orange",Color.orange));
JMenuItem yellowTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Yellow",Color.yellow));
JMenuItem greenTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Green",Color.green));
JMenuItem blueTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Blue",Color.blue));
JMenuItem cyanTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Cyan",Color.cyan));
JMenuItem magentaTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Magenta",Color.magenta));
JMenuItem blackTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Black",Color.black));
redTextItem.setIcon(new ImageIcon("red.gif"));
orangeTextItem.setIcon(new ImageIcon("orange.gif"));
yellowTextItem.setIcon(new ImageIcon("yellow.gif"));
greenTextItem.setIcon(new ImageIcon("green.gif"));
blueTextItem.setIcon(new ImageIcon("blue.gif"));
cyanTextItem.setIcon(new ImageIcon("cyan.gif"));
magentaTextItem.setIcon(new ImageIcon("magenta.gif"));
blackTextItem.setIcon(new ImageIcon("black.gif"));
colorMenu.add(redTextItem);
colorMenu.add(orangeTextItem);
colorMenu.add(yellowTextItem);
colorMenu.add(greenTextItem);
colorMenu.add(blueTextItem);
colorMenu.add(cyanTextItem);
colorMenu.add(magentaTextItem);
colorMenu.add(blackTextItem);
JMenu fontTypeMenu = new JMenu("Font Type");
fontMenu.add(fontTypeMenu);

String[] fontTypes = {"SansSerif", "Serif", "Monospaced", "Dialog", "DialogInput"};
for (int i = 0; i < fontTypes.length;i++){
if (debug) System.out.println(fontTypes);
JMenuItem nextTypeItem = new JMenuItem(fontTypes);
nextTypeItem.setAction(new StyledEditorKit.FontFamilyAction(fontTypes, fontTypes));
fontTypeMenu.add(nextTypeItem);
}
JMenu fontSizeMenu = new JMenu("Font Size");
fontMenu.add(fontSizeMenu);
int[] fontSizes = {6, 8,10,12,14, 16, 20,24, 32,36,48,72};
for (int i = 0; i <fontSizes.length;i++){
if (debug) System.out.println(fontSizes);
JMenuItem nextSizeItem = new JMenuItem(String.valueOf(fontSizes));
nextSizeItem.setAction(new StyledEditorKit.FontSizeAction(String.valueOf(fontSizes), fontSizes));
fontSizeMenu.add(nextSizeItem);
}

JMenuItem boldMenuItem = new JMenuItem(boldAction);
JMenuItem underlineMenuItem = new JMenuItem(underlineAction);
JMenuItem italicMenuItem = new JMenuItem(italicAction);

boldMenuItem.setText("Bold");
underlineMenuItem.setText("Underline");
italicMenuItem.setText("Italic");
boldMenuItem.setIcon(new ImageIcon("bold.gif"));
underlineMenuItem.setIcon(new ImageIcon("underline.gif"));
italicMenuItem.setIcon(new ImageIcon("italic.gif"));
styleMenu.add(boldMenuItem);
styleMenu.add(underlineMenuItem);
styleMenu.add(italicMenuItem);

JMenuItem subscriptMenuItem = new JMenuItem(new SubscriptAction());
JMenuItem superscriptMenuItem = new JMenuItem(new SuperscriptAction());
JMenuItem strikeThroughMenuItem = new JMenuItem(new StrikeThroughAction());
subscriptMenuItem.setText("Subscript");
superscriptMenuItem.setText("Superscript");
strikeThroughMenuItem.setText("StrikeThrough");
subscriptMenuItem.setIcon(new ImageIcon("subscript.gif"));
superscriptMenuItem.setIcon(new ImageIcon("superscript.gif"));
strikeThroughMenuItem.setIcon(new ImageIcon("strikethough.gif"));
styleMenu.add(subscriptMenuItem);
styleMenu.add(superscriptMenuItem);
styleMenu.add(strikeThroughMenuItem);

JMenuItem leftAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
JMenuItem centerMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
JMenuItem rightAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
leftAlignMenuItem.setText("Left Align");
centerMenuItem.setText("Center");
rightAlignMenuItem.setText("Right Align");
leftAlignMenuItem.setIcon(new ImageIcon("left.gif"));
centerMenuItem.setIcon(new ImageIcon("center.gif"));
rightAlignMenuItem.setIcon(new ImageIcon("right.gif"));
alignMenu.add(leftAlignMenuItem);
alignMenu.add(centerMenuItem);
alignMenu.add(rightAlignMenuItem);
JMenuItem helpItem = new JMenuItem("Help");
helpItem.addActionListener(this);
helpMenu.add(helpItem);
JMenuItem shortcutsItem = new JMenuItem("Keyboard Shortcuts");
shortcutsItem.addActionListener(this);
helpMenu.add(shortcutsItem);
JMenuItem aboutItem = new JMenuItem("About QuantumHyperSpace");
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);

JPanel editorControlPanel = new JPanel();
//editorControlPanel.setLayout(new GridLayout(3,3));
editorControlPanel.setLayout(new FlowLayout());
/* JButtons */
JButton cutButton = new JButton(cutAction);
JButton copyButton = new JButton(copyAction);
JButton pasteButton = new JButton(pasteAction);
JButton boldButton = new JButton(boldAction);
JButton underlineButton = new JButton(underlineAction);
JButton italicButton = new JButton(italicAction);

//JButton insertButton = new JButton(insertAction);
//JButton insertBreakButton = new JButton(insertBreakAction);
//JButton tabButton = new JButton(tabAction);

cutButton.setText("Cut");
copyButton.setText("Copy");
pasteButton.setText("Paste");

boldButton.setText("Bold");
underlineButton.setText("Underline");
italicButton.setText("Italic");

//insertButton.setText("Insert");
//insertBreakButton.setText("Insert Break");
//tabButton.setText("Tab");
cutButton.setIcon(new ImageIcon("cut.gif"));
copyButton.setIcon(new ImageIcon("copy.gif"));
pasteButton.setIcon(new ImageIcon("paste.gif"));
boldButton.setIcon(new ImageIcon("bold.gif"));
underlineButton.setIcon(new ImageIcon("underline.gif"));
italicButton.setIcon(new ImageIcon("italic.gif"));

editorControlPanel.add(cutButton);
editorControlPanel.add(copyButton);
editorControlPanel.add(pasteButton);

editorControlPanel.add(boldButton);
editorControlPanel.add(underlineButton);
editorControlPanel.add(italicButton);

//editorControlPanel.add(insertButton);
//editorControlPanel.add(insertBreakButton);
//editorControlPanel.add(tabButton);

JButton subscriptButton = new JButton(new SubscriptAction());
JButton superscriptButton = new JButton(new SuperscriptAction());
JButton strikeThroughButton = new JButton(new StrikeThroughAction());
subscriptButton.setIcon(new ImageIcon("subscript.gif"));
superscriptButton.setIcon(new ImageIcon("superscript.gif"));
strikeThroughButton.setIcon(new ImageIcon("strikethough.gif"));

JPanel specialPanel = new JPanel();
specialPanel.setLayout(new FlowLayout());
specialPanel.add(subscriptButton);
specialPanel.add(superscriptButton);
specialPanel.add(strikeThroughButton);
//JButton leftAlignButton = new JButton(new AlignLeftAction());
//JButton centerButton = new JButton(new CenterAction());
//JButton rightAlignButton = new JButton(new AlignRightAction());

JButton leftAlignButton = new JButton(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
JButton centerButton = new JButton(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
JButton rightAlignButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
JButton colorButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
leftAlignButton.setIcon(new ImageIcon("left.gif"));
centerButton.setIcon(new ImageIcon("center.gif"));
rightAlignButton.setIcon(new ImageIcon("right.gif"));
colorButton.setIcon(new ImageIcon("color.gif"));
leftAlignButton.setText("Left Align");
centerButton.setText("Center");
rightAlignButton.setText("Right Align");

JPanel alignPanel = new JPanel();
alignPanel.setLayout(new FlowLayout());
alignPanel.add(leftAlignButton);
alignPanel.add(centerButton);
alignPanel.add(rightAlignButton);
document.addUndoableEditListener(undoHandler);
resetUndoManager();
textPane = new JTextPane(document);
textPane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5*screenSize.width/8,5*screenSize.height/8);
scrollPane.setPreferredSize(scrollPaneSize);

JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER);
toolPanel.add(alignPanel, BorderLayout.SOUTH);
getContentPane().add(menuBar, BorderLayout.NORTH);
//getContentPane().add(toolPanel, BorderLayout.CENTER);
getContentPane().add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
startNewDocument();
show();
}

public void actionPerformed(ActionEvent ae){
String actionCommand = ae.getActionCommand();
if (debug){
int modifier = ae.getModifiers();
long when = ae.getWhen();
String parameter = ae.paramString();
System.out.println("actionCommand: " + actionCommand);
System.out.println("modifier: " + modifier);
System.out.println("when: " + when);
System.out.println("parameter: " + parameter);
}
if (actionCommand.compareTo("New") == 0){
startNewDocument();
} else if (actionCommand.compareTo("Open") == 0){
openDocument();
} else if (actionCommand.compareTo("Save") == 0){
saveDocument();
} else if (actionCommand.compareTo("Save As") == 0){
saveDocumentAs();
} else if (actionCommand.compareTo("Exit") == 0){
exit();
} else if (actionCommand.compareTo("Clear") == 0){
clear();
} else if (actionCommand.compareTo("Select All") == 0){
selectAll();
} else if (actionCommand.compareTo("Help") == 0){
help();
} else if (actionCommand.compareTo("Keyboard Shortcuts") == 0){
showShortcuts();
} else if (actionCommand.compareTo("About QuantumHyperSpace") == 0){
aboutQuantumHyperSpace();
}
}

protected void resetUndoManager() {
undo.discardAllEdits();
undoAction.update();
redoAction.update();
}

public void startNewDocument(){
Document oldDoc = textPane.getDocument();
if(oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument)editorKit.createDefaultDocument();
textPane.setDocument(document);
currentFile = null;
setTitle("HTMLDocumentEditor");
textPane.getDocument().addUndoableEditListener(undoHandler);
resetUndoManager();
}
public void openDocument(){
try{
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION){
currentFile = chooser.getSelectedFile();
setTitle(currentFile.getName());
FileReader fr = new FileReader(currentFile);
Document oldDoc = textPane.getDocument();
if(oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument)editorKit.createDefaultDocument();
editorKit.read(fr,document,0);
document.addUndoableEditListener(undoHandler);
textPane.setDocument(document);
resetUndoManager();
}
}catch(BadLocationException ble){
System.err.println("BadLocationException: " + ble.getMessage());
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public void saveDocument(){
if (currentFile != null){
try{
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}else{
saveDocumentAs();
}
}
public void saveDocumentAs(){
try{
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION){
File newFile = chooser.getSelectedFile();
if (newFile.exists()){
String message = newFile.getAbsolutePath()
+ " already exists. \n"
+ "Do you want to replace it?";
if (JOptionPane.showConfirmDialog(this, message) == JOptionPane.YES_OPTION){
currentFile = newFile;
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
}
}else{
currentFile = new File(newFile.getAbsolutePath());
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
}
}
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public void exit(){
String exitMessage = "Are you sure you want to exit?";
if (JOptionPane.showConfirmDialog(this, exitMessage) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
public void clear(){
startNewDocument();
}
public void selectAll(){
textPane.selectAll();
}

public void help(){
JOptionPane.showMessageDialog(this,"DocumentEditor.java\n" +
"Author: Charles Bell\n" +
"Version: May 25, 2002\n" +
"http://www.quantumhyperspace.com\n" +
"QuantumHyperSpace Programming Services");
}
[/highlight]
 

Capasso

Bekanntes Mitglied
.. fortsetzung
[highlight=Java]


public void showShortcuts(){
String shortcuts = "Navigate in | Tab\n" +
"Navigate out | Ctrl+Tab\n" +
"Navigate out backwards | Shift+Ctrl+Tab\n" +
"Move up/down a line | Up/Down Arrown\n" +
"Move left/right a component or char | Left/Right Arrow\n" +
"Move up/down one vertical block | PgUp/PgDn\n" +
"Move to start/end of line | Home/End\n" +
"Move to previous/next word | Ctrl+Left/Right Arrow\n" +
"Move to start/end of data | Ctrl+Home/End\n" +
"Move left/right one block | Ctrl+PgUp/PgDn\n" +
"Select All | Ctrl+A\n" +
"Extend selection up one line | Shift+Up Arrow\n" +
"Extend selection down one line | Shift+Down Arrow\n" +
"Extend selection to beginning of line | Shift+Home\n" +
"Extend selection to end of line | Shift+End\n" +
"Extend selection to beginning of data | Ctrl+Shift+Home\n" +
"Extend selection to end of data | Ctrl+Shift+End\n" +
"Extend selection left | Shift+Right Arrow\n" +
"Extend selection right | Shift+Right Arrow\n" +
"Extend selection up one vertical block | Shift+PgUp\n" +
"Extend selection down one vertical block | Shift+PgDn\n" +
"Extend selection left one block | Ctrl+Shift+PgUp\n" +
"Extend selection right one block | Ctrl+Shift+PgDn\n" +
"Extend selection left one word | Ctrl+Shift+Left Arrow\n" +
"Extend selection right one word | Ctrl+Shift+Right Arrow\n";
JOptionPane.showMessageDialog(this,shortcuts);
}
public void aboutQuantumHyperSpace(){
JOptionPane.showMessageDialog(this,"QuantumHyperSpace Programming Services\n" +
"http://www.quantumhyperspace.com\n" +
"email: support@quantumhyperspace.com\n" +
" or \n" +
"email: charles@quantumhyperspace.com\n",
"QuantumHyperSpace",JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("quantumhyperspace.gif"));
}

class FrameListener extends WindowAdapter{
public void windowClosing(WindowEvent we){
exit();
}
}

class SubscriptAction extends StyledEditorKit.StyledTextAction{

public SubscriptAction(){
super(StyleConstants.Subscript.toString());
}
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);
}
}
}

class SuperscriptAction extends StyledEditorKit.StyledTextAction{

public SuperscriptAction(){
super(StyleConstants.Superscript.toString());
}
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);
}
}
}

class StrikeThroughAction extends StyledEditorKit.StyledTextAction{

public StrikeThroughAction(){
super(StyleConstants.StrikeThrough.toString());
}

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);
}
}
}

class HTMLFileFilter extends javax.swing.filechooser.FileFilter{

public boolean accept(File f){
return ((f.isDirectory()) ||(f.getName().toLowerCase().indexOf(".htm") > 0));
}

public String getDescription(){
return "html";
}
}

class UndoHandler implements UndoableEditListener {
/**
* Messaged when the Document has created an edit, the edit is
* added to &lt;code&gt;undo&lt;/code&gt;, an instance of UndoManager.
*/
public void undoableEditHappened(UndoableEditEvent e) {
undo.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}

class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.undo();
} catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update() {
if(undo.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
}else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction {

public RedoAction() {
super("Redo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.redo();
} catch (CannotRedoException ex) {
System.err.println("Unable to redo: " + ex);
ex.printStackTrace();
}
update();
undoAction.update();
}

protected void update() {
if(undo.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
}else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
}
[/highlight]
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Stylen eines JTextPane funktioniert nicht AWT, Swing, JavaFX & SWT 1
T JTextPane ignoriert HTML Zeilenumbruch <br> tag wegen eigenem HTMLEditorKit AWT, Swing, JavaFX & SWT 5
J Swing JTextpane aktuallisiert nicht den setText? AWT, Swing, JavaFX & SWT 9
M Strings im JTextPane vergleichen und mit StyledDocument formatieren AWT, Swing, JavaFX & SWT 3
X JTextPane automatischer Zeilenumbruch IM Wort AWT, Swing, JavaFX & SWT 2
R GUI Element in einer Methode aktualisieren (JTextPane) AWT, Swing, JavaFX & SWT 13
M Swing JTextPane, Bilder und Text-flow AWT, Swing, JavaFX & SWT 2
D Swing Breite einer HTML Tabelle in einer JTextPane AWT, Swing, JavaFX & SWT 6
S Swing Warum funktioniert der automatische Zeilenumbruch mit arabischen Zeichen beim JTextPane nicht AWT, Swing, JavaFX & SWT 3
S Swing JtextPane sau langsam AWT, Swing, JavaFX & SWT 15
J Swing JTextPane mit JScrollPane auf JPanel scrollen nicht AWT, Swing, JavaFX & SWT 6
C JTextPane scrollt mit setText() immer nach oben AWT, Swing, JavaFX & SWT 2
S Swing Style Attribute aus JTextPane kopieren AWT, Swing, JavaFX & SWT 1
E JTextPane Focus AWT, Swing, JavaFX & SWT 1
TheJavaKid JTextPane: komplexe Formatierung (links) AWT, Swing, JavaFX & SWT 2
B Swing JTextPane - Einfärbungen auf "default" AWT, Swing, JavaFX & SWT 2
A Swing Einrücken mehrerer Zeilen per Tab in einem JTextPane/JTextArea AWT, Swing, JavaFX & SWT 3
T JtextPane Zeile aktualisieren AWT, Swing, JavaFX & SWT 3
T Hintergrundbild in JTextPane fixieren AWT, Swing, JavaFX & SWT 2
M Swing JTextPane frisst Anführungszeichen AWT, Swing, JavaFX & SWT 2
M Swing JTextPane + Scrollbar = Fragen AWT, Swing, JavaFX & SWT 15
H Swing JTextPane hat unästhetisches Scrollbar AWT, Swing, JavaFX & SWT 3
R Swing Renderproblem bei HTML in einer JTextPane AWT, Swing, JavaFX & SWT 12
N Swing JTextPane zur Anzeige von HTML AWT, Swing, JavaFX & SWT 2
E Swing jTextPane Text datei anzeigen AWT, Swing, JavaFX & SWT 7
xehpuk Swing Line-wrapping von JTextPane (in JScrollPane) AWT, Swing, JavaFX & SWT 3
Tobse Swing [JTextPane] Farbliche Hervorhebung AWT, Swing, JavaFX & SWT 9
X Swing JTextPane bei Größenänderung Formatierung behalten AWT, Swing, JavaFX & SWT 11
0 JTextPane copy/paste trotz editable(false) ? AWT, Swing, JavaFX & SWT 10
T Swing JTextPane Tastatureingabe abfangen AWT, Swing, JavaFX & SWT 4
B Durchsichtiges JScroll und JTextPane AWT, Swing, JavaFX & SWT 4
xehpuk Swing JTextPane in JScrollPane wird nicht ordentlich neugezeichnet AWT, Swing, JavaFX & SWT 3
S Swing JTextPane append formatprobleme AWT, Swing, JavaFX & SWT 5
C JTextPane / JEditorPane ohne Keywords farbig machen AWT, Swing, JavaFX & SWT 4
C Zeilenumbruch von JTextPane wird nicht erkannt AWT, Swing, JavaFX & SWT 2
S JEditorPane oder JTextPane? AWT, Swing, JavaFX & SWT 2
P Aktuelle Zeile in JTextPane farblich hervorheben AWT, Swing, JavaFX & SWT 3
J JTextPane Background setzen AWT, Swing, JavaFX & SWT 6
T Text in JTextPane vertikal (!) zentrieren AWT, Swing, JavaFX & SWT 4
M Zugriff paralleler Threads auf selbes JTextPane AWT, Swing, JavaFX & SWT 6
M JTextPane - bewegungsloses Hintergrundsbild (Duplikat) AWT, Swing, JavaFX & SWT 2
B JTextPane formatierten Inhalt speichern AWT, Swing, JavaFX & SWT 2
E JTextPane setText ignoriert \n AWT, Swing, JavaFX & SWT 6
P Swing Sichtbaren teil eines JTextPane herausbekommen (mit JScrollPane) AWT, Swing, JavaFX & SWT 2
C Swing JTextPane zeigt HTML-Text aus Variable nicht an :( AWT, Swing, JavaFX & SWT 3
T Zeichen zählen JTextPane AWT, Swing, JavaFX & SWT 4
K Swing JTextPane Formatierungen SyntaxHighlighting AWT, Swing, JavaFX & SWT 11
F Mit TAB JTextPane wechseln AWT, Swing, JavaFX & SWT 5
J JSlider auf JTextPane? AWT, Swing, JavaFX & SWT 2
S Buchstabenpositionen in JTextPane erhalten..? AWT, Swing, JavaFX & SWT 5
I JTextPane und Hyperlink (ohne JEditorPane) AWT, Swing, JavaFX & SWT 5
T Swing JTextArea, JTextPane, JEditorPane...Unterschied?! AWT, Swing, JavaFX & SWT 3
A Swing JTextPane sehr langsam AWT, Swing, JavaFX & SWT 6
P Drucken von Jtextpane mit Seitenangabe AWT, Swing, JavaFX & SWT 1
M Swing JTextPane mit JScrollPane soll aussehen wie JLabel AWT, Swing, JavaFX & SWT 3
A JTextPane: Text hinzufügen AWT, Swing, JavaFX & SWT 9
E Swing JTextPane Inhalt(Text und Bilder) als HTML text bekommen AWT, Swing, JavaFX & SWT 3
K Swing jTextPane zeilenumbruch AWT, Swing, JavaFX & SWT 2
Stillmatic JTextPane langsam? AWT, Swing, JavaFX & SWT 5
E Swing JTextPane linewrap AWT, Swing, JavaFX & SWT 4
M Swing [JTextPane] Cursor lässt sich nicht mit Pfeiltasten bewegen AWT, Swing, JavaFX & SWT 3
T Swing xml in JTextPane mit EditorKit, StyledDocument & co. AWT, Swing, JavaFX & SWT 3
M Cursor setzen im JTextPane AWT, Swing, JavaFX & SWT 3
M Formatierungsfehler JTextPane WARUM ?? AWT, Swing, JavaFX & SWT 4
JavaKaffee Array auslesen in jTextPane? AWT, Swing, JavaFX & SWT 9
E JTextPane Bold Italic und Co AWT, Swing, JavaFX & SWT 6
M JTextPane Ende des Feldes AWT, Swing, JavaFX & SWT 4
D JTextPane - Text andersfarbig unterstreichen AWT, Swing, JavaFX & SWT 7
P Probleme mit JTextpane AWT, Swing, JavaFX & SWT 7
M Größe von JTextPane manuell bei Bedarf anpassen AWT, Swing, JavaFX & SWT 6
bugmenot JTextPane String stimmt nicht mit übergebenem String überein AWT, Swing, JavaFX & SWT 9
C JTextPane HTML und eigene Tags AWT, Swing, JavaFX & SWT 10
G JTextPane durchsichtig? AWT, Swing, JavaFX & SWT 6
GilbertGrape Fehler bei HTML in JTextPane AWT, Swing, JavaFX & SWT 2
S JTextPane: Probem mit AlignmentAction AWT, Swing, JavaFX & SWT 3
J Problem mit JTextPane & JScrollPane (horizontal) AWT, Swing, JavaFX & SWT 3
B JTextPane und Image importieren in HTML String. AWT, Swing, JavaFX & SWT 2
J JTextPane mit genau 80 Zeichen breite definieren? AWT, Swing, JavaFX & SWT 3
J Html in JTextPane anzeigen? AWT, Swing, JavaFX & SWT 2
Steev JTextPane mit Textformatierung funktioniert nicht AWT, Swing, JavaFX & SWT 5
S JTextPane - markierten Text Formatieren AWT, Swing, JavaFX & SWT 11
P HTMLDocument in einem JTextPane. Anhängen von HTML-Code AWT, Swing, JavaFX & SWT 2
M Makierten Text in JTextPane zentrieren AWT, Swing, JavaFX & SWT 2
F JTextPane aktive Zeile AWT, Swing, JavaFX & SWT 2
G insert & JTextPane AWT, Swing, JavaFX & SWT 4
J JTextPane bekommt keinen Focus! AWT, Swing, JavaFX & SWT 3
S Farben eienr JTextPane dynamisch zuweisen AWT, Swing, JavaFX & SWT 2
R JTextPane scrollen vermeiden AWT, Swing, JavaFX & SWT 2
B Feste Breite einer JTextPane, die HTML anzeigt AWT, Swing, JavaFX & SWT 8
A JScrollPane in JTextPane AWT, Swing, JavaFX & SWT 2
B Swing Logging: Ein/Ausblenden eines scrollenden JTextPane AWT, Swing, JavaFX & SWT 2
F Java JTextPane AWT, Swing, JavaFX & SWT 13
Saxony JTextPane - letzte Zeile löschen AWT, Swing, JavaFX & SWT 4
TheJavaKid LineWrap & WrapStyleWord in JTextPane AWT, Swing, JavaFX & SWT 3
G Zeilenabstand bei HTML-Texten in JLabel/JTextPane AWT, Swing, JavaFX & SWT 7
O Swing Xml highlightning mit JTextPane und Regular expressions AWT, Swing, JavaFX & SWT 5
J JTextPane Alignment AWT, Swing, JavaFX & SWT 6
Y entfernten String einer JTextPane per DocumentListener AWT, Swing, JavaFX & SWT 5
B Ein großes JTextPane komplett drucken AWT, Swing, JavaFX & SWT 2
7 Latex/MathML Code in JTextPane AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen

Neue Themen


Oben