Wednesday 14 October 2015

Filled Under:

Tutorial On Table In JavaFx

In this program i have tried to discuss how to create simple table in java fx with data and without data in our first example we have created table with no data this can be done by creating TableColumn and then adding into a table.

TABLE WITH NO DATA :

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import javafx.stage.Stage;
public class tab extends Application
 {

public static void main(String[] args)
 {
launch(args);
}
@Override
public void start(Stage stage)
 {
Group root = new Group();
stage.setTitle("Simple Table View");
TableView table = new TableView();
table.setEditable(true);
TableColumn firstcol = new TableColumn("Name");
TableColumn secondcol = new TableColumn("Phone Number");
TableColumn thirdcol = new TableColumn("Fax");
table.getColumns().addAll(firstcol,secondcol,thirdcol);
root.getChildren().add(table);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}


OUTPUT:




TABLE WITH DATA :

In this example we show you how to add data in table with simple method in  we have created class name myData to define a data model for table and an ObservableList array is used to define data that is to be added in table .


import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class tab extends Application {

private final TableView<myData> table = new TableView<>();
private final ObservableList<myData> data =
FXCollections.observableArrayList(new myData("Jones ","8745674","jones@yahoo.com"),
new myData("Adam","9766765","adam@outlook.com"));


public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) {

TableColumn firstCol = new TableColumn("First Name");
firstCol.setMinWidth(100);
firstCol.setCellValueFactory(
new PropertyValueFactory<>("name"));


TableColumn secCol = new TableColumn("Fax-Number");
secCol.setCellValueFactory(
new PropertyValueFactory<>("number"));


TableColumn lastCol = new TableColumn("Email");
lastCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstCol, secCol,lastCol);
Group root = new Group();
root.getChildren().add(table);
stage.setTitle("Simple Table With Data");    
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}

public static class myData {
private final SimpleStringProperty Name;
private final SimpleStringProperty email;
private final SimpleStringProperty number;
private myData(String fName,String fnum,String email) {
this.Name = new SimpleStringProperty(fName);
this.email = new SimpleStringProperty(email);
this.number = new SimpleStringProperty(fnum);
}

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
public String getName() {
return Name.get();
}
public void setName(String fName) {
Name.set(fName);
}
public String getNumber() {
return number.get();
}
public void setNumber(String fName) {
number.set(fName);
}

}
}

OUTPUT: 






0 comments:

Post a Comment