JEditorpane und Headlines

Hallo liebe Community!

Ich habe ein Problem mit Java - JEditorPane - und bräuchte eure Hilfe dabei.
Ich würde gerne, wenn man einen Button drückt, eine neue Headline einfügen.
Soweit so gut, das würde mit dieser Action ja funktionieren:
Code:
new HTMLEditorKit.InsertHTMLTextAction("h1", "<h1></h1>", HTML.Tag.BODY, HTML.Tag.H1);
doch dann wird eine komplett neue Headline ohne Text eingefügt.
Ich will aber, dass der selektierte Text dann als Headline angezeigt wird.

Hoffe ihr könnt mir helfen.

mfg
 
sollte so gehen:

Code:
new HTMLEditorKit.InsertHTMLTextAction("h1", "<h1>"+jeditorpane.getSelectedText()+"</h1>", HTML.Tag.BODY, HTML.Tag.H1);
 
ja, das habe ich mir auch schon gedacht.
nur wird dann immer der Text genommen, welcher zur Zeit der Deklaration selektiert war - und das ist immer "null".

mfg
 
OK

Deklaration der Actions:

Code:
    private void initActions() {
        ActionListener a;
        
        //Bold
        a = new HTMLEditorKit.BoldAction();
        btBold.addActionListener(a);
        
        //Italic
        a = new HTMLEditorKit.ItalicAction();
        btItalic.addActionListener(a);
        
        //Underlined
        a = new HTMLEditorKit.UnderlineAction();
        btUnderlined.addActionListener(a);
        
        //Left
        a = new HTMLEditorKit.AlignmentAction("left", 0);
        btLeft.addActionListener(a);
        
        //Center
        a = new HTMLEditorKit.AlignmentAction("center", 1);
        btCenter.addActionListener(a);
        
        //right        
        a = new HTMLEditorKit.AlignmentAction("right", 2);
        btRight.addActionListener(a);
       
        //Copy
        a = new HTMLEditorKit.CopyAction();
        miKopieren.addActionListener(a);
        
        //Cut
        a = new HTMLEditorKit.CutAction();
        miAusschneiden.addActionListener(a);
        
        //Paste
        a = new HTMLEditorKit.PasteAction();
        miEinfuegen.addActionListener(a);  
        
        //Schwarz
        a = new HTMLEditorKit.ForegroundAction("set-foreground-black", Color.black);
        btFontColorSchwarz.addActionListener(a);
        
        //Rot
        a = new HTMLEditorKit.ForegroundAction("set-foreground-red", Color.red);
        btFontColorRot.addActionListener(a);        
        
        //Gelb
        a = new HTMLEditorKit.ForegroundAction("set-foreground-green", Color.green);
        btFontColorGruen.addActionListener(a);    
        
        //Rot
        a = new HTMLEditorKit.ForegroundAction("set-foreground-blue", Color.blue);
        btFontColorBlau.addActionListener(a); 
        
        a = new HTMLEditorKit.InsertHTMLTextAction("h1", "<h1></h1>", HTML.Tag.BODY, HTML.Tag.H1);
        btH1.addActionListener(a);   
        
        a = new HTMLEditorKit.InsertHTMLTextAction("h2", "<h2></h2>", HTML.Tag.BODY, HTML.Tag.H2);
        btH2.addActionListener(a);
        
        a = new HTMLEditorKit.InsertHTMLTextAction("h3", "<h3></h3>", HTML.Tag.BODY, HTML.Tag.H3);
        btH3.addActionListener(a);
    }

Das ist eigentlich das Wichtigste.
Und ich hätte nun aber gerne eine Action wie oben, die eine Headline (h1/h2/h3) einfügt mit selektierten Text (obige macht nur eine neue Headline, wo man den Text selbst einfügen muss).

mfg

the Oracle
 
hier ist der code mit dem ich das erfolgreich getestet habe:
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class HtmlTest extends JFrame {
	
	JEditorPane jep = new JEditorPane();
	JButton knopf = new JButton();
	
	public HtmlTest() {
	super("Test");
		
	this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	this.setSize( 500, 500 );
	this.setLayout(new GridLayout());
	   

	JButton knopf = new JButton();
	knopf.addActionListener(
		new ActionListener() {
		            public void actionPerformed(ActionEvent e) {
		            		try {
						new HTMLEditorKit().insertHTML((HTMLDocument)jep.getDocument(),0, "<h1>"+jep.getSelectedText()+"</h1>", 0,0, HTML.Tag.H1);
					} catch (BadLocationException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
		            }
		        }
		);

	
	jep.setEditorKit(new HTMLEditorKit());
	this.add(knopf);
	this.add(jep);
	this.setVisible( true ); 
	}

	public static void main(String[] args) {
		new HtmlTest();
	}
	
}

ich haber hier die Methode insertHTML verwendet. laut java api gibt es auch noch die methode insertAtBoundary welche die gleichen parameter hat, die du auch bei HTMLEditorKit.InsertHTMLTextAction verwendet hast. allerdings meldet mein compiler dass er die funktion nicht kennt. :rolleyes:
 
Zurück
Oben