You are on page 1of 11

Javafx tableview by adding new row in the table

during runtime with radiobutton

Fie 1: Using the FxmlDocument.fxml

- we can see what are the controls used and what are the id's created for the controls

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="423.0" prefWidth="513.0"


xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="javafxtableiew.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16"
minWidth="69" />
<TableView fx:id="table" layoutX="87.0" layoutY="188.0" prefHeight="200.0"
prefWidth="340.0">
<columns>
<TableColumn fx:id="name" prefWidth="75.0" text="Name" />
<TableColumn fx:id="regno" prefWidth="75.0" text="Regno" />
<TableColumn fx:id="course" prefWidth="75.0" text="course" />
<TableColumn fx:id="gender" prefWidth="75.0" text="Gender" />
</columns>
</TableView>
<TextField fx:id="t1" layoutX="52.0" layoutY="68.0" />
<TextField fx:id="t2" layoutX="224.0" layoutY="68.0" />
<TextField fx:id="t3" layoutX="52.0" layoutY="125.0" />
<Label layoutX="73.0" layoutY="42.0" text="Name" />
<Label layoutX="257.0" layoutY="42.0" text="Regno" />
<Label layoutX="73.0" layoutY="103.0" text="course" />
<Label layoutX="271.0" layoutY="103.0" text="Sex" />
<Button fx:id="b1" layoutX="421.0" layoutY="68.0" mnemonicParsing="false"
onAction="#addrowhandler" text="Add row" />
<Button fx:id="b2" layoutX="427.0" layoutY="116.0" mnemonicParsing="false"
onAction="#exithandler" text="Exit" />
<RadioButton fx:id="mradio" layoutX="257.0" layoutY="129.0"
mnemonicParsing="false" text="Male" />
<RadioButton fx:id="fradio" layoutX="330.0" layoutY="129.0"
mnemonicParsing="false" text="Female" />
<Button fx:id="b3" layoutX="426.0" layoutY="157.0" mnemonicParsing="false"
onAction="#clearallhandler" text="Clear All" />
</children>
</AnchorPane>

File 2:student.java

- This file helps in creating the class and its corresponding


methods and constructor

package javafxtableiew;

/*
* 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.
*/

/**
*
* @author admin
*/
public class Student {
private String name;
private String regno;
private String course;
private String gender;

public Student(){} //default constructor for the new row to be


added during runtime

/**
* @return the name
*/
public Student(String name,String regno, String course,String
gender)
{
this.name=name;
this.regno=regno;
this.course=course;
this.gender=gender;
}

public String getName() {


return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the regno
*/
public String getRegno() {
return regno;
}
/**
* @param regno the regno to set
*/
public void setRegno(String regno) {
this.regno = regno;
}

/**
* @return the course
*/
public String getCourse() {
return course;
}

/**
* @param course the course to set
*/
public void setCourse(String course) {
this.course = course;
}

/**
* @return the gender
*/
public String getGender() {
return gender;
}

/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}

}
3. FXML Controller file
FMXLDocumentController.java

package javafxtableiew;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;

/**
*
* @author admin
*/
public class FXMLDocumentController implements Initializable
{
@FXML
private Label label;

@FXML
private TableView<Student> table;

@FXML
private TableColumn<Student,String> name;
@FXML
private TableColumn<Student, String> regno;

@FXML
private TableColumn<Student,String> course;
@FXML
private TableColumn<Student,String> gender;

@FXML
private TextField t1;

@FXML
private TextField t2;

@FXML
private TextField t3;

@FXML
private Button b1;

@FXML
private Button b2;
@FXML
private RadioButton mradio;

@FXML
private RadioButton fradio;
@FXML
private Button b3;

private void handleButtonAction(ActionEvent event) {


System.out.println("You clicked me!");
label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
ToggleGroup sex = new ToggleGroup();
mradio.setToggleGroup(sex);
fradio.setToggleGroup(sex);
mradio.setSelected(true);

ObservableList <Student>
items=FXCollections.observableArrayList();
items.add(new
Student("AAA","1001","B.Tech","Female"));
items.add(new
Student("BBB","1002","B.Tech","Female"));
items.add(new
Student("CCC","1003","B.Tech","Female"));
items.add(new Student("DDD","1004","B.Tech","Male"));
items.add(new Student("EEE","1005","B.Tech","Male"));
table.setItems(items);
name.setCellValueFactory(new
PropertyValueFactory<Student,String>("name"));
regno.setCellValueFactory(new
PropertyValueFactory<Student,String>("regno"));
course.setCellValueFactory(new
PropertyValueFactory<Student,String>("course"));
gender.setCellValueFactory(new
PropertyValueFactory<Student,String>("gender"));

@FXML
private void addrowhandler(ActionEvent event) {
ToggleGroup sex = new ToggleGroup();
mradio.setToggleGroup(sex);
fradio.setToggleGroup(sex);
mradio.setSelected(true);
RadioButton rb = (RadioButton) sex.getSelectedToggle();
String d=rb.getText();
String a = t1.getText();
String b = t2.getText();
String c = t3.getText();

Student stu =new Student(); //new object created during


runtime

stu.setName(a);
stu.setRegno(b);
stu.setCourse(c);
stu.setGender(d);
table.getItems().addAll(stu);
System.out.println(a+b+c+d);
//t1.clear();
//t2.clear();
//t3.clear();
//t4.clear();

@FXML
private void exithandler(ActionEvent event) {
System.exit(0);
}

@FXML
private void clearallhandler(ActionEvent event) // to clear the
data
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
mradio.setSelected(false);
fradio.setSelected(false);
}

File 4: Main file


/*
* 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 javafxtableiew;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
*
* @author admin
*/
public class JavafxTableiew extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("FXMLDocument.fx
ml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

output:

Before adding new row


After adding the new row

clear all button - clears the data


Exit - close the application

You might also like