JavaFX - Turning off possible focus on TextArea - javafx

I have TextArea and TextField in my app. I have managed to give focus to TextField on the start and made TextArea impossible to edit. I want to also somehow turn off possibility to focus it with forexample mouse click or TAB cycling.
Is there any suitable way to do it?

You need to use:
textArea.setFocusTraversable(false);
textArea.setMouseTransparent(true);
Example demonstration :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* #author StackOverFlow
*
*/
public class Sample2 extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = new BorderPane();
// Label
TextArea textArea1 = new TextArea("I am the focus owner");
textArea1.setPrefSize(100, 50);
// Area
TextArea textArea2 = new TextArea("Can't be focused ");
textArea2.setFocusTraversable(false);
textArea2.setMouseTransparent(true);
textArea2.setEditable(false);
// Add the items
pane.setLeft(textArea1);
pane.setRight(textArea2);
// Scene
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
// Show stage
primaryStage.show();
}
/**
* Application Main Method
*
* #param args
*/
public static void main(String[] args) {
launch(args);
}

Related

How to chain various MouseEvent on 1 click?

On javafx, I have a GridPane with buttons. Each button has a number on them that increases when a leftclick on the button happens and it decreases when a rightclick happens.
I want to know how can I make it so that when the number of one button reaches a certain value (5 for example), then the mouseEvent activates on the buttons that surround the button.
Also if one of those buttons reaches that certain value too, then the mouseEvent activates on its surrounding buttons as well and so on and so forth.
For increasing, decreasing and showing the number of the buttons, I wrap the Button class in an inner class called NumberButton.
Here is my code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
BorderPane root= new BorderPane();
GridPane grid= new GridPane();
class NumberButton extends Button{
private int innerNumber;
public NumberButton(){
this.innerNumber=0;
showNumber();
}
public void showNumber(){
this.setText(String.valueOf(innerNumber));
}
public void increaseInnerNumber(){
this.innerNumber++;
showNumber();
}
public void decreaseInnerNumber(){
this.innerNumber--;
showNumber();
}
public int getInnerNumber() {
return this.innerNumber;
}
}
#Override
public void start(Stage primaryStage) throws Exception{
NumberButton[][] buttons= new NumberButton[10][10];
for (int i=0; i<buttons.length;i++){
for (int j=0; j<buttons[i].length;j++){
buttons[i][j]= new NumberButton();
NumberButton button= buttons[i][j];
button.setOnMouseClicked(mouseEvent -> {
if (mouseEvent.getButton()== MouseButton.PRIMARY){
button.increaseInnerNumber();
if (button.getInnerNumber()==5){
/*all surrounding buttons activate the mouseEvent
* and mouseEvent.getButton()== MouseButton.PRIMARY is true */
}
}else if (mouseEvent.getButton()==MouseButton.SECONDARY){
button.decreaseInnerNumber();
}
});
button.setMinSize(30,30);
button.setMaxSize(30,30);
grid.add(button,i,j);
}
}
root.setCenter(grid);
primaryStage.setTitle("Number Buttons!");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I have tried using button.setOnAction() but then I don't know how to get if the event was triggered by a leftclick or a rightclick and when I tried using both button.setOnAction() and button.setOnMouseClicked() then the event triggered twice and I don't want that.

Netbeans 9 0 Java FX 10

Try creating a JavaFX application.
Don't add or modify any code.
Run the Project. It does not find the MAIN.
If you run the File, then it works.
However, create JavaFX FXML application. As mentioned earlier, don't make any change to the code.
Run the Package.... it WORKS.
Does anyone know why? I tried to report the problem, but that looked more complicated than writing the code. :)
I am enclosing generated code for a JAVAFX APPLICATION
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forsubmission;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author Hornigold
*/
public class ForSubmission extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Thanks,
Gold

Event Handler in javaFx for menu

I have a question in JavaFX , how can i set an event Handler for menu (not menu bar or menu item) that when i clicked on the menu a popup window appear. i have tried this but when i click on menu nothing happens:
settingsMenu.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> {//To DO});
and even the code below doesn't works:
settingMenu.setOnAction(event -> {//To Do});
Here is a hack. Menu has a constructor Menu(String text, Node graphic). Set the String to empty-string and the Node to Label. Then add a MouseListener to the Label.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author Sedrick
*/
public class JavaFXApplication33 extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Help!");
label.setOnMouseClicked(mouseEvent->{System.out.println("Hello World!");});
Menu menu = new Menu("", label);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(menu);
StackPane root = new StackPane();
root.getChildren().add(menuBar);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Javafx add a dropdown event listener for choicebox

I have a ChoiceBox and I want to refresh it's content whenever user expands it. I haven't found a proper listener for this. All the stuff google gives is related to handling ChangeValue events.
I reckon I should add eventListener<ActionEvent> to ChoiceBox since what I'm handling is click on a ChoiceBox, but my implementation doesn't work.
ActionEvent fires when I click on any List value, not when I click ChoiceBox itself.
Register a listener with the choice box's showingProperty:
choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> {
if (isNowShowing) {
// choice box popup is now displayed
} else {
// choice box popup is now hidden
}
});
Here is a quick demo:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ChoiceBoxPopupTest extends Application {
private int nextValue ;
#Override
public void start(Stage primaryStage) {
ChoiceBox<Integer> choiceBox = new ChoiceBox<>();
choiceBox.getItems().add(nextValue);
choiceBox.setValue(nextValue);
choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> {
if (isNowShowing) {
choiceBox.getItems().setAll(++nextValue, ++nextValue, ++nextValue);
}
});
BorderPane root = new BorderPane();
root.setTop(choiceBox);
BorderPane.setAlignment(choiceBox, Pos.CENTER);
root.setPadding(new Insets(5));
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

How to set the ellipsis string to an empty string if it is the only text displayed in a JavaFX Label

I'm trying to keep the Elipsis String (...) on my Label (lbl) but I want it to be set to an empty String when the window gets too small. I'm using the code here to show an example. I'm using the ResizeHelper class found here: Allow user to resize an undecorated Stage.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* #author bparmeter
*/
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) {
Label lbl = new Label();
lbl.setText("Hello World");
AnchorPane root = new AnchorPane();
AnchorPane.setBottomAnchor(lbl, 0.0);
AnchorPane.setTopAnchor(lbl, 0.0);
AnchorPane.setRightAnchor(lbl, 0.0);
AnchorPane.setLeftAnchor(lbl, 0.0);
root.getChildren().add(lbl);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
ResizeHelper.addResizeListener(primaryStage);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Not the best solution but it works...
lbl.widthProperty().addListener((ov, o, n) -> {
final Text t = new Text(lbl.getText().charAt(0)+"...");
t.applyCss();
final double width = t.getLayoutBounds().getWidth();
lbl.setEllipsisString(width>=lbl.getWidth() ? "" : "...");
});

Resources