tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
1
ZUGRIFFE
321
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    proforma proforma ist offline Mitglied
    Registriert seit
    Feb 2007
    Beiträge
    20
    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!!
    Geändert von proforma (13.06.11 um 00:30 Uhr)
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo,

    schau aml hier:
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    
    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
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

Ähnliche Themen

  1. Nur bestimmte Links zulassen, bzw Validierung
    Von typhoch2 im Forum Javascript & Ajax
    Antworten: 1
    Letzter Beitrag: 09.03.10, 19:00
  2. nur bestimmte Zeichen zulassen...
    Von ruNN0r im Forum PHP
    Antworten: 2
    Letzter Beitrag: 23.05.08, 21:21
  3. Antworten: 2
    Letzter Beitrag: 28.11.06, 18:26
  4. bestimmte URL zulassen
    Von makitaman im Forum PHP
    Antworten: 16
    Letzter Beitrag: 16.05.05, 10:53
  5. Antworten: 9
    Letzter Beitrag: 21.02.02, 18:37