I'm making a button which opens a new window containing a gridview of members, which shows their name and rank. A member can be clicked to be set as currentMember. However my JavaFX knowledge is limited and I have no idea how to create this temporary window without changing the current controller. I'm not sure if what I have is a good way of doing it. Am I doing something wrong or is this the correct way of doing it?
This code gets run when the Select Person button is clicked
#FXML
private void setLid(ActionEvent event) {
Stage stage = new Stage();
VBox box = new VBox();
box.setPadding(new Insets(10));
TableView<Persoon> tablePers = new TableView<>();
TableColumn<Persoon, String> voornaam = new TableColumn<>();
TableColumn<Persoon, String> achternaam = new TableColumn<>();
TableColumn<Persoon, String> graad = new TableColumn<>();
voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());
Label label = new Label("Selecteer een persoon");
Button btnSelectCurrentLid = new Button();
btnSelectCurrentLid.setText("Bevestigen");
btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
geselecteerdePersoon = (tablePers.getSelectionModel().getSelectedItem());
stage.close();
});
box.getChildren().add(label);
box.getChildren().add(tablePers);
box.getChildren().add(btnSelectCurrentLid);
Scene scene = new Scene(box, 250, 150);
stage.setScene(scene);
stage.show();
}
Here is a one-file mcve (copy paste the entire code into FxMain.java and run):
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FxMain extends Application {
private Persoon geselecteerdePersoon;
#Override
public void start(Stage primaryStage) throws Exception{
Label selectedPersonInfo = new Label();
Button start = new Button("Show Tabel");
start.setOnAction(e-> {
new LoginDialog().showAndWait();
selectedPersonInfo.setText("Selected: " +geselecteerdePersoon.voornaamProperty().get()
+ " " +geselecteerdePersoon.achternaamProperty().get());
});
BorderPane root = new BorderPane(selectedPersonInfo);
root.setBottom(start);
primaryStage.setScene(new Scene(root, 300,300));
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
class LoginDialog extends Dialog {
public LoginDialog() {
VBox box = new VBox();
box.setPadding(new Insets(10));
TableView<Persoon> tablePers = new TableView<>();
TableColumn<Persoon, String> voornaam = new TableColumn<>("Name");
TableColumn<Persoon, String> achternaam = new TableColumn<>("Last Name");
TableColumn<Persoon, String> graad = new TableColumn<>("Grade");
voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());
tablePers.getColumns().addAll(voornaam, achternaam, graad);
tablePers.getItems().add(new Persoon("Alice", "Bee","70"));
tablePers.getItems().add(new Persoon("Charly", "Din","32"));
Label label = new Label("Selecteer een persoon");
Button btnSelectCurrentLid = new Button();
btnSelectCurrentLid.setText("Bevestigen");
btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
geselecteerdePersoon = tablePers.getSelectionModel().getSelectedItem();
close();
});
box.getChildren().addAll(label, tablePers, btnSelectCurrentLid);
getDialogPane().setContent(box);
getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL);
}
}
}
class Persoon {
private final SimpleStringProperty lName;
private final SimpleStringProperty fName;
private final SimpleStringProperty grade;
public Persoon(String fName, String lName, String grade) {
this.fName = new SimpleStringProperty(fName);
this.lName = new SimpleStringProperty(lName);
this.grade = new SimpleStringProperty(grade);
}
public final StringProperty achternaamProperty() { return lName; }
public final StringProperty voornaamProperty() { return fName; }
public final StringProperty graadProperty() { return grade; }
}
Related
I know how to copy an entire cell in JavaFX's tableview, but is there a way I can only copy PART of the text in a single cell?
For example if I have the following in a cell:
1. apples
2. oranges
3. bananas
I want to be able to copy "2. oranges" from the cell without copying the entire text contents list. Currently if I click on a cell it highlights and copies the entire text.
One of the things I like to do is create options in the right-click context menu.
Key code:
//Use setRowFactory to set up the ContextMenu
table.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>()
{
#Override
public TableRow<Person> call(TableView<Person> tableView)
{
final TableRow<Person> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem copyItem = new MenuItem("copy index and first name");
copyItem.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
Person tempPerson = row.getItem();
//Put the row index and first name in clipboard
content.putString(row.getIndex() + ". " + tempPerson.getFirstName());
clipboard.setContent(content);
}
});
contextMenu.getItems().add(copyItem);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu)
);
return row;
}
});
Full Code - Altered code from: https://gist.github.com/james-d/7758918
Main Class
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSample extends Application
{
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data
= FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith#example.com"),
new Person("Isabella", "Johnson", "isabella.johnson#example.com"),
new Person("Ethan", "Williams", "ethan.williams#example.com"),
new Person("Emma", "Jones", "emma.jones#example.com"),
new Person("Michael", "Brown", "michael.brown#example.com")
);
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage stage)
{
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));
table.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>()
{
#Override
public TableRow<Person> call(TableView<Person> tableView)
{
final TableRow<Person> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem copyItem = new MenuItem("copy index and first name");
copyItem.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
Person tempPerson = row.getItem();
content.putString(row.getIndex() + ". " + tempPerson.getFirstName());
clipboard.setContent(content);
}
});
contextMenu.getItems().add(copyItem);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu)
);
return row;
}
});
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
}
Person Class
/*
* 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 javafxapplication206;
import javafx.beans.property.SimpleStringProperty;
public class Person
{
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
public Person(String fName, String lName, String email)
{
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName()
{
return firstName.get();
}
public void setFirstName(String fName)
{
firstName.set(fName);
}
public String getLastName()
{
return lastName.get();
}
public void setLastName(String fName)
{
lastName.set(fName);
}
public String getEmail()
{
return email.get();
}
public void setEmail(String fName)
{
email.set(fName);
}
}
When I paint the background of a column and then I fixed the size of a cell, this happens when I hidden a column:
If I comment this line: "table.setFixedCellSize(24)" works correctly:
But I need fix the size of a cell... ¿Does this error have any solutions?
Many thanks to all and sorry for my poor English.
You can run my example:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application {
#Override
public void start(final Stage stage) {
// Create Items
final ObservableList<Person> data =
FXCollections.observableArrayList(new Person("Ruben", "Martin"),
new Person("Ruben", "Martin"), new Person("Ruben", "Martin"));
// Create columns
final TableColumn<Person, String> firstNameCol =
new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
final TableColumn<Person, String> lastNameCol =
new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
// Create Table
final TableView<Person> table = new TableView<>();
table.setFixedCellSize(24);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
// Create CheckBox
final CheckBox checkLastName = new CheckBox();
checkLastName.setText("Last Name");
checkLastName.setSelected(true);
lastNameCol.setStyle("-fx-background-color:yellow");
lastNameCol.visibleProperty().
bindBidirectional(checkLastName.selectedProperty());
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(table, checkLastName);
final Scene scene = new Scene(new Group());
stage.setWidth(450);
stage.setHeight(550);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static void main(final String[] args) {
launch(args);
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(final String fName, final String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() {
return this.firstName.get();
}
public void setFirstName(final String fName) {
this.firstName.set(fName);
}
public String getLastName() {
return this.lastName.get();
}
public void setLastName(final String fName) {
this.lastName.set(fName);
}
}
}
Probably this is a workaround but it works:
StringBinding sb = Bindings.when(lastNameCol.visibleProperty())
.then("-fx-background-color:yellow")
.otherwise("");
lastNameCol.styleProperty().bind(sb);
I followed the tutorial about TableView in Oracle docs, and I want to do the same thing, but instead of showing a TextField to modified items, I want to show a RadioButton.
(I created the TableView with RadionButton on it)
I used the tutorial at https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm (see Editing Data in the Table) and extended it. As a basis for the values, which should be set by radio buttons I assume an Enumeration.
In my example I extended the Person class with the enum Participation (indicating whether or not the people of the list attending an fictive event) ...
public static enum Participation {
YES,
NO,
MAYBE;
public String toString() {
return super.toString().toLowerCase();
};
}
...
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private final SimpleObjectProperty<Participation> participation;
...
I implemented a RadioButtonCell, which takes an arbitraty EnumSet<T>. So you can use it for every Enumeration and every TableColumn, which should contain RadioButtons.
public static class RadioButtonCell<S,T extends Enum<T>> extends TableCell<S,T>{
private EnumSet<T> enumeration;
public RadioButtonCell(EnumSet<T> enumeration) {
this.enumeration = enumeration;
}
#Override
protected void updateItem(T item, boolean empty)
{
super.updateItem(item, empty);
if (!empty)
{
// gui setup
HBox hb = new HBox(7);
hb.setAlignment(Pos.CENTER);
final ToggleGroup group = new ToggleGroup();
// create a radio button for each 'element' of the enumeration
for (Enum<T> enumElement : enumeration) {
RadioButton radioButton = new RadioButton(enumElement.toString());
radioButton.setUserData(enumElement);
radioButton.setToggleGroup(group);
hb.getChildren().add(radioButton);
if (enumElement.equals(item)) {
radioButton.setSelected(true);
}
}
// issue events on change of the selected radio button
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#SuppressWarnings("unchecked")
#Override
public void changed(ObservableValue<? extends Toggle> observable,
Toggle oldValue, Toggle newValue) {
getTableView().edit(getIndex(), getTableColumn());
RadioButtonCell.this.commitEdit((T) newValue.getUserData());
}
});
setGraphic(hb);
}
}
}
You now have to adjust the CellFactory of the particular TableColumn
participationColumn.setCellFactory((param) -> new RadioButtonCell<Person, Participation>(EnumSet.allOf(Participation.class)));
Finally update the actual value of your data on a commit as usual:
participationColumn.setCellValueFactory(new PropertyValueFactory<Person, Participation>("participation"));
participationColumn.setOnEditCommit(
new EventHandler<CellEditEvent<Person, Participation>>() {
#Override
public void handle(CellEditEvent<Person, Participation> t) {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setParticipation(t.getNewValue());
}
}
);
gkri, thank you for the nice code! This is very useful. I have one additional remark: Of course the new SimpleObjectProperty needs its get & set methods. Without the getter the table will not properly update, especially when sorting or, with TreeTableView, when expanding or collapsing nodes:
public void setParticipation(Participation p){
participation.set(p);
}
public Participation getParticipation(){
return participation.get();
}
So the full code sample:
import java.util.EnumSet;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class TableViewSample extends Application {
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith#example.com",Participation.MAYBE),
new Person("Isabella", "Johnson", "isabella.johnson#example.com",Participation.MAYBE),
new Person("Ethan", "Williams", "ethan.williams#example.com",Participation.MAYBE),
new Person("Emma", "Jones", "emma.jones#example.com",Participation.MAYBE),
new Person("Michael", "Brown", "michael.brown#example.com",Participation.MAYBE));
final HBox hb = new HBox();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(650);
stage.setHeight(550);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
table.setMinWidth(640);
TableColumn<Person,String> firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
TableColumn<Person,String>lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
TableColumn<Person,String> emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
TableColumn<Person,Participation> participationColumn = new TableColumn("Participation");
participationColumn.setCellFactory((param) -> new RadioButtonCell<Person, Participation>(EnumSet.allOf(Participation.class)));
participationColumn.setCellValueFactory(new PropertyValueFactory<Person, Participation>("participation"));
participationColumn.setOnEditCommit(
new EventHandler<CellEditEvent<Person, Participation>>() {
#Override
public void handle(CellEditEvent<Person, Participation> t) {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setParticipation(t.getNewValue());
}
}
);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, participationColumn );
final TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
final TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setPromptText("Last Name");
final TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setPromptText("Email");
final Button addButton = new Button("Add");
addButton.setOnAction((ActionEvent e) -> {
data.add(new Person(
addFirstName.getText(),
addLastName.getText(),
addEmail.getText(),
Participation.NO
));
addFirstName.clear();
addLastName.clear();
addEmail.clear();
});
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
hb.setSpacing(3);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static enum Participation {
YES,
NO,
MAYBE;
public String toString() {
return super.toString().toLowerCase();
};
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private final SimpleObjectProperty<Participation> participation = new SimpleObjectProperty<Participation>();
private Person(String fName, String lName, String email, Participation p ) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
this.participation.setValue(p);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public void setParticipation(Participation p){
participation.set(p);
}
public Participation getParticipation(){
return participation.get();
}
}
public static class RadioButtonCell<S,T extends Enum<T>> extends TableCell<S,T>{
private EnumSet<T> enumeration;
public RadioButtonCell(EnumSet<T> enumeration) {
this.enumeration = enumeration;
}
#Override
protected void updateItem(T item, boolean empty)
{
super.updateItem(item, empty);
if (!empty)
{
// gui setup
HBox hb = new HBox(7);
hb.setAlignment(Pos.CENTER);
final ToggleGroup group = new ToggleGroup();
// create a radio button for each 'element' of the enumeration
for (Enum<T> enumElement : enumeration) {
RadioButton radioButton = new RadioButton(enumElement.toString());
radioButton.setUserData(enumElement);
radioButton.setToggleGroup(group);
hb.getChildren().add(radioButton);
if (enumElement.equals(item)) {
radioButton.setSelected(true);
}
}
// issue events on change of the selected radio button
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#SuppressWarnings("unchecked")
#Override
public void changed(ObservableValue<? extends Toggle> observable,
Toggle oldValue, Toggle newValue) {
getTableView().edit(getIndex(), getTableColumn());
RadioButtonCell.this.commitEdit((T) newValue.getUserData());
}
});
setGraphic(hb);
}
}
}
}
Simly do it like in the example 12-8 Editing a Table Cell.
Define a EditingCell like there(at the bottom) but simply replace the TextField with a RadioButton.
If you need further assistance, write a comment under this answer.
Happy Coding,
Kalasch
I am working with Table in Java FX. The code is below :
package addsubject;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class AddSubject extends Application {
private TableView<Subject> table = new TableView<Subject>();
private final ObservableList<Subject> data
= FXCollections.observableArrayList(new Subject("Mobile Computing", "5623"));
final HBox hb = new HBox();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Add Subject");
stage.setWidth(700);
stage.setHeight(600);
final Label label = new Label("Subject Details");
label.setFont(new Font("Calibri", 20));
table.setEditable(true);
TableColumn sub = new TableColumn("Subject Name");
sub.setMinWidth(400);
sub.setCellValueFactory(
new PropertyValueFactory<Subject, String>("sub"));
sub.setCellFactory(TextFieldTableCell.forTableColumn());
sub.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Subject, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<Subject, String> t) {
((Subject) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setSubName(t.getNewValue());
}
}
);
TableColumn code = new TableColumn("Subject Code");
code.setMinWidth(150);
code.setCellValueFactory(
new PropertyValueFactory<Subject, String>("code"));
code.setCellFactory(TextFieldTableCell.forTableColumn());
code.setCellFactory(TextFieldTableCell.forTableColumn());
code.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Subject, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<Subject, String> t) {
((Subject) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setSubCode(t.getNewValue());
}
}
);
table.setItems(data);
table.getColumns().addAll(sub, code);
final TextField addSubName = new TextField();
addSubName.setPromptText("Subject Name");
addSubName.setPrefWidth(350);
final TextField addCode = new TextField();
addCode.setPrefWidth(150);
addCode.setPromptText("Subject Code");
final Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
data.add(new Subject(
addSubName.getText(),
addCode.getText()));
addSubName.clear();
addCode.clear();
}
});
hb.getChildren().addAll(addSubName, addCode, addButton);
hb.setSpacing(5);
final VBox vbox = new VBox();
vbox.setSpacing(10);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Subject {
private final SimpleStringProperty sub;
private final SimpleStringProperty code;
private Subject(String subName, String subCode) {
this.sub = new SimpleStringProperty(subName);
this.code = new SimpleStringProperty(subCode);
}
public String getSubName() {
return sub.get();
}
public void setSubName(String subName) {
sub.set(subName);
}
public String getSubCode() {
return code.get();
}
public void setSubCode(String subCode) {
code.set(subCode);
}
}
}
I will say its working.
When I run the code, the Table should contain one row of information which is stored in 'data'. It is not displayed in the table when I run the code. How can I make it displayed ?
Next thing is to add new data to the Table when the 'Add' button is pressed after filling the 2 TextFields. I filled the 2 Text Fields, pressed the button and nothing appeared in the Table. How can I make it appear ?
The PropertyValueFactories are wrong. For example
new PropertyValueFactory<Subject, String>("sub")
This means your data object should have a method getSub() to return the value. But yours is
public String getSubName() {
return sub.get();
}
Either rename the Getter to getSub() or use new PropertyValueFactory<Subject, String>("subName")
The same applies to subject code.
You should read about JavaBeans naming conventions. Or this article on JavaFX Properties and Binding
Try implementing the following functions:
public StringProperty subProperty(){
return sub;
}
public StringProperty codeProperty(){
return code;
}
Why are these members final?
In nutshell I want to add the row silently at the end of the table without affecting other rows.
This is my controller class code excerpt to add entries to the table. The problem is when I add an item to the sourceTree, instead of adding a row at the end silently it disturbs the complete table. Suppose there were already some entries in the table and the user expands one of the titled pane in the table, now when a new row will be added to the table, complete table will blink and this titled pane will automatically shrink. This blinking led me to conclude that instead of adding an entry whole table is refreshing probably. Please help...
#FXML
public static TableView<NodeInfo> tableView;
#FXML
public static TableColumn<NodeInfo, TitledPane> nodeTree;
#FXML
private TableColumn<NodeInfo, String> name;
#FXML
private TableColumn<NodeInfo, CheckBox> favourite;
#FXML
private TableColumn<NodeInfo, Button> updates;
#FXML
public static ObservableList<NodeInfo> sourceTree = FXCollections.observableArrayList();
tableView.setPlaceholder(new Label("You have no Nodes in the network at the moment!!!"));
nodeTree.setCellValueFactory(new PropertyValueFactory<NodeInfo, TitledPane>("TitledPaneNode"));
nodeTree.prefWidthProperty().bind(tableView.widthProperty().divide(3));
name.setCellValueFactory(new PropertyValueFactory<NodeInfo, String>("name"));
name.prefWidthProperty().bind(tableView.widthProperty().divide(3));
favourite.setCellValueFactory(new PropertyValueFactory<NodeInfo, CheckBox>("favourite"));
favourite.prefWidthProperty().bind(tableView.widthProperty().divide(6));
updates.setCellValueFactory(new PropertyValueFactory<NodeInfo, Button>("NoOfUpdates"));
updates.prefWidthProperty().bind(tableView.widthProperty().divide(6));
tableView.setItems(sourceTree);
The following is a SSCCE code I have simulated your use case. At the result there was no table disturbing, or pane shrinking. Test it yourself and compare it with yours:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TitledPane;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class PaneDemo extends Application {
private TableView<NodeInfo> table = new TableView<NodeInfo>();
private final ObservableList<NodeInfo> data = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setWidth(450);
stage.setHeight(500);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<NodeInfo, String>("firstName"));
TableColumn paneCol = new TableColumn("Pane");
paneCol.setMinWidth(100);
paneCol.setCellValueFactory(new PropertyValueFactory<NodeInfo, TitledPane>("titledPane"));
for (int i = 0; i < 5; i++) {
TitledPane pane = new TitledPane("title " + i, new Text("text " + i));
data.add(new NodeInfo("name " + i, pane));
}
table.setItems(data);
table.getColumns().addAll(firstNameCol, paneCol);
Button btn = new Button("add new item");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
TitledPane pane = new TitledPane("title new", new Text("text new"));
data.add(new NodeInfo("name new", pane));
}
});
final VBox vbox = new VBox(20);
vbox.getChildren().addAll(table, btn);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class NodeInfo {
private final SimpleStringProperty firstName;
private TitledPane titledPane;
private NodeInfo(String fName, TitledPane titledPane) {
this.firstName = new SimpleStringProperty(fName);
this.titledPane = titledPane;
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public TitledPane getTitledPane() {
return titledPane;
}
public void setTitledPane(TitledPane fName) {
titledPane = fName;
}
}
}