SWING - Aktionen in Toolbar hinzufügen

Status
Nicht offen für weitere Antworten.

win_tho

Neues Mitglied
Hallo,

habe einen kleinen Editor, der HTML-Code erzeugt. Diesen habe ich mir aus zahlreichen Beispielen zu diesem Thema zusammengebaut und soweit funktioniert auch alles wunderbar. Änderungen am Text werden mit einem Caret-Listener detektiert.

Das Problem, was ich allerdings habe ist, dass Änderungen an der Formatierung, die durch die Toolbar erfolgen nicht als Änderung detektiert werden. Um die Änderungen jedoch abzuspeichern muss ich allerdings irgendwie feststellen, wann der Text oder die Formatierung denn angepasst wurde.

Daher meine Frage: Wie füge ich eine Aktion zu einem Knopf in der Toolbar hinzu, oder welche anderen Möglichkeiten gibt es, Änderungen an der Formatierung festzustellen.

Vorab schon einmal herzlichen Dank

Code:
public class ESReaderWriter extends JPanel {

	private JEditorPane textComp;
	private ESGUIDirector guiDirector;
	private JToolBar bar;

	/**
	 * Konstruktor des ESReaderWriter.
	 * 
	 * @param guiDirector
	 */
	public ESReaderWriter(ESGUIDirector guiDirector) {
		
		this.guiDirector = guiDirector;

		this.textComp = new JEditorPane();
		this.textComp.setContentType("text/html"); 
		
		makeActionsPretty();

		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(textComp), BorderLayout.CENTER);
		
		// Toobar erzeugen und einfuegen
		this.createToolBar();
		this.add(this.bar, BorderLayout.NORTH);
		
		// Caret-Listener
		this.textComp.addCaretListener(new ESWriterListener(this.guiDirector));
		
	}
	
	/**
	 * Methode zum Hinzufuegen von Icons und Namen.
	 */
	protected void makeActionsPretty() {

		Action a;

		a = textComp.getActionMap().get(DefaultEditorKit.cutAction);
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cut.gif"));
		a.putValue(Action.NAME, "Cut");

		a = textComp.getActionMap().get(DefaultEditorKit.copyAction);
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
		a.putValue(Action.NAME, "Copy");

		a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
		a.putValue(Action.NAME, "Paste");

		a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
		a.putValue(Action.NAME, "Select All");

		a = this.textComp.getActionMap().get("font-bold");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/bold.gif"));
		a.putValue(Action.NAME, "Bold");
		a = this.textComp.getActionMap().get("font-italic");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/italic.gif"));
		a.putValue(Action.NAME, "Italic");
		a = this.textComp.getActionMap().get("font-underline");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/underline.gif"));
		a.putValue(Action.NAME, "Underline");

		a = this.textComp.getActionMap().get("font-size-10");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/10.gif"));
		a.putValue(Action.NAME, "10");
		a = this.textComp.getActionMap().get("font-size-12");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/12.gif"));
		a.putValue(Action.NAME, "12");
		a = this.textComp.getActionMap().get("font-size-14");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/14.gif"));
		a.putValue(Action.NAME, "14");
		a = this.textComp.getActionMap().get("font-size-16");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/16.gif"));
		a.putValue(Action.NAME, "16");
		a = this.textComp.getActionMap().get("font-size-24");
		a.putValue(Action.SMALL_ICON, new ImageIcon("icons/24.gif"));
		a.putValue(Action.NAME, "24");
	}

	/**
	 * Methode zum Hinzufuegen der FontActions zur Toolbar.
	 * (Toolbar des ESReaderWriters mit zentralen Funktionen)
	 */
	protected void createToolBar() {

		this.bar = new JToolBar();
		
		// Add/Copy/Paste-Buttons.
		this.bar.add(this.textComp.getActionMap().get(DefaultEditorKit.cutAction)).setText("");
		this.bar.add(this.textComp.getActionMap().get(DefaultEditorKit.copyAction)).setText("");
		this.bar.add(this.textComp.getActionMap().get(DefaultEditorKit.pasteAction)).setText("");

		// Separator
		this.bar.addSeparator();

		// Schriftformatierungen
		this.bar.add(this.textComp.getActionMap().get("font-bold")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-italic")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-underline")).setText("");

		// Separator
		this.bar.addSeparator();

		// Schriftgroessen
		this.bar.add(this.textComp.getActionMap().get("font-size-10")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-size-12")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-size-14")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-size-16")).setText("");
		this.bar.add(this.textComp.getActionMap().get("font-size-24")).setText("");
	}
 
Deine Frage ist mir nicht ganz klar. Du kannst einerseits einen HTML Editor programmieren, weißt andererseits aber nicht wie man einen Listener zu einem Knopf hinzufügt?

Oder steh ich einfach nur auf dem Schlauch?

Ansonsten:
Code:
JButton button = new JButton("Knopf");
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
// mache irgendetwas
}
});
 
Das ist mir natürlich klar, sonst würde das ganze programm um den Editor herum ja nicht funktionieren.

Meine Frage ist, wie ich im konkreten Fall auf die Buttons der Toolbar eine Aktion legen kann. Ich sehe nämlich in diesem Code keine Buttons.

Es hängt wohl irgendwie mit dieser "Action"-Klasse zusammen.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben