Working checkboxes in JavaFX table (CheckBoxTableCell) - checkbox

(This is similar to a homework question.)
I recently made an example UI in Scenebuilder for something I later had to program with Java Swing. That more or less worked. Now it is my task, not for the actual development of the program, but for learning something in my job training, to make a similar UI with Scenebuilder, but this time an actually working one. The specifications are:
It's a window with a table in it.
At least two of the columns have radio boxes in them that look like checkboxes or checkboxes that act like radio boxes (because the company has weird standards).
It uses FXML files made with Scenebuilder for the layout.
Making the check boxes act as radio boxes should be easy, if I could just enable the editing. I found a lot of examples that do almost what I want, but are still all not really applicable to my situation. Here are some of them:
I started with this video and almost exactly copied the code to first have a working example. Then I adjusted it to my needs until I only had the check boxes to do (first working prototype had Booleans instead).
Then I took part of the full code of this answer to add the check boxes. That worked, but they don't react to clicks.
This, this and this seems to only apply to text fields, not checkboxes.
I then used the two lambda expressions from the second code block in this answer (actually I used the variant in the first answer and manually resolved some errors until suddenly Eclipse automatically converted it to lambda expressions). I also added the public ObservableValue<Boolean> getCompleted() method, Eclipse suggested some magic and then I had what you can see in the corresponding code below (without the console print).
I also added a listener to the boolean property, like this site (archive) apparently does (I think), but that doesn't seem like it helped much.
Here is a picture of how the dialog looks now, I still can't use the check boxes:
My question: How can I make it so that the check boxes react to clicks? React can mean outputting something on the console, I don't need a given code that makes it automatically disable the other checkbox, I want to figure that part out myself.
My code:
src.controller.MainController.java
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import view.Table;
public class MainController implements Initializable{
#FXML TableView<Table> tableID;
#FXML TableColumn<Table,String> iFirstName;
#FXML TableColumn<Table,String> iLastName;
#FXML TableColumn<Table,Boolean> iMalebox;
#FXML TableColumn<Table,Boolean> iFemalebox;
#Override public void initialize(URL location,ResourceBundle resources){
iFirstName.setCellValueFactory(new PropertyValueFactory<Table,String>("rFirstName"));
iLastName.setCellValueFactory(new PropertyValueFactory<Table,String>("rLastName"));
iMalebox.setCellValueFactory(p->p.getValue().getCompleted());
iMalebox.setCellFactory(p->new CheckBoxTableCell<>());
iMalebox.setEditable(true);
// iMalebox.setCellValueFactory(p->p.getValue().getCompleted());
// iMalebox.setCellFactory(p->new CheckBoxTableCell<>());
iFemalebox.setCellValueFactory(p->p.getValue().getCompleted());
iFemalebox.setCellFactory(p->new CheckBoxTableCell<>());
// iMalebox.setCellValueFactory(new PropertyValueFactory<Table,Boolean>("rMalebox"));
// iFemalebox.setCellValueFactory(new PropertyValueFactory<Table,Boolean>("rFemalebox"));
tableID.setItems(FXCollections.observableArrayList(new Table("Horst","Meier",true,false),new Table("Anna","Becker",false,true),new Table("Karl","Schmidt",true,false)));
tableID.setEditable(true);
}
}
src.controller.MainView.java
package controller;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainView extends Application{
#Override public void start(Stage primaryStage){
try{
// FXMLLoader.load(MainView.class.getResource("MainController.fxml"));
AnchorPane page=(AnchorPane)FXMLLoader.load(MainView.class.getResource("MainController.fxml"));
Scene scene=new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("Window Title");
primaryStage.show();
}catch(Exception e){
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE,null,e);
}
}
public static void main(String[] args){
Application.launch(MainView.class,(java.lang.String[])null);
}
}
src.controller.MainController.fxml
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<TableView fx:id="tableID" prefHeight="494.0" prefWidth="798.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="iFirstName" prefWidth="75.0" text="First name" />
<TableColumn fx:id="iLastName" prefWidth="75.0" text="Last name" />
<TableColumn fx:id="iMalebox" prefWidth="75.0" text="Male" />
<TableColumn fx:id="iFemalebox" prefWidth="75.0" text="Female" />
</columns>
</TableView>
</children>
</AnchorPane>
src.view.Table.java
package view;
import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class Table{
private SimpleStringProperty rFirstName;
private SimpleStringProperty rLastName;
private SimpleBooleanProperty rMalebox;
private SimpleBooleanProperty rFemalebox;
public Table(String sFirstName,String sLastName,Boolean sMalebox,Boolean sFemalebox){
rFirstName=new SimpleStringProperty(sFirstName);
rLastName=new SimpleStringProperty(sLastName);
rMalebox=new SimpleBooleanProperty(sMalebox);
rMalebox.addListener((ChangeListener)(observable,oldValue,newValue)->{
System.out.println("test");
System.out.println("abc");
});
rFemalebox=new SimpleBooleanProperty(sFemalebox);
}
public String getRFirstName(){
return rFirstName.get();
}
public void setRFirstName(String v){
rFirstName.set(v);
}
public String getRLastName(){
return rLastName.get();
}
public void setRLastName(String v){
rLastName.set(v);
}
public Boolean getRMalebox(){
return rMalebox.get();
}
public void setRMalebox(Boolean v){
rMalebox.set(v);
}
public Boolean getRFemalebox(){
return rFemalebox.get();
}
public void setRFemalebox(Boolean v){
rFemalebox.set(v);
}
public ObservableValue<Boolean> getCompleted(){
return new ObservableValue<Boolean>(){
#Override public void removeListener(InvalidationListener arg0){}
#Override public void addListener(InvalidationListener arg0){}
#Override public void removeListener(ChangeListener<? super Boolean> listener){}
#Override public Boolean getValue(){
return null;
}
#Override public void addListener(ChangeListener<? super Boolean> listener){
System.out.println("Test");
}
};
}
}

I found a solution. Since I changed so much (I worked on getting this stupid thing to work for almost 20 hours after asking that question), it's not really useful to enumerate all the changes I did. But here is at least a working example.
There are very few lines responsible for making the "male" and "female" boxes trigger each other's opposites (which is specific to my task), everything else is just for getting actually properly working CheckBoxTableCells. One would think that that is a really common case and there should be standard methods for that, like for "print text into the console" or "read file", but apparently not, apparently everything has to be complicated in programming with UIs.
Anyway, rant aside, here is the working code:
src.controller.MainView.java
package controller;
import java.io.*;
import javafx.application.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class MainView extends Application{
#Override public void start(Stage primaryStage) throws IOException{
primaryStage.setScene(new Scene((AnchorPane)FXMLLoader.load(MainView.class.getResource("MainController.fxml"))));
primaryStage.show();
}
public static void main(String[] args){
Application.launch(MainView.class);
}
}
src.controller.MainController.java
package controller;
import java.net.*;
import java.util.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.fxml.*;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.*;
import javafx.scene.control.cell.*;
import javafx.util.*;
import view.*;
public class MainController implements Initializable{
#FXML TableView<Table> tableID;
#FXML TableColumn<Table,String> iFirstName;
#FXML TableColumn<Table,String> iLastName;
#FXML TableColumn<Table,Boolean> iMalebox;
#FXML TableColumn<Table,Boolean> iFemalebox;
#Override public void initialize(URL location,ResourceBundle resources){
iFirstName.setCellValueFactory(new PropertyValueFactory<Table,String>("rFirstName"));
iLastName.setCellValueFactory(new PropertyValueFactory<Table,String>("rLastName"));
iMalebox.setCellValueFactory(new Callback<CellDataFeatures<Table,Boolean>,ObservableValue<Boolean>>(){
#Override public ObservableValue<Boolean> call(CellDataFeatures<Table,Boolean> cellData){
return cellData.getValue().maleCheckedProperty(true);
}
});
iMalebox.setCellFactory(new Callback<TableColumn<Table,Boolean>,TableCell<Table,Boolean>>(){
#Override public TableCell<Table,Boolean> call(TableColumn<Table,Boolean> param){
return new CheckBoxTableCell<>();
}
});
iFemalebox.setCellValueFactory(new Callback<CellDataFeatures<Table,Boolean>,ObservableValue<Boolean>>(){
#Override public ObservableValue<Boolean> call(CellDataFeatures<Table,Boolean> cellData){
return cellData.getValue().femaleCheckedProperty(true);
}
});
iFemalebox.setCellFactory(new Callback<TableColumn<Table,Boolean>,TableCell<Table,Boolean>>(){
#Override public TableCell<Table,Boolean> call(TableColumn<Table,Boolean> param){
return new CheckBoxTableCell<>();
}
});
tableID.setItems(FXCollections.observableArrayList(new Table("Horst","Meier",true),new Table("Anna","Becker",false),new Table("Karl","Schmidt",true)));
}
}
src.view.Table.java
package view;
import javafx.beans.property.*;
public class Table{
private String rFirstName;
private String rLastName;
public Table(String sFirstName,String sLastName,Boolean sMale){
rFirstName=sFirstName;
rLastName=sLastName;
maleCheckedProperty(false).set(sMale);
}
private SimpleBooleanProperty maleChecked=new SimpleBooleanProperty(false);
private SimpleBooleanProperty femaleChecked=new SimpleBooleanProperty(false);
public SimpleBooleanProperty maleCheckedProperty(boolean recursion){
if(recursion) femaleCheckedProperty(false).set(!maleChecked.get());
return maleChecked;
}
public SimpleBooleanProperty femaleCheckedProperty(boolean recursion){
if(recursion) maleCheckedProperty(false).set(!femaleChecked.get());
return femaleChecked;
}
public String getRFirstName(){
return rFirstName;
}
public String getRLastName(){
return rLastName;
}
}
src.controller.MainController.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="98.0" prefWidth="218.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<TableView fx:id="tableID" editable="true" prefHeight="494.0" prefWidth="798.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="iFirstName" maxWidth="1.7976931348623157E308" minWidth="-1.0" prefWidth="63.0" text="First name" />
<TableColumn fx:id="iLastName" maxWidth="1.7976931348623157E308" minWidth="-1.0" prefWidth="63.0" text="Last name" />
<TableColumn fx:id="iMalebox" maxWidth="1.7976931348623157E308" minWidth="-1.0" prefWidth="45.0" text="Male"/>
<TableColumn fx:id="iFemalebox" maxWidth="1.7976931348623157E308" minWidth="-1.0" prefWidth="45.0" text="Female"/>
</columns>
</TableView>
</children>
</AnchorPane>

Related

replacement for camel cxfbean

we are currently struggling with updating our legacy service (non spring, jee + deltaspike, weld) and it's dependencies.
We try to upgrade from camel 2.16.2 to 3.x (due to java 11 compatibility).
We have already read through the migration guide several times, but could not find any reference to your replacement of the cxfbean component.
e.g.:
public class MonitoringRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
from("servlet:///monitoring?matchOnUriPrefix=true")
.to("cxfbean:monitoringService")
.setId("MonitoringRoute");
}
}
#Named("monitoringService")
public class MonitoringService implements MonitoringAPI {
#Override
public String status() {
return "OK";
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public interface MonitoringAPI {
#GET
#Path("status")
#Produces(MediaType.TEXT_PLAIN)
String status();
}
We already tried cxfrs:monitoringService, but this will led to "Uri is not absolute" exception.
Any idea to replace cxfbean properly?

Download pdf for my web users

I'm having fun with pdfbox but still can't find usefull info about downloading this.
So, I've got button, when I click it - pdf is saved on my computer locally.
Controller:
#RequestMapping("/pdf")
public class DocumentCreateController {
#Autowired
private DocumentCreate DCpdf;
#RequestMapping(method=RequestMethod.GET)
public void getPDF() throws IOException{
DCpdf.getPDF();
}
}
Service:
package pl.asseco.portalhr.obiegdokum;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.stereotype.Service;
#Service
public class DocumentCreate {
public void getPDF() throws IOException{
PDDocument document = new PDDocument();
document.save("C:/myfolder/test.pdf");
document.close();
return;
}
}
Please help, what about I have to learn? What I need to use?

Populating a TableView with data from Database JavaFX

I'm trying to populate my TableView in JavaFX with data from my database.
I've been trying a lot of different methods to try and get something working. I've been changing a lot of things but ultimately I haven't gotten anywhere. I think it has something to do with the way I'm loading my FXML file.
My database has 3 columns, so I'm not sure if I have to define everything from the database in my Artist class and that's why it's giving me errors.
I built my GUI with SceneBuilder so I'll attach my FXML file.
My main class
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
public class Main extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(Main.class
.getResource("artistSelection.fxml"));
Group rootGroup = loader.load();
final artistSelectionController controller = loader.getController();
controller.setStage(stage);
Scene scene = new Scene(rootGroup,600,400);
stage.setScene(scene);
StageStyle stageStyle = StageStyle.TRANSPARENT;
scene.setFill(Color.TRANSPARENT);
stage.initStyle(stageStyle);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My Controller Class
public class artistSelectionController{
#FXML
private Rectangle rect;
#FXML
private ImageView logo;
#FXML
private HBox buttons;
#FXML
private Button newArtist;
#FXML
private Button closeScreen;
private Stage stage;
private double dragAnchorX;
private double dragAnchorY;
private PreparedStatement preparedStatement;
public void setStage(Stage stage){
this.stage = stage;
}
#FXML
public void closeScreenHandler(ActionEvent e){
stage.close();
}
#FXML
public void mousePressedHandler(MouseEvent me) {
dragAnchorX = me.getScreenX() - stage.getX();
dragAnchorY = me.getScreenY() - stage.getY();
}
#FXML
public void mouseDraggedHandler(MouseEvent me) {
stage.setX(me.getScreenX() - dragAnchorX);
stage.setY(me.getScreenY() - dragAnchorY);
}
#FXML
private static TableView<Artist> artistTable;
#FXML
private TableColumn<Artist, String> artistName;
private DBConnect objDbClass;
private Connection con;
private Logger logger;
#FXML
void initialize(){
// assert artistTable != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
artistName.setCellValueFactory(
new PropertyValueFactory<Artist,String>("Artist"));
objDbClass = new DBConnect();
try{
con = objDbClass.getConnection();
buildData();
}
catch(ClassNotFoundException ce){
logger.newInput(ce.toString());
}
catch(SQLException ce){
logger.newInput(ce.toString());
}
}
private ObservableList<Artist> data;
public void buildData(){
data = FXCollections.observableArrayList();
try{
String SQL = "Select Name from ARTIST";
ResultSet rs = con.createStatement().executeQuery(SQL);
while(rs.next()){
Artist cm = new Artist();
cm.name.set(rs.getString("name"));
data.add(cm);
}
artistTable.setItems(data);
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
My POJO
package application;
import javafx.beans.property.SimpleStringProperty;
public class Artist {
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty genre = new SimpleStringProperty();
public String getName() {
return name.get();
}
public String getGenre() {
return genre.get();
}
}
My Database Connector
import java.sql.*;
public class DBConnect{
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost/myStudio","root","root");
}
}
And finally my FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<Group fx:id="rootGroup" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application/artistSelectionController">
<children>
<Rectangle fx:id="rect" arcHeight="50.0" arcWidth="50.0" height="376.0" layoutX="13.0" layoutY="13.0" stroke="BLACK" strokeType="INSIDE" width="577.0">
<fill>
<LinearGradient cycleMethod="REPEAT" endX="1.0" endY="1.0" startY="1.0">
<stops>
<Stop color="#1b2f36" />
<Stop color="#1b2f36" offset="0.007662835249042145" />
<Stop color="#15596e" offset="1.0" />
</stops>
</LinearGradient>
</fill>
</Rectangle>
<ImageView fx:id="logo" fitHeight="411.0" fitWidth="606.0" layoutX="13.0" layoutY="13.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../Downloads/myStudio.png" />
</image>
</ImageView>
<TableView fx:id="artistTable" layoutX="316.0" layoutY="143.0" prefHeight="223.0" prefWidth="241.0">
<columns>
<TableColumn fx:id="artistName" editable="false" prefWidth="240.0" resizable="false" text="Artists" />
</columns>
</TableView>
<HBox fx:id="buttons" layoutX="65.0" layoutY="339.0" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<children>
<Button fx:id="newArtist" mnemonicParsing="false" prefWidth="75.0" text="New Artist" />
<Button fx:id="closeScreen" mnemonicParsing="false" prefWidth="75.0" text="Close" />
</children>
</HBox>
</children>
</Group>
Thanks
There seem to be many errors in your code.
First, you have made artistTable static. Don't do this - it makes no sense at all, and static fields won't be injected by the FXMLLoader. This is why you are getting the null pointer exception.
Second, the value you pass to the PropertyValueFactory ("Artist") does not match the name of any property in your model class Artist. (I.e. there is no ArtistProperty() method and no getArtist() method).
You probably want
artistName.setCellValueFactory(
new PropertyValueFactory<Artist,String>("name"));
which will tie the value for the column to the getName() method. See the PropertyValueFactory documentation for a description of how it works.
(You should probably define your POJO according to the JavaFX Property pattern described here. However just having a get...() method will work.)

Multiple Columns in Oracle TableView

my code is working to 90% I think.
The Output is right, but not all columns are shown in the tableview.
I don't know where the problem is.
The fx:id is right, the code too, I believe.
Here's a picture of the openend program. I will give you also my output an my controller an table-code. The launching code isn't important.
Picture:
http://www.directupload.net/file/d/3791/aitj4rff_png.htm
Output:
*** Loaded Oracle-Driver ***
*** Connected with Database ***
Film: Hugo Gesehen: JA
Film: FAST and FURIOS 6 Gesehen: JA
Film: Emil Gesehen: NEIN
Film: DAS_BOOT Gesehen: JA
*** Database data saved to Observable List named 'data'
*** Table Items setted ***
table_controller:
package controller;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import oracle.jdbc.*;
import model.filmtable_data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
#SuppressWarnings("unused")
public class table_controller implements Initializable {
private ObservableList<filmtable_data> filmData = FXCollections.observableArrayList();
#FXML
private TableView<filmtable_data> FilmTable;
#FXML
private TableColumn<filmtable_data, String> Filmtitel_Col;
#FXML
private TableColumn<filmtable_data, Integer> ID_Col;
#FXML
private TableColumn<filmtable_data, String> Gesehen_Col;
#Override
public void initialize(URL location, ResourceBundle resources) {
// Observable List
ID_Col.setCellValueFactory(new PropertyValueFactory<filmtable_data, Integer>("rID"));
Filmtitel_Col.setCellValueFactory(new PropertyValueFactory<filmtable_data, String>("rFilmtitel"));
Gesehen_Col.setCellValueFactory(new PropertyValueFactory<filmtable_data, String>("rGesehen"));
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("*** Loaded Oracle-Driver ***");
} catch (ClassNotFoundException e1) {
System.out.println("Driver-Loading failed.");
e1.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:lukas/1234#10.140.79.39:1521:OTTO");
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery("SELECT FILMTITEL, GESEHEN FROM LUKA1");
String filmtitel = "empty";
String gesehen = "empty";
int zaehler = 1;
System.out.println("*** Connected with Database ***");
while (resultset.next()) {
filmtitel = resultset.getString("FILMTITEL");
gesehen = resultset.getString("GESEHEN");
System.out.println("Film: " + filmtitel + "\t\t\tGesehen: " + gesehen);
filmtable_data entry = new filmtable_data(zaehler, filmtitel, gesehen);
filmData.add(entry);
zaehler++;
}
System.out.println("*** Database data saved to Observable List named 'data'");
FilmTable.setItems(filmData);
System.out.println("*** Table Items setted ***");
statement.close();
} catch (SQLException e) {
System.out.println("Login fehlgeschlagen.");
e.printStackTrace();
}
}
}
filmtable_data:
package model;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class filmtable_data {
private final SimpleIntegerProperty rID;
private final SimpleStringProperty rFilmtitel;
private final SimpleStringProperty rGesehen;
public filmtable_data (Integer sID, String sFilmtitel, String sGesehen) {
this.rID = new SimpleIntegerProperty(sID);
this.rFilmtitel = new SimpleStringProperty(sFilmtitel);
this.rGesehen = new SimpleStringProperty(sGesehen);
}
public Integer getRID() {
return rID.get();
}
public void setRID(int set) {
rID.set(set);
}
public String getRFilmtitel() {
return rFilmtitel.get();
}
public void setRFilmtitel(String set) {
rFilmtitel.set(set);
}
public String setRGesehen() {
return rGesehen.get();
}
public void setRGesehen(String set) {
rGesehen.set(set);
}
}
the fx-file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="366.0" prefWidth="268.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.table_controller">
<children>
<TableView fx:id="FilmTable" prefHeight="366.0" prefWidth="425.0">
<columns>
<TableColumn fx:id="ID_Col" minWidth="-Infinity" prefWidth="43.0" text="ID" />
<TableColumn fx:id="Filmtitel_Col" prefWidth="137.0" text="Filmtitel" />
<TableColumn fx:id="Gesehen_Col" prefWidth="137.0" text="Gesehen" />
</columns>
</TableView>
</children>
</AnchorPane>
You have a typo in your POJO class, and there is not getter:
This is wrong:
public String setRGesehen() {
return rGesehen.get();
}
It should be:
public String getRGesehen() {
return rGesehen.get();
}
Notice you should add also the property methods, like:
public StringProperty rGesehenProperty() {
return rGesehen;
}
Even with the wrong getter, as the PropertyValueFactory looks first for the property, it would have worked.

Selenium Web driver--Failure Screenshot is not captured in TestNG report

With below mentioned code,if the test case is pass-screenshot captured successfully and displayed in report.But when the test is failed--screenshot is not displayed.Even screenshot hyperlink is not displayed in report.Anybody can sort out the mistake in code?
package listeners;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.logging.Logger;
#Listeners
public class CountryChoserLayer extends TestListenerAdapter {
#Test(priority=1)
public void choseCountry() throws Exception{
driver.findElement(By.id("intselect")).sendKeys("India");
driver.findElement(By.xpath(".//*[#id='countryChooser']/a/img")).click();
//window.onbeforeunload = null;
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
FileUtils.copyFile(scrnsht, new File(NewFileNamePath));
System.out.println(NewFileNamePath);
Reporter.log("Passed Screenshot");
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case-Success");
System.out.println("---------------------------------------");
}
public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605";
public WebDriver driver;
public int Count = 0;
#Test(priority=0)
public void openBrowser() {
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get(baseurl);
}
#Test(priority=2)
public void closeBrowser() {
driver.quit();
}
#Override
public void onTestFailure(ITestResult result){
Reporter.log("Fail");
System.out.println("BBB");
//Reporter.setCurrentTestResult(result);
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
//System.out.println("AAA" + NewFileNamePath);
try {
//System.out.println("CCC");
FileUtils.copyFile(scrnsht,new File(NewFileNamePath));
System.out.println(NewFileNamePath);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("DDD");
e.printStackTrace();
}
Reporter.log("Failed Screenshot");
Reporter.setCurrentTestResult(null);
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case Failed");
System.out.println("---------------------------------------");
}
#Override
public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped
Reporter.log("Skip");
}
#Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
Reporter.log("Pass");
}
}
Your onTestFailure method is not being called because you didn't specify listener for your test class. You are missing a value in #Listeners annotation. It should be something like
#Listeners({CountryChoserLayer.class})
You can find more ways of specifying a listener in official TestNg's documentation.
Another problem you are likely to encounter would be NullPointerException while trying to take screenshot in onTestFailure method. The easiest workaround for that would be changing the declaration of driver field to static. I run the code with those fixes and I got the report with screenshot.
I must add that in my opinion putting both test and listener methods into one class is not a good practice.

Resources