JavaFX - Mouse Event not triggering - java

Here is my code :
Button start = new Button("Start");
start.setPrefHeight(100d);
start.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent arg0) {
//ss.startServer();
System.out.println("adfasddasfrwgafsdasdf");
}
});
The message does not get printed on the console at eclipse! I want the event to trigger when the mouse is released. What am I doing wrong?
Upon furthet investagation I realised that if I have another mouseReleased event on another node , it will fire more events!... why is that?
For example , here is the code I am using, when I press the start button , it prints Start and Reload!
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent arg0) {
ss.startServer();
System.out.println("Start");
arg0.consume();
}
});
Button stop = new Button("Stop");
stop.setPrefHeight(100d);
stop.setMaxWidth(Double.MAX_VALUE);
stop.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
sc.stopServer();
System.out.println("Stop");
event.consume();
}
});
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent some) {
sc.reloadServer();
System.out.println("Reload");
some.consume();
}
});
Stop button works properly!
Anyway , if the other piece of code works , here it is(http://pastebin.com/4f55R2gN)

You typo'd your code. You set your MouseReleased handler on the start object instead of reload.
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){

Try this
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("button click and mouse released");
}
});
The below works for me fine.
public class FxmlSample extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws IOException{
JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
Pane pane = new Pane();
VBox vBox = new VBox();
Button button = new Button("Click me");
button.setMinSize(50.0, 50.0);
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("11111111111111");
}
});
Button button_2 = new Button("Click me 2");
button_2.setMinSize(50.0, 50.0);
button_2.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("2222222222222");
}
});
vBox.setPadding(new Insets(20));
vBox.getChildren().addAll(button, button_2);
pane.getChildren().addAll(vBox);
Scene scene = new Scene(pane);
jfxPanel.setScene(scene);
frame.add(jfxPanel);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(700, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Related

Hand Cursor when hover on a button in Java AWT

I've created a button in AWT with the name "Reset". I want the cursor to be hand cursor when the mouse is hovered on this button.
I tried the mouseEntered method of the MouseAdapter class but no effect.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
#Override
public void mouseEntered(MouseEvent e) {
Cursor.getPredefinedCursor(HAND_CURSOR);
}
});
}
Thanks in advance.
Your statement Cursor.getPredefinedCursor(HAND_CURSOR);
in your mouseEntered method had no effect,
because you only got the cursor, but then did nothing with it.
The solution is simpler than you might have expected.
You don't need your mouseEntered method.
Just use the setCursor(Cursor) method of class Componenton your resetButton.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
});
}
Then AWT will do the rest for you: showing the hand cursor when the mouse
enters the resetButton, and showing the normal cursor when leaving it.
I got this done this way after few hits and trials:
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
#Override
public void mouseEntered(MouseEvent e) {
resetButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
});
}

JavaFx Spreadsheet Cell Right Click Open Dialog

I am new to SpreadSheet functionality of ControlsFx Api. I would like to open Dialog on right click of Spreadsheetcell of SpreadsheetView in Javafx. Any help is greatly appreciated.
this is code where you can off the standard ContextMenu and implements own handler with Dialog, in this example TextInputDialog:
SpreadsheetView spreadsheetView = new SpreadsheetView();
//off the standard ContextMenu
spreadsheetView.setContextMenu(null);
//set own handler for right click with Dialog
spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override public void handle(ContextMenuEvent event) {
CellView cellView = (CellView) event.getTarget();
TextInputDialog dialog = new TextInputDialog(cellView.getText());
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println(cellView.getText());
}
}
});
I don't know very good this library, but it works good.
Example how it works:
My program:
public class MainController extends Application {
public static void main(String[] args) {
launch(args);
}
#Override public void start(Stage primaryStage) throws Exception {
SpreadsheetView spreadsheetView = new SpreadsheetView();
//off the standard ContextMenu
spreadsheetView.setContextMenu(null);
//set own handler for right click with Dialog
spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override public void handle(ContextMenuEvent event) {
CellView cellView = (CellView) event.getTarget();
TextInputDialog dialog = new TextInputDialog(cellView.getText());
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println(cellView.getText());
}
}
});
HBox hBox = new HBox();
hBox.getChildren().add(spreadsheetView);
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
It is using mouse handler on the table view which checks when mouse is clicked and on clicking it fires a new dialogue in fx and then accepts the input and updates the fx table view.
table.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 1) {
Call dialogue method of java fx
}
}
});
Or if you want right click you can create cell
Eg
FirstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
#Override
public TableCell<Person, String> call(TableColumn<Person, String> col) {
final TableCell<Person, String> cell = new TableCell<>();
cell.textProperty().bind(cell.itemProperty()); // in general might need to subclass TableCell and override updateItem(...) here
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton == MouseButton.SECONDARY) {
// handle right click on cell...
// access cell data with cell.getItem();
// access row data with (Person)cell.getTableRow().getItem();
}
}
});
return cell ;
}
});

What's the code behind showAndWait in JavaFX?

I developed a small JavaFX application to be deployed in an Android device. Since, Alert is not yet supported in this current version of JavaFXPorts, I made my own implementaion of dialog. My problem is: I do not know how to implement the showAndWait functionality. Can someone know the idea?
Here's the code:
private static int status;
public static int showConfirm(String message){
status = 0;
black = new StackPane();
black.setStyle("-fx-background-color: black; -fx-opacity: 0.7;");
black.setPrefSize(Main.WIDTH, Main.HEIGHT);
Label label = new Label(message);
label.setStyle("-fx-font-size: 22px;");
label.setWrapText(true);
Button okBtn = new Button("OK");
okBtn.getStyleClass().add("terminal-button");
okBtn.setPrefWidth(120);
okBtn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
status = 1;
Dialog.hide();
}
});
Button cancelBtn = new Button("Cancel");
cancelBtn.getStyleClass().add("terminal-button");
cancelBtn.setPrefWidth(120);
cancelBtn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
status = -1;
Dialog.hide();
}
});
HBox btnContainer = new HBox(10);
btnContainer.setAlignment(Pos.CENTER_RIGHT);
btnContainer.getChildren().addAll(okBtn,cancelBtn);
VBox root = new VBox(30);
root.setStyle("-fx-background-color: white;");
root.setPadding(new Insets(20));
root.setPrefWidth(Main.WIDTH - 200);
root.getChildren().addAll(label,btnContainer);
Group container = new Group();
container.getChildren().add(root);
Main.PAGE.getChildren().add(black);
Main.PAGE.getChildren().add(container);
return status;
}

Javafx: how to fill a TilePane with circles and add an event handler to each one

I tryed with this code but it is not working:
public class Scacchiera extends Application {
TilePane root;
#Override
public void start(Stage primaryStage) {
root = new TilePane();
root.setPrefColumns(4);
root.setPrefRows(4);
for (int i = 0; i < 16; i++) {
Circle c = new Circle(i);
c.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Circle clicked");
}
});
root.getChildren().add(c);
}
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Why? Can you help me? I am not sure what is the problem but may be my events handler dies with the for cicle..
Circles are just shapes, they don't have actions like controls.
What you want to do is is handle an onMouseClicked event to your circle:
// java 8
circle.setOnMouseClicked(mouseEvent -> System.out.println("Mouse Clicked!"));

Switching fxml scenes with setScene hangs on Mac

We have a JDialog that contains a single stage with multiple scenes. Each scene has a next button. When the user clicks "next" we call stage.setScene. This works fine on Windows and Linux, but on Mac, setScene never returns. The application hangs, and it appears to be a thread deadlock. Here is a sample app that reproduces the problem. We have also tried various java 7 builds and java8 pre-release. We think we have found a couple workarounds, but we would like to understand why this code has problems on Mac. Sample app with bug:
public class SampleFxInsideSwing
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SampleFxInsideSwing().initFX();
}
});
}
private void initFX()
{
JDialog dialog = new JDialog();
dialog.setSize(new Dimension(500,500));
final JFXPanel stage = new JFXPanel();
dialog.add(stage);
Platform.runLater(new Runnable()
{
#Override
public void run()
{
Button nextButton = new Button("Next");
nextButton.setOnAction(new EventHandler<ActionEvent>()
{
//#Override
public void handle(ActionEvent event)
{
try
{
System.out.println("Clicked");
AnchorPane parent = FXMLLoader.load(SampleFxInsideSwing.class.getResource("SampleFxml.fxml"));
Scene secondScene = new Scene(parent);
stage.setScene(secondScene);
System.out.println("Displaying second scene!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
Group group = new Group();
group.getChildren().add(nextButton);
Scene scene1 = new Scene(group);
stage.setScene(scene1);
}
});
dialog.setVisible(true);
}
}

Categories

Resources