JavaFX 8 Edit table cell on click of placeholder button - javafx

In my TableView I'm using a button as the placeholder when there are no items available in the table. And on click of this button I want to add a new row and then make the first cell in this row editable. I was able to successfully add new row, but the making the first cell editable does not work. I have tried using Platform.runLater to start the editing, but it doesn't work either. Below is the code for what I'm trying to do. Any help would be appreciated. Thanks
public class TableEditTest extends Application {
#Override
public void start(Stage primaryStage) {
TableColumn<Person, String> colA = new TableColumn<>("Name");
colA.setCellFactory(TextFieldTableCell.forTableColumn());
colA.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> colB = new TableColumn<>("Age");
colB.setCellFactory(TextFieldTableCell.forTableColumn());
colB.setCellValueFactory(new PropertyValueFactory<>("age"));
TableColumn<Person, String> colC = new TableColumn<>("Gender");
colC.setCellValueFactory(new PropertyValueFactory<>("gender"));
colC.setCellFactory(TextFieldTableCell.forTableColumn());
TableView<Person> tableView = new TableView<>();
tableView.setEditable(true);
tableView.getColumns().addAll(colA, colB, colC);
Button btn = new Button();
btn.setText("Add Item");
btn.setOnAction((ActionEvent event) -> {
tableView.getItems().add(new Person());
// Edit the first cell
Platform.runLater(() -> {
tableView.edit(0, colA);
});
});
tableView.setPlaceholder(btn);
StackPane root = new StackPane();
root.getChildren().add(tableView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

Open Image From FileChooser On a Button

enter image description hereI need help I want to open a Image with a button of browse using filechooser on javafx ? how can I do it?
FileChooser f;
File file;
Image img;
ImageView mv;
#Override
public void start(Stage primaryStage)
{
f = new FileChooser();
Button browse = new Button("Browse");
browse.setOnAction((event) ->
{
file = f.showOpenDialog(primaryStage);
img = new Image(file.toURI().toString());
mv = new ImageView(img);
});
mv.setImage(img);
BorderPane root = new BorderPane();
root.setTop(browse);
root.setCenter(mv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
The start method sets up the scene and shows the window. It completes before the button handler is run and the information you get during it's execution is not available at this time. You need to do modify the scene with the image from the event handler for this reason:
#Override
public void start(Stage primaryStage) {
final FileChooser f = new FileChooser();
final ImageView mv = new ImageView(); // create empty ImageView
Button browse = new Button("Browse");
browse.setOnAction((event) -> {
File file = f.showOpenDialog(primaryStage);
if (file != null) { // only proceed, if file was chosen
Image img = new Image(file.toURI().toString());
mv.setImage(img);
}
});
BorderPane root = new BorderPane();
root.setTop(browse);
root.setCenter(mv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Alternatively you could create the ImageView in the event handler and place it in the scene:
#Override
public void start(Stage primaryStage) {
final FileChooser f = new FileChooser();
Button browse = new Button("Browse");
final BorderPane root = new BorderPane();
browse.setOnAction((event) -> {
File file = f.showOpenDialog(primaryStage);
if (file != null) { // only proceed, if file was chosen
Image img = new Image(file.toURI().toString());
ImageView mv = new ImageView(img);
root.setCenter(mv); // add ImageView to scene
}
});
root.setTop(browse);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

Some nodes won't appear in HBox

I am working on a Yahtzee game just to develop my programming skills and I can't seem to get all the Labels and Radio Buttons to appear in the HBox. Here is all the relevant code:
public class Main extends Application{
Label diceRoll1 = new Label("1");
Label diceRoll2 = new Label("2");
Label diceRoll3 = new Label("3");
Label diceRoll4 = new Label("4");
Label diceRoll5 = new Label("5");
RadioButton holdRButton1 = new RadioButton("Hold");
RadioButton holdRButton2 = new RadioButton("Hold");
RadioButton holdRButton3 = new RadioButton("Hold");
RadioButton holdRButton4 = new RadioButton("Hold");
RadioButton holdRButton5 = new RadioButton("Hold");
Button rollDice = new Button("Roll!");
Stage window;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage)throws Exception{
VBox diceRoll1Layout = new VBox(5);
diceRoll1Layout.getChildren().addAll(diceRoll1, holdRButton1);
VBox diceRoll2Layout = new VBox(5);
diceRoll2Layout.getChildren().addAll(diceRoll2, holdRButton2);
VBox diceRoll3Layout = new VBox(5);
diceRoll3Layout.getChildren().addAll(diceRoll3, holdRButton3);
VBox diceRoll4Layout = new VBox(5);
diceRoll4Layout.getChildren().addAll(diceRoll4, holdRButton4);
VBox diceRoll5Layout = new VBox(5);
diceRoll5Layout.getChildren().addAll(diceRoll5, holdRButton5);
HBox diceRolls = new HBox(5);
diceRolls.getChildren().addAll(diceRoll1Layout,diceRoll2Layout,diceRoll3Layout,
diceRoll4Layout,diceRoll5Layout, rollDice);
BorderPane rootPane = new BorderPane();
rootPane.setBottom(diceRolls);
Scene scene = new Scene(rootPane, 400, 500);
window = primaryStage;
window.setTitle("Yahtzee!");
window.setScene(scene);
window.show();
}
}
But for some reason whenever I run the code only diceRoll1, diceRoll2, holdRButton1, holdRButton2 and rollDice appear in the window. They're all coded the same so I don't know why they don't all appear. I tried setting them to visible and it made no difference. Am I missing something?

javafx TabPane show search field in the default dropddown showing hidden tabs

How to set search field for the default drop down on the tab pane in JavaFX ,which shows hidden tab list. and here is my code for tabs on the Tabpane
public void start(Stage primaryStage) {
primaryStage.setTitle("Tabs");
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);
TabPane tabPane = new TabPane();
BorderPane borderPane = new BorderPane();
for (int i = 0; i < 10; i++) {
Tab tab = new Tab();
tab.setText("Tab" + i);
HBox hbox = new HBox();
hbox.getChildren().add(new Label("Tab" + i));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);
tabPane.getTabs().add(tab);
}
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabPane);
root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}

How can the text of the label be shown?

As you can see in the picture, the label in the third box, next to the ComboBox, has the text '...' instead of 'fase'.
I have previously attempted to increase the size of the HBox in which it resides, increasing spacing, but to no avail. Any suggestions?
public void start(Stage primaryStage) {
RadioButtonCustom inhoud = new RadioButtonCustom();
Fieldset fs = new Fieldset("eerste", inhoud);
RadioButtonCustom inhoud2 = new RadioButtonCustom();
Fieldset fs2 = new Fieldset("tweede", inhoud2);
//error
HBox hb = new HBox();
hb.minWidth(200);
Label fase = new Label("fase");
fase.setText("Fase");
fase.minWidth(500);
fase.getStyleClass().add("bordered-titled-title");
StackPane.setAlignment(fase, Pos.CENTER);
ComboBox<Integer> cb = new ComboBox<>();
hb.setSpacing(40);
for (Integer i : array) {
cb.getItems().add(i);
}
hb.getChildren().addAll(fase,cb);
Fieldset fs3 = new Fieldset("fase",hb);
// einde error
VBox vb = new VBox();
vb.getChildren().addAll(fs, fs2,fs3);
vb.setSpacing(20);
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!");
}
});
BorderPane root = new BorderPane();
root.getChildren().add(vb);
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("/lissa/stijl.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

How to disable Button when TextField is empty?

In the following code I have a TextField and a Button. I need to disable the Button when ever the TextField is empty, so that I can avoid entering empty values to the database. How can I make the button disabled ?
private VBox addVBox() {
VBox vb1 = new VBox();
vb1.setPadding(new Insets(15, 20, 25, 20));
vb1.setSpacing(15);
vb1.setStyle("-fx-background-color: #333333;");
final Label label = new Label("Staff Details");
label.setFont(Font.font("Arial", FontWeight.BOLD, 20));
label.setTextFill(Color.WHITE);
TableColumn sub = new TableColumn("Staff Name");
sub.setMinWidth(400);
sub.setCellValueFactory(
new PropertyValueFactory<Staff, String>("subName"));
table.setItems(data);
table.getColumns().addAll(sub);
addSubName = new TextField();
addSubName.setPromptText("Staff Name");
addSubName.setPrefSize(200, 30);
final Button b2 = new Button("Add");
b2.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
b2.setPrefSize(70, 30);
b2.setStyle(" -fx-base: #0066ff;");
b2.setTextFill(Color.BLACK);
b2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
msg = addSubName.getText();
try {
enterStaff();
} catch ( ClassNotFoundException | SQLException ex) {
Logger.getLogger(AddStaff.class.getName()).log(Level.SEVERE, null, ex);
}
data.add(new Staff(addSubName.getText()));
addSubName.clear();
}
});
hb.getChildren().addAll(addSubName, b2);
hb.setSpacing(5);
vb1.getChildren().addAll(label, table, hb);
return vb1;
}
Similar to Uluk's answer, but using the Bindings fluent API:
btn.disableProperty().bind(
Bindings.isEmpty(textField1.textProperty())
.and(Bindings.isEmpty(textField2.textProperty()))
.and(Bindings.isEmpty(textField3.textProperty()))
);
Also note that, this new overloaded method of Bindings is added in JavaFX-8 and not avaliable in JavaFX-2.
The other way can be using bindings:
final TextField textField1 = new TextField();
final TextField textField2 = new TextField();
final TextField textField3 = new TextField();
BooleanBinding bb = new BooleanBinding() {
{
super.bind(textField1.textProperty(),
textField2.textProperty(),
textField3.textProperty());
}
#Override
protected boolean computeValue() {
return (textField1.getText().isEmpty()
&& textField2.getText().isEmpty()
&& textField3.getText().isEmpty());
}
};
Button btn = new Button("Button");
btn.disableProperty().bind(bb);
VBox vBox = new VBox();
vBox.getChildren().addAll(textField1, textField2, textField3, btn);
use textProperty() Listener for TextField
try this...
Button b1 = new Button("DELETE");
b1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
b1.setPrefSize(100, 30);
b1.setStyle(" -fx-base: #ffffff;");
b1.setTextFill(Color.BLACK);
b1.setDisable(true); // Initally text box was empty so button was disable
txt1.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> ov, String t, String t1) {
//System.out.println(t+"====="+t1);
if(t1.equals(""))
b1.setDisable(true);
else
b1.setDisable(false);
}
});

Resources