0

I issue I am trying to get rid off is the following:

I intend to setup a costum event handling chain as a workaround for JavaFX's lack of actioncommands. The issue in particular is, that a menuitem upon clicking it, still fires an ActionEvent instead of the self-written MilvaLabActionEvent. The code:

Event class

package jpt.gui.items;

import javafx.event.ActionEvent;

public class MilvaLabActionEvent extends ActionEvent {
    private static final long serialVersionUID = 6757067652205246280L;

    private String actionCommand ="";

    public MilvaLabActionEvent(String actionCommand2) {
        setActionCommand(actionCommand2);
    }

    public MilvaLabActionEvent() {}

    public String getActionCommand() {
        return actionCommand;
    }

    public void setActionCommand(String actioncommand) {
        this.actionCommand = actioncommand;
    }
}

My EventHandler:

package jpt.gui.items;

import javafx.event.EventHandler;
import jpt.MilvaLabGlobal;
import jpt.MilvaLabKonst;
import jpt.handle.MilvaLabDateiHandle;
import jpt.handle.MilvaLabEinHandle;
import jpt.handle.MilvaLabHilfeHandle;
import jpt.handle.MilvaLabMilvaHandle;
import jpt.handle.MilvaLabRvAnwendungHandle;
import jpt.handle.MilvaLabrvTextHandle;
import jpt.log4j.MilvaLabLogger;

public class MilvaLabEventHandler implements EventHandler<MilvaLabActionEvent>{

    @Override
    public void handle(MilvaLabActionEvent event) {     
        // the command string of the menu item
        final String sCmd = event.getActionCommand();

        if (sCmd.charAt(0) == 'M')
        {//doing something here
        }
}

The costum MenuItem-Class I figured out I gotta write.

package jpt.gui.items;

import javafx.event.Event;
import javafx.scene.control.MenuItem;

public class MilvaLabMenuItem extends MenuItem {
    private String actionCommand;

    public MilvaLabMenuItem(String sText) {
        this.setText(sText);
    }
    @Override
    public void fire() {
        Event.fireEvent(this, new MilvaLabActionEvent(getActionCommand()));

    }

    public String getActionCommand() {
        return actionCommand;
    }
    public void setActionCommand(String actionCommand) {
        this.actionCommand = actionCommand;
    }
}

And the initialization of the costum MenuItem:

final MilvaLabMenuItem jmi = new MilvaLabMenuItem("I am a menuItem");
        jmi.addEventHandler(evtype, new MilvaLabEventHandler());
        jmi.setOnAction((event) -> {
            System.out.print("I have fired an ActionEvent!");
        });

Well, as of now, I got "I have fired an ActionEvent" when I click on the MilvaLabMenuItem, nothing else happens. (Looked into that thing already using the debugger). What I want to happen is that, obviously, the MilvaLabEventHandler is called.

M. Klingert
  • 35
  • 1
  • 6

1 Answers1

0

I figured it out again. I declared two EventTypes, though, only one was necessary.

This helped me finding the solution, though, they use Nodes instead of MenuItems. How to emit and handle custom events?

M. Klingert
  • 35
  • 1
  • 6