JEditorPane nur bestimmte Aktionen zulassen

proforma

Mitglied
Hallo,

ich hoffe ihr könnt mir bei meinem Problem helfen: Ich zeige in einem JEditorPane eine html Seite an.
Die Seite besteht nur aus Text.

Nun möchte ich für meinen Editor nur bestimmte Aktionen zulassen. Ich möchte nicht, dass man den Text verändern kann, was ja mit setEditable(false) getan wäre. Aber gleichzeitig soll noch die Möglichkeit erhalten bleiben, dass man den Text im JEditorPane unterstreichen kann.

Hat da jemand irgendeine Idee, wie man das umsetzen könnte? Also der Text bleibt unverändert, aber Aktionen wie Unterstreichen sind möglich.

Danke!!
 
Zuletzt bearbeitet:
Hallo,

schau aml hier:
Java:
package de.tutorials;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class JEditorPaneExample extends JFrame {

	public JEditorPaneExample() {
		super("JEditorPaneExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationByPlatform(true);

		try {
			final JEditorPane editorPane = new JEditorPane(
					"http://www.w3.org/TR/html401/");
			editorPane.setEditable(false);
			
			add(new JScrollPane(editorPane), BorderLayout.CENTER);
			JButton btnAddUnderline = new JButton(new AbstractAction("Add underline to selected text") {
				public void actionPerformed(ActionEvent e) {

					SimpleAttributeSet attrSet = new SimpleAttributeSet();

					attrSet.addAttribute(StyleConstants.Underline, true);
					StyledDocument styledDocument = (StyledDocument) editorPane.getDocument();
					int selectionLength = editorPane.getSelectionEnd()-editorPane.getSelectionStart();
					styledDocument.setCharacterAttributes(editorPane.getSelectionStart(),selectionLength, attrSet, false);
				}
			});

			JPanel commandPanel = new JPanel(new FlowLayout());
			commandPanel.add(btnAddUnderline);
			commandPanel.add(new JButton(new AbstractAction("Save") {
				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.showSaveDialog(JEditorPaneExample.this);
					fileChooser.setApproveButtonText("Speichern");
					File htmlFile = fileChooser.getSelectedFile();
					try {
						editorPane.write(new BufferedWriter(new FileWriter(htmlFile)));
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}));
			
			add(commandPanel, BorderLayout.SOUTH);
			setSize(new Dimension(800, 600));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new JEditorPaneExample().setVisible(true);
	}

}

Gruß Tom
 

Neue Beiträge

Zurück