TextArea does not handle MouseEvent.MOUSE_PRESSED - css

I am building a JavaFX application and I have a TextArea inserted.
The TextArea has a CSS class assigned (don't know if it matters):
.default-cursor{
-fx-background-color:#EEEEEE;
-fx-cursor:default;
}
There are 2 issues about this TextArea:
-fx-cursor:default; Has no effect as the cursor remains the text cursor. That is weird as i use the same class for a TextField with proper/expected results
The TextArea does not handle MOUSE_PRESSED eventMy code is :
textArea.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("print message");
}
});
Any ideas why?
I want to note that when I changed EventHandler to handle MOUSE_CLICKED everything is fine

I suspect the default handlers for mouse events on the TextArea are consuming the mouse pressed event before it gets to your handler.
Install an EventFilter instead:
textArea.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("mouse pressed");
}
});
The Event filter will get processed before the default handlers see the event.
For your css issue, try
.default-cursor .content {
-fx-cursor: default ;
}

Related

javaFX focusHandler?

I am just changing from AWT to JavaFX and im wondering how to work with focus.
For Exampe: In AWT I wrote something like that:
Button bFocus = new Button("Focus");
bFocus.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.out.println("Having the Focus");
}
public void focusLost(FocusEvent e) {
System.out.println("Lost the Focus");
}
});
But how does it work in JavaFX? I tried many different things, but that doesnt work...
JavaFX has an API that defines observable properties with which you can register listeners and respond when they change. Almost all state that belongs to UI elements in JavaFX is represented by these properties, allowing you to register a listener that responds when they change.
So, for example, the superclass of all UI elements, Node has a ReadOnlyBooleanProperty called focused, with which you can register a listener:
Button bFocus = new Button("Focus");
bFocus.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
if (isNowFocused) {
System.out.println("Having the Focus");
} else {
System.out.println("Lost the Focus");
}
});
I thought it might be helpful to see an example which specifies the ChangeListener as an anonymous inner class like James_D mention here.
TextField yourTextField = new TextField();
yourTextField.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
}
else
{
System.out.println("Textfield out focus");
}
}
});
I hope this answer is helpful!

how to make JFXPanel to get focus if clicked anywhere on it not on children

I have JFXPanel with a textbox and few buttons on it. I want to lost focus of textbox if user click on anywhere other than child controls.
Here is my working example:
override the mouse click event of JFXPanel as follows:
jfxPanel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(!toolbarJFX.getScene().getFocusOwner().isPressed()) {
toolbarJFX.getScene().getRoot().requestFocus();
}
}
});

Overlaying elements mouse listener ScalaFx/JavaFx execution

I have the following problem in my ScalaFX Application. I have a Label in a VBox. Both have a onMouseClicked listener assigned, as can be seen in my example code. When clicking on the inside label, both handlers are executed. This is not the behavior I want to force. I only want the labels listener to be executed.
Example-Code
new VBox{
content add new Label {
text = "inside label"
onMouseClicked = (me : MouseEvent) => println("Execute just me!")
}
onMouseClicked = (me : MouseEvent) => println("Do not execute when label is clicked!")
}
Is there an easy way to stop the VBox handler from being executed when clicking the Label?
You need to consume the event. The following code works in JavaFX:
class TestPane extends Pane {
private Label label;
private VBox vbox;
public TestPane() {
label = new Label();
label.setText("Waiting...");
vbox = new VBox();
vbox.getChildren().add(label);
getChildren().add(vbox);
label.setOnMouseClicked(new EventHandler<Event>() {
#Override
public void handle(Event event) {
System.out.println("label event");
event.consume();
}
});
vbox.setOnMouseClicked(new EventHandler<Event>() {
#Override
public void handle(Event event) {
System.out.println("vbox event");
}
});
}
}
The event handling chain is well defined:
There's more information about how to manipulate the dispatching and handling of events on this page: Oracle Tutorial: Handling JavaFX Events

JavaFX opposite function of .requestFocus()?

I'm looking for function in JavaFX, where I can "disable or dismiss" the requested focus.
Here is a screenshot of my program: Screenshot
Every cell is filled with an Eventhandler (onMouseEntered and onMouseExited) and in every onMouseEntered function I have to request the focus like this:
label.requestFocus(). I need to do that, because I'm using KeyEvents to change the content of the cell.
It works fine, but there is a bug: When I move out of the scrollPane, there is still the requested focus on the last entered cell.
How can I solve that issue, without requesting focus for everything around the Scrollpane to fix this bug? Is there a function, where I can dismiss the requested focus, so after exiting a cell, it'll dismiss the focus.
Best regards,
My code:
arbeitetLabel.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
arbeitetLabel.requestFocus();
arbeitetLabel.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
// Do some keyEvent Stuff
}
});
}
});
arbeitetLabel.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
// Dismiss the requested Focus here?
}
});
I'm passing the focus to the parent, that's it!
arbeitetLabel.getParent().requestFocus()

Change label during button execution

I have a "Connect" button which calls internal logic for network connection
I have this button which starts network connection.
Button connectButton = new Button("Connect");
connectButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
actiontarget.setText("Successful Connection Test!");
}
});
How I can change the button label during the action execution with label "Cancel"?
And also how I can cancel the action when the button label is "Cancel"? Maybe I need to call specific action when the button label is different?
You could extend your code snippet with the logic below. Haven't tried it but I think it should work, even I would suggest using Injection.
private final String CONNECT = "connect";
private final String DISCONNECT = "disconnect";
Button connectButton = new Button(CONNECT);
connectButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e)
{
if (connectButton.getText().equals(CONNECT)) {
do_something();
actiontarget.setText(DISCONNECT);
} else {
do_something_else();
actiontarget.setText(CONNECT);
}
}
});
Another Idea would be to use a ToggleButton:
In your FXML:
<ToggleButton fx:id="btnConnect" alignment="CENTER" maxHeight="1" maxWidth="1" onAction="#actionClickedConnectBtn" text="connect"/>
In your Code:
#FXML
public void actionClickedConnectBtn(ActionEvent event) {
if (btnConnectGpsd.isSelected()) {
do_something();
} else {
do_something_else();
}
}
You can use a Boolean flag to indicate which action to execute when the button is clicked; if the flag is true, then execute your cancel action and set the flag to false, setting your button's text to "Connect". If the flag is instead false, execute your connection action and set the flag to true, setting your button's text to "Cancel".

Resources