Why these statements does not work?
if (iv_ship.intersects(iv_plane.getBoundsInLocal())) System.out.println("xxxxxxxxx");
if (iv_plane.intersects(iv_ship.getBoundsInLocal())) System.out.println("zzzz");
if (iv_plane.getBoundsInLocal().intersects(iv_ship.getBoundsInLocal())) System.out.println("dupa");
I am pretty sure these two objects intersect. Maybe it is fault of TranslateTransition?
But i was also trying to intersect Rectangles in coast, which are not TT.
Best regards.
Here is all the code:
package riverpuff.v3;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javafx.animation.AnimationTimer;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* #author Marek
*/
public class RiverPuffV3 extends Application {
public String name = "";
public Rectangle shot = new Rectangle();
#Override
public void start(Stage primaryStage) {
createMenu(primaryStage);
primaryStage.show();
primaryStage.setResizable(false);
primaryStage.getIcons().
add(new Image(getClass().getResourceAsStream("Icon.png")));
}
private void createMenu(Stage primaryStage) {
primaryStage.setScene(null);
GridPane pane_menu = new GridPane();
Button button_name = new Button("Name");
button_name.setMaxHeight(Double.MAX_VALUE);
button_name.setMaxWidth(Double.MAX_VALUE);
button_name.setOnAction(e -> {
createName(primaryStage);
/*try{
OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("log.txt", true), "UTF-8");
BufferedWriter fbw = new BufferedWriter(writer);
fbw.newLine();
fbw.write("append txt...");
fbw.newLine();
fbw.close();
}
catch (Exception v) {
System.out.println("Error: " + v.getMessage());
}*/
});
Button button_start = new Button("Start");
button_start.setMaxHeight(Double.MAX_VALUE);
button_start.setMaxWidth(Double.MAX_VALUE);
button_start.setOnAction(e -> {
drawGame(primaryStage);
});
pane_menu.setHgap(10);
pane_menu.setVgap(10);
pane_menu.add(button_name,0,10,10,10);
pane_menu.add(button_start,15,10,10,10);
//reading name from a file
try {
String read_file = null;
BufferedReader in = new BufferedReader(new FileReader("log.txt"));
read_file = in.readLine();
Text text_name = new Text("Hello " + read_file);
pane_menu.add(text_name,5,5,10,5);
} catch (FileNotFoundException e) {
System.out.println("File not found!");
//throw new RuntimeException("File not found");
} catch (IOException e) {
System.out.println("IO Error occured");
//throw new RuntimeException("IO Error occured");
}
Scene scene_menu = new Scene(pane_menu, 300, 500);
primaryStage.setTitle("River Puff");
primaryStage.setScene(scene_menu);
}
//save name to a file
private void createName (Stage primaryStage) {
primaryStage.setScene(null);
GridPane pane_name = new GridPane();
TextField tf_name = new TextField();
tf_name.setMaxHeight(50);
tf_name.setMaxWidth(240);
tf_name.setAlignment(Pos.CENTER);
tf_name.setFont(Font.font("Verdana",25));
tf_name.setOnKeyPressed(ke -> {
if (ke.getCode() == KeyCode.ENTER) {
name = tf_name.getText();
if (!name.isEmpty()){
MyFile myFile = new MyFile();
myFile.writeTextFile("log.txt", name);
}
createMenu(primaryStage);
}
});
Button button_ok = new Button("OK");
button_ok.setMaxHeight(30);
button_ok.setMaxWidth(80);
button_ok.setOnAction(e -> {
name = tf_name.getText();
if (!name.isEmpty()){
MyFile myFile = new MyFile();
myFile.writeTextFile("log.txt", name);
}
createMenu(primaryStage);
});
Text text_name = new Text("What is your name?");
text_name.setFont(Font.font("Verdana",15));
pane_name.setHgap(10);
pane_name.setVgap(10);
pane_name.add(text_name,8,9,5,5);
pane_name.add(button_ok,11,22,8,3);
pane_name.add(tf_name,3,15,24,5);
Scene scene_name = new Scene(pane_name, 300, 500);
primaryStage.setTitle("River Puff - Name");
primaryStage.setScene(scene_name);
}
private void drawGame (Stage primaryStage) {
primaryStage.setScene(null);
final int H = 700;
final int W = 1000;
Group root = new Group();
Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
Button button_menu = new Button("Menu");
button_menu.setOnAction(e ->{
createMenu(primaryStage);
});
Image ship = new Image((getClass().getResourceAsStream("ship.png"))); //loading images
Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
/**********************************************************************/
Rectangle[] coastL = {
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle()
};
Rectangle[] coastR = {
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle(),
new Rectangle(), new Rectangle()
};
for (int i=0; i<8; i++) {
coastL[i].setFill(Color.FORESTGREEN);
coastL[i].setHeight(100);
coastR[i].setFill(Color.FORESTGREEN);
coastR[i].setHeight(100);
}
AnimationTimer timer = new AnimationTimer() {
int[] j = {0,0,0,0,0,0,0,0};
#Override
public void handle(long now) {
for (int i=0; i<8; i++) if (j[i]==(i*100+800)) j[i]=i*100;
for (int i=1;i<9;i++) { //creating coast
coastL[i-1].setX(0);
coastL[i-1].setY(j[i-1]-(i*100));
coastL[i-1].setWidth(250+i*(i%3));
coastR[i-1].setX(W-(250+i*(i%4)));
coastR[i-1].setY(j[i-1]-(i*100));
coastR[i-1].setWidth(250+i*(i%4));
}
for (int i=0;i<8;i++) j[i]++;
}
};
timer.start();
ImageView iv_ship = new ImageView();
iv_ship.setImage(ship);
iv_ship.setFitWidth(150);
iv_ship.setFitHeight(45);
iv_ship.setX(300);
iv_ship.setY(0);
TranslateTransition tt_shipX =
new TranslateTransition(Duration.millis(2000), iv_ship); //moving enemies
tt_shipX.setAutoReverse(true);
tt_shipX.setCycleCount(Timeline.INDEFINITE);
tt_shipX.setByX(200f);
tt_shipX.play();
TranslateTransition tt_shipY =
new TranslateTransition(Duration.millis(13000), iv_ship);
tt_shipY.setAutoReverse(false);
tt_shipY.setCycleCount(Timeline.INDEFINITE);
tt_shipY.setByY(800);
tt_shipY.play();
ImageView iv_plane = new ImageView();
iv_plane.setImage(plane);
iv_plane.setFitWidth(50);
iv_plane.setFitHeight(50);
iv_plane.setX(475);
iv_plane.setY(600);
TranslateTransition tt_plane =
new TranslateTransition(Duration.millis(1), iv_plane);
TranslateTransition tt_shot =
new TranslateTransition(Duration.millis(4000), shot);
tt_shot.setAutoReverse(false);
tt_shot.setCycleCount(1);
TranslateTransition tt_shotB =
new TranslateTransition(Duration.millis(0.5f), shot);
tt_shotB.setAutoReverse(false);
tt_shotB.setCycleCount(1);
root.setOnKeyPressed((KeyEvent ke) -> { //steering a plane }
if (ke.getCode() == KeyCode.LEFT &&
tt_plane.getNode().getTranslateX() > -475) {
tt_plane.setByX(-5f);
tt_plane.play();
System.out.println(tt_plane.getNode().getTranslateX());
}
else if (ke.getCode() == KeyCode.RIGHT &&
tt_plane.getNode().getTranslateX() < 475) {
tt_plane.setByX(5f);
tt_plane.play();
System.out.println(tt_plane.getNode().getTranslateX());
}
else if (ke.getCode() == KeyCode.A) {
shot.setFill(Color.BLACK);
shot.setX(tt_plane.getNode().getTranslateX()+495);
shot.setY(580);
shot.setWidth(10);
shot.setHeight(20);
tt_shot.setByY(-600);
tt_shot.play();
tt_shot.setOnFinished((ActionEvent arg0) -> {
shot.setDisable(true);
tt_shotB.setByY(600);
tt_shotB.play();
});
}
});
if (iv_ship.intersects(iv_plane.getBoundsInLocal())) System.out.println("xxxxxxxxx");
if (iv_plane.intersects(iv_ship.getBoundsInLocal())) System.out.println("zzzz");
if (iv_plane.getBoundsInLocal().intersects(iv_ship.getBoundsInLocal())) System.out.println("dupa");
root.getChildren().add(button_menu);
for (int i=0; i<8; i++) {
root.getChildren().add(coastL[i]);
root.getChildren().add(coastR[i]);
}
root.getChildren().add(iv_plane);
root.getChildren().add(iv_ship);
root.getChildren().add(shot);
primaryStage.setScene(scene_game);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
First of all, you need to take into account that the nodes can be transformed (with translations), so instead of getBoundsInLocal() you need getBoundsInParent().
According to javadocs:
getBoundsInParent() gets the value of the property boundsInParent. The rectangular bounds of this Node which include its transforms. boundsInParent is calculated by taking the local bounds (defined by boundsInLocal) and applying the transform created.
This will work, at any given position of both nodes:
if(iv_plane.getBoundsInParent().intersects(iv_ship.getBoundsInParent())){
System.out.println("Intersection detected");
}
The next problem you have to solve is when do you check a possible intersection. Based on your code, you checked only once, when the nodes where not even added to the root/scene/stage. That couldn't work.
For this to work, you have to check everytime one of them is moved, using some listeners to changes in the translateXProperty() and translateYProperty(), something like this:
private final ChangeListener<Number> checkIntersection = (ob,n,n1)->{
if (iv_plane.getBoundsInParent().intersects(iv_ship.getBoundsInParent())){
System.out.println("Intersection detected");
}
};
private ImageView iv_ship, iv_plane;
private void drawGame (Stage primaryStage) {
iv_ship = new ImageView();
iv_plane = new ImageView();
...
iv_ship.translateXProperty().addListener(checkIntersection);
iv_ship.translateYProperty().addListener(checkIntersection);
...
root.getChildren().addAll(iv_plane,iv_ship);
primaryStage.setScene(scene_game);
}
Related
I need to use a Label that was created in another class in my JavaFx application
This is my code:
Main Class
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class Main extends Application {
Stage window;
Scene scene1, scene2, scene3;
private Scanner x;
#Override
public void start(Stage primaryStage) throws Exception {
readTutor r = new readTutor();
window = primaryStage;
window.setTitle("Tutor Finder");
Label labelA1 = new Label("Welcome to the best online tutor\nproviding service in the world");
Button buttonA2 = new Button("Find a tutor!");
Button buttonA1 = new Button("Become a tutor!");
Button buttonB1 = new Button("Click here to go back");
Button buttonB2 = new Button("Submit");
Button buttonC1 = new Button("Click here to go back");
buttonB1.setOnAction(e -> window.setScene(scene1));
buttonC1.setOnAction(e -> window.setScene(scene1));
buttonA1.setOnAction(e -> window.setScene(scene2));
buttonA2.setOnAction(e -> window.setScene(scene3));
Label labelB1 = new Label("First Name:");
TextField textB1 = new TextField();
Label labelB2 = new Label("Last Name:");
TextField textB2 = new TextField();
Label labelB3 = new Label("Subject:");
TextField textB3 = new TextField();
Label labelB4 = new Label("Age:");
TextField textB4 = new TextField();
Label labelB5 = new Label("Contact No#:");
TextField textB5 = new TextField();
Label labelB6 = new Label("Country: ");
TextField textB6 = new TextField();
VBox layout = new VBox(20);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(labelA1, buttonA1, buttonA2);
scene1 = new Scene(layout, 300, 300);
VBox layout2 = new VBox(20);
layout2.getChildren().addAll(labelB1, textB1, labelB2, textB2, labelB3,
textB3, labelB4, textB4, labelB5, textB5, labelB6, textB6, buttonB2, buttonB1);
buttonB2.setOnAction(l -> {
try {
BufferedWriter bw = new BufferedWriter(
new FileWriter("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt", true));
bw.write(textB1.getText()+" ");
bw.write(textB2.getText()+" ");
bw.write(textB3.getText()+" ");
bw.write(textB4.getText()+" ");
bw.write(textB5.getText()+" ");
bw.write(textB6.getText()+" \n");
bw.close();
textB1.clear();
textB2.clear();
textB3.clear();
textB4.clear();
textB5.clear();
textB6.clear();
} catch (Exception e) {
return;
}
r.openFile();
r.readFile();
r.closeFile();
});
scene2 = new Scene(layout2, 400, 600);
VBox layout3 = new VBox(20);
layout3.setAlignment(Pos.CENTER);
layout3.getChildren().addAll(buttonC1);
scene3 = new Scene(layout3, 300, 300);
window.setScene(scene1);
window.show();
}
public static void main (String[]args){
launch(args);
}
}
readTutor Class
import javafx.scene.control.Label;
import java.io.*;
import java.util.*;
public class readTutor {
private Scanner x;
public Label tutorLabel;
public void openFile(){
try {
x = new Scanner(new File("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt"));
}
catch(Exception e){
System.out.println("couldn't find file");
}
}
public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
String e = x.next();
String f = x.next();
System.out.printf("%s %s %s %s %s %s\n", a, b, c, d, e, f);
tutorLabel = new Label(a+b+c+d+e+f);
}
}
public void closeFile(){
x.close();
}
}
What I want is to use the Label created in the readFile method in the readTutor Class, in scene3 and display that label there. But I can't figure out how to use that label from the Main class.
You should make an Object to store you're data or pass them to the target location.
There is much that is very, very wrong about the structure of code in the question. Mostly that it does all kinds of file handling and I/O on the FXAT, which will cause havoc with the GUI if there is any kind of lag with those operations.
Also, the idea of having a file handling helper class with a JavaFX screen Node in it is really not a good design. #Kleopatra's comment about using a data model is totally on point.
But in the spirit of answering the question about exposing the layout of a screen to other classes which have a legitimate reason to access it, I've made the required changes to the code so that it should work.
First, I've added a TextField and Label to the Main screen to show the results from the readTutor class. Then I've passed the TextField to readTutor in it's constructor:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class LabelPasser extends Application {
Stage window;
Scene scene1, scene2, scene3;
private Scanner x;
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Tutor Finder");
Label labelA1 = new Label("Welcome to the best online tutor\nproviding service in the world");
Button buttonA2 = new Button("Find a tutor!");
Button buttonA1 = new Button("Become a tutor!");
Button buttonB1 = new Button("Click here to go back");
Button buttonB2 = new Button("Submit");
Button buttonC1 = new Button("Click here to go back");
buttonB1.setOnAction(e -> window.setScene(scene1));
buttonC1.setOnAction(e -> window.setScene(scene1));
buttonA1.setOnAction(e -> window.setScene(scene2));
buttonA2.setOnAction(e -> window.setScene(scene3));
Label labelB1 = new Label("First Name:");
TextField textB1 = new TextField();
Label labelB2 = new Label("Last Name:");
TextField textB2 = new TextField();
Label labelB3 = new Label("Subject:");
TextField textB3 = new TextField();
Label labelB4 = new Label("Age:");
TextField textB4 = new TextField();
Label labelB5 = new Label("Contact No#:");
TextField textB5 = new TextField();
Label labelB6 = new Label("Country: ");
TextField textB6 = new TextField();
Label labelTutor = new Label("Tutor: ");
TextField tutorTF = new TextField();
VBox layout = new VBox(20);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(labelA1, buttonA1, buttonA2);
readTutor r = new readTutor(tutorTF);
scene1 = new Scene(layout, 300, 300);
VBox layout2 = new VBox(20);
layout2.getChildren()
.addAll(labelB1,
textB1,
labelB2,
textB2,
labelB3,
textB3,
labelB4,
textB4,
labelB5,
textB5,
labelB6,
textB6,
labelTutor,
tutorTF,
buttonB2,
buttonB1);
buttonB2.setOnAction(l -> {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt", true));
bw.write(textB1.getText() + " ");
bw.write(textB2.getText() + " ");
bw.write(textB3.getText() + " ");
bw.write(textB4.getText() + " ");
bw.write(textB5.getText() + " ");
bw.write(textB6.getText() + " \n");
bw.close();
textB1.clear();
textB2.clear();
textB3.clear();
textB4.clear();
textB5.clear();
textB6.clear();
} catch (Exception e) {
return;
}
r.openFile();
r.readFile();
});
scene2 = new Scene(layout2, 400, 600);
VBox layout3 = new VBox(20);
layout3.setAlignment(Pos.CENTER);
layout3.getChildren().addAll(buttonC1);
scene3 = new Scene(layout3, 300, 300);
window.setScene(scene1);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
Then I've modified readTutor such that it no longer creates a Label, but instead updates the Text property of the TextField that it received in its constructor:
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.io.File;
import java.util.Scanner;
public class readTutor {
private Scanner x;
public Label tutorLabel;
TextField tutorTF;
public readTutor(TextField tutorTF) {
this.tutorTF = tutorTF;
}
public void openFile() {
try {
x = new Scanner(new File("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt"));
} catch (Exception e) {
System.out.println("couldn't find file");
}
}
public void readFile() {
while (x.hasNext()) {
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
String e = x.next();
String f = x.next();
System.out.printf("%s %s %s %s %s %s\n", a, b, c, d, e, f);
tutorTF.setText(a + b + c + d + e + f);
}
}
}
It turns out that if you program the file handling the way you should, with a Task, then most of the issues about the label passing become moot. I've cleaned up the code a bit to make it easier to read, then split the file handling out into a Task:
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class LabelPasser extends Application {
Stage window;
Scene scene1, scene2, scene3;
private Scanner x;
private TextField textB1 = new TextField();
private TextField textB2 = new TextField();
private TextField textB3 = new TextField();
private TextField textB4 = new TextField();
private TextField textB5 = new TextField();
private TextField textB6 = new TextField();
private TextField tutorTF = new TextField();
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Tutor Finder");
Button buttonA2 = new Button("Find a tutor!");
Button buttonA1 = new Button("Become a tutor!");
Button buttonB1 = new Button("Click here to go back");
Button buttonB2 = new Button("Submit");
Button buttonC1 = new Button("Click here to go back");
buttonB1.setOnAction(e -> window.setScene(scene1));
buttonC1.setOnAction(e -> window.setScene(scene1));
buttonA1.setOnAction(e -> window.setScene(scene2));
buttonA2.setOnAction(e -> window.setScene(scene3));
VBox layout = new VBox(20, new Label("Welcome to the best online tutor\nproviding service in the world"), buttonA1, buttonA2);
layout.setAlignment(Pos.CENTER);
scene1 = new Scene(layout, 300, 300);
VBox layout2 = new VBox(20);
layout2.getChildren()
.addAll(new Label("First Name:"),
textB1,
new Label("Last Name:"),
textB2,
new Label("Subject:"),
textB3,
new Label("Age:"),
textB4,
new Label("Contact No#:"),
textB5,
new Label("Country: "),
textB6,
new Label("Tutor: "),
tutorTF,
buttonB2,
buttonB1);
buttonB2.setOnAction(evt -> buttonAction());
scene2 = new Scene(layout2, 400, 600);
VBox layout3 = new VBox(20);
layout3.setAlignment(Pos.CENTER);
layout3.getChildren().addAll(buttonC1);
scene3 = new Scene(layout3, 300, 300);
window.setScene(scene1);
window.show();
}
private void buttonAction() {
Task<String> fileTask = new Task<String>() {
#Override
protected String call() throws Exception {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt", true));
bw.write(textB1.getText() + " ");
bw.write(textB2.getText() + " ");
bw.write(textB3.getText() + " ");
bw.write(textB4.getText() + " ");
bw.write(textB5.getText() + " ");
bw.write(textB6.getText() + " \n");
bw.close();
textB1.clear();
textB2.clear();
textB3.clear();
textB4.clear();
textB5.clear();
textB6.clear();
} catch (Exception e) {
return "";
}
ReadTutor readTutor = new ReadTutor();
readTutor.openFile();
return readTutor.readFile();
}
};
fileTask.setOnSucceeded(evt -> tutorTF.setText(fileTask.getValue()));
Thread fileThread = new Thread(fileTask);
fileThread.start();
}
public static void main(String[] args) {
launch(args);
}
}
The readFile() method in ReadTutor was strange, and it looked like it was trying to create Labels in a loop, so the file reading is weird. I modified it just so that it would compile:
import java.io.File;
import java.util.Scanner;
public class ReadTutor {
private Scanner x;
public void openFile() {
try {
x = new Scanner(new File("C:\\Users\\Salman\\Desktop\\Tutor Details\\output.txt"));
} catch (Exception e) {
System.out.println("couldn't find file");
}
}
public String readFile() {
String results = "";
while (x.hasNext()) {
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
String e = x.next();
String f = x.next();
System.out.printf("%s %s %s %s %s %s\n", a, b, c, d, e, f);
results = a + b + c + d + e + f;
}
return results;
}
}
So now you can see that ReadTutor is just a file handler, and it returns a plain old Java String as a result and doesn't know anything about JavaFX at all. The Task handles the activity on a background thread and returns the String value from the read operation. When the Task completes, it will trigger the OnSucceeded event, which will update the value in the tutorTF TextField on the FXAT.
I have two classes ViewTaskTest and ControllerTaskTest. The view has two buttons, one to create a random point and the other to start the controller. After the start, the controller will place 10 random points to the chart inside the view. But the points are visible only at the end. I want to see the points being placed one by one. I know, that I have to use somehow the Task-functions and I am struggling to understand this concept.
This is the ViewTaskTest-class:
package View;
import Controller.ControllerTaskTest;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ViewTaskTest{
ScatterChart<Number, Number> scatterChart;
XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
Axis<Number> xAxis = new NumberAxis(0, 10, 2); ;
Axis<Number> yAxis = new NumberAxis(0, 10, 2); ;
Button buttonAddRandomPoint = new Button("Add random point");
Button buttonStartControllerTest = new Button("Start controller");
private final int MAIN_WINDOW_HEIGHT = 800;
private final int MAIN_WINDOW_WIDTH = 800;
ControllerTaskTest controller;
public ViewTaskTest() {
}
public void setController(ControllerTaskTest controller) {
this.controller = controller;
}
public void fillStage(Stage stage) {
stage.setTitle("QuickPID");
stage.setMinHeight(MAIN_WINDOW_HEIGHT);
stage.setMinWidth(MAIN_WINDOW_WIDTH);
stage.setMaxHeight(MAIN_WINDOW_HEIGHT);
stage.setMaxWidth(MAIN_WINDOW_WIDTH);
Scene sceneOne = new Scene(new Group());
scatterChart = new ScatterChart<Number, Number>(xAxis, yAxis);
scatterChart.getData().add(series);
// add a random point
buttonAddRandomPoint.setOnAction(e -> {
Double x = new Double(0.0);
Double y = new Double(0.0);
try{
x = Math.random() * 10;
y = Math.random() * 10;
series.getData().add(new XYChart.Data<Number, Number>(x, y));
} catch (Exception ex) {
System.out.println("Error");
}
});
buttonStartControllerTest.setOnAction(e -> {
controller.startAddingPoints();
});
VBox vboxButtons = new VBox(5);
HBox hboxMain = new HBox(5);
vboxButtons.getChildren().addAll(buttonAddRandomPoint, buttonStartControllerTest);
vboxButtons.setAlignment(Pos.BOTTOM_LEFT);
vboxButtons.setPadding(new Insets(10));
hboxMain.getChildren().addAll(scatterChart, vboxButtons);
sceneOne.setRoot(hboxMain);
stage.setScene(sceneOne);
}
public void addRandomPointFromController(Double x, Double y) {
System.out.println("starting view task");
Task<Integer> task = new Task<Integer>() {
#Override protected Integer call() throws Exception {
series.getData().add(new XYChart.Data<Number, Number>(x, y));
return 0;
}
};
task.run();
}
}
Here is the ControllerTestTask-Class:
package Controller;
import View.ViewTaskTest;
import javafx.concurrent.Task;
public class ControllerTaskTest {
ViewTaskTest view;
public ControllerTaskTest() {
}
public void setView(ViewTaskTest view) {
this.view = view;
}
public void startAddingPoints() {
System.out.println("starting controller task");
Task<Integer> task = new Task<Integer>() {
#Override protected Integer call() throws Exception {
for(int i = 0; i < 10; i++) {
Double x = Math.random() * 10;
Double y = Math.random() * 10;
view.addRandomPointFromController(x, y);
System.out.println("Adding point x = " + x + " and y = " + y);
Thread.sleep(100);
}
return 0;
}
};
task.run();
}
}
Here is the main-class:
package Test;
import Controller.ControllerTaskTest;
import View.ViewTaskTest;
import javafx.application.Application;
import javafx.stage.Stage;
public class Test extends Application{
private ViewTaskTest view= new ViewTaskTest();
private ControllerTaskTest controller = new ControllerTaskTest();
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws Exception {
view.setController(controller);
controller.setView(view);
stage.show();
view.fillStage(stage);
}
}
Some please explain me what I am doing wrong. I am experimenting with the Thread-funcitons but I cant solve my problem.
This question already has answers here:
JavaFX launch another application
(3 answers)
Closed 4 years ago.
I've got an app made in javafx and another class with menu in this project. In this menu I've got two buttons and one works (exit buuton) and I want buttonStart to open my Main class. How to launch it?
Button buttonStart = new Button("START GAME");
Button buttonExit = new Button("EXIT");
buttonExit.setOnMouseClicked(event -> System.exit(0));
My menu:
package pl.main;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Menu extends Application {
private BorderPane layout;
private Scene scene;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage window) throws Exception {
layout = new BorderPane();
scene = new Scene(layout, 720, 480);
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #F9A825;");
Button buttonStart = new Button("START GAME");
buttonStart.setPrefSize(100, 20);
buttonStart.setStyle("-fx-background-color: #E65100;");
Button buttonExit = new Button("EXIT");
buttonExit.setPrefSize(100, 20);
buttonExit.setStyle("-fx-background-color: #E65100;");
buttonExit.setOnMouseClicked(event -> System.exit(0));
hbox.getChildren().addAll(buttonStart, buttonExit);
layout.setCenter(hbox);
window.setScene(scene);
window.show();
}
}
Class which I wanna launch:
package pl.main;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
public class Main extends Application {
private HashMap<KeyCode, Boolean> keys = new HashMap<KeyCode, Boolean>();
private ArrayList<Node> blocks = new ArrayList<Node>();
private Pane appRoot = new Pane();
private Pane gameRoot = new Pane();
private Pane uiRoot = new Pane();
private Node player;
private Point2D playerGoDown = new Point2D(0, 0);
private Point2D playerGoRight = new Point2D(0, 0);
private boolean canJump = true;
private int levelWidth;
private void initContent() {
Rectangle background = new Rectangle(720, 480);
// BackgroundFill(Color.WHITE);
levelWidth = LevelData.LEVEL1[0].length();
for (int i = 0; i < LevelData.LEVEL1.length; i++) {
String map = LevelData.LEVEL1[i] + LevelData.LEVEL2[i]+ LevelData.LEVEL1[i]+ LevelData.LEVEL2[i]+ LevelData.LEVEL1[i]+ LevelData.LEVEL2[i];
String line = map;
for (int j = 0; j < line.length(); j++) {
switch (line.charAt(j)) {
case '0':
break;
case '1':
Node block = createEntity(j * 30, i * 30, 30, 30, Color.ORANGE);
blocks.add(block);
break;
}
}
}
player = createEntity(0, 350, 40, 40, Color.YELLOW);
// a ????????????
player.translateXProperty().addListener((a, old, newValue) -> {
int offset = newValue.intValue();
//if (offset > 360 && offset < levelWidth - 360) {
gameRoot.setLayoutX(-(offset - 360));
//}
});
appRoot.getChildren().addAll(background, gameRoot, uiRoot);
}
private void update() {
if (isPressed(KeyCode.W) && player.getTranslateY() >= 0) {
jumpPlayer();
}
if (playerGoDown.getY() < 10) {
playerGoDown = playerGoDown.add(0, 1);
}
movePlayerY((int) playerGoDown.getY());
if (player.getTranslateX() <= levelWidth - 5) {
// movePlayerX(5);
movePlayerRight();
}
if (playerGoRight.getX() < 0) {
playerGoRight = playerGoRight.add(0, 1);
}
movePlayerX((int) playerGoRight.getX());
}
private void movePlayerX(int value) {
boolean movingRight = value > 0;
for (int i = 0; i < Math.abs(value); i++) {
for (Node block : blocks) {
if (player.getBoundsInParent().intersects(block.getBoundsInParent())) {
if (movingRight) {
if (player.getTranslateX() + 40 == block.getTranslateX()) {
return;
}
} else {
if (player.getTranslateX() == block.getTranslateX() + 60) {
return;
}
}
}
}
player.setTranslateX(player.getTranslateX() + (movingRight ? 1 : -1));
}
}
private void movePlayerY(int value) {
boolean movingDown = value > 0;
for (int i = 0; i < Math.abs(value); i++) {
for (Node block : blocks) {
if (player.getBoundsInParent().intersects(block.getBoundsInParent())) {
if (movingDown) {
if (player.getTranslateY() + 40 == block.getTranslateY()) {
canJump = true;
return;
}
} else {
if (player.getTranslateY() == block.getTranslateY() + 60) {
return;
}
}
}
}
player.setTranslateY(player.getTranslateY() + (movingDown ? 1 : -1));
}
}
private void jumpPlayer() {
if (canJump) {
playerGoDown = playerGoDown.add(0, -10);
canJump = false;
}
}
private void movePlayerRight() {
playerGoRight = playerGoRight.add(10, 0);
}
private Node createEntity(int x, int y, int w, int h, Color color) {
Rectangle entity = new Rectangle(w, h);
entity.setTranslateX(x);
entity.setTranslateY(y);
entity.setFill(color);
gameRoot.getChildren().add(entity);
return entity;
}
private boolean isPressed(KeyCode key) {
return keys.getOrDefault(key, false);
}
#Override
public void start(Stage primaryStage) throws Exception {
initContent();
Scene scene = new Scene(appRoot);
scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
primaryStage.setTitle("Jetpack gameplay");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
update();
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
}
In future I wanna change current Main.java into ordinary class because now I have two classes which works independently.
If you want to go on another page, you have to load the game scene.
Look at Scene class in JavaDoc.
Once you have your scene, you just have to add an ActionEvent to your button that will display your page.
I am tasked with creating a DVD Collection application that displays Three columns and five row of stuff. I have written the code for this to work but keep on getting compilation errors( CANNOT FIND SYMBOL ==> class DVDColletionApp ) that I am having a hard time debugging.
Below is the code in question. Assistance would be greatly appreciated.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
public class DVDCollectionApp extends Application {
private DVDCollection model;
private ListView<String> tList;
private ListView<Integer> yList, lList;
public DVDCollectionApp() {
model = DVDCollection.example1();
}
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
// Create the labels
HBox labelPane = new HBox();
labelPane.setPadding(new Insets(0,0,0,10));
labelPane.setSpacing(10);
Label label1 = new Label("Title");
label1.setMinSize(300,30);
label1.setPrefSize(2000,30);
Label label2 = new Label("Year");
label2.setMinSize(60,30);
label2.setPrefSize(60,30);
Label label3 = new Label("Length");
label3.setMinSize(60,30);
label3.setPrefSize(60,30);
labelPane.getChildren().addAll(label1, label2, label3);
borderPane.setTop(labelPane);
// Create the lists
GridPane listPane = new GridPane();
listPane.setPadding(new Insets(10));
listPane.setHgap(10);
tList = new ListView<String>();
listPane.add(tList, 0, 0);
tList.setMinSize(300,60);
tList.setPrefSize(2000,2000);
yList = new ListView<Integer>();
listPane.add(yList, 1, 0);
yList.setMinSize(60,60);
yList.setPrefSize(60,500);
lList = new ListView<Integer>();
listPane.add(lList, 2, 0);
lList.setMinSize(60,60);
lList.setPrefSize(60,500);
borderPane.setCenter(listPane);
// Create the button pane
HBox buttonPane = new HBox();
buttonPane.setPadding(new Insets(10));
buttonPane.setSpacing(10);
Button addButton = new Button("Add");
addButton.setStyle("-fx-font: 12 arial; -fx-base: rgb(0,100,0); -fx-text-fill: rgb(255,255,255);");
addButton.setPrefSize(90,30);
Button deleteButton = new Button("Delete");
deleteButton.setStyle("-fx-font: 12 arial; -fx-base: rgb(200,0,0); -fx-text-fill: rgb(255,255,255);");
deleteButton.setPrefSize(90,30);
Button statsButton = new Button("Stats");
statsButton.setStyle("-fx-font: 12 arial;");
statsButton.setPrefSize(90,30);
buttonPane.getChildren().addAll(addButton, deleteButton, statsButton);
borderPane.setBottom(buttonPane);
addButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
String title = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Title: ");
String year = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Year: ");
String length = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Duration: ");
if ((title != null) && (year != null) && (length != null) && (title.length() > 0) && (year.length() > 0) && (length.length() > 0)) {
DVD d = new DVD(title, Integer.parseInt(year), Integer.parseInt(length));
model.add(d);
update(model, -1);
}
}
});
deleteButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
if (tList.getSelectionModel().getSelectedItem() != null) {
model.remove(tList.getSelectionModel().getSelectedItem());
update(model, -1);
}
}
});
tList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(tList.getSelectionModel().getSelectedIndex());
update(model, tList.getSelectionModel().getSelectedIndex());
}
});
yList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(yList.getSelectionModel().getSelectedIndex());
update(model, yList.getSelectionModel().getSelectedIndex());
}
});
lList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(lList.getSelectionModel().getSelectedIndex());
update(model, lList.getSelectionModel().getSelectedIndex());
}
});
// Populate the lists
DVD[] theList = model.getDVDList();
String[] titles = new String[theList.length];
Integer[] years = new Integer[theList.length];
Integer[] lengths = new Integer[theList.length];
for (int i=0; i<theList.length; i++) {
titles[i] = theList[i].getTitle();
years[i] = theList[i].getYear();
lengths[i] = theList[i].getDuration();
}
tList.setItems(FXCollections.observableArrayList(titles));
yList.setItems(FXCollections.observableArrayList(years));
lList.setItems(FXCollections.observableArrayList(lengths));
primaryStage.setTitle("My DVD Collection");
primaryStage.setScene(new Scene(borderPane, 600, 300));
primaryStage.show();
}
// Update the view to reflect the model
public void update(DVDCollection model, int selectedDVD) {
DVD[] theList = model.getDVDList();
String[] titles = new String[theList.length];
Integer[] years = new Integer[theList.length];
Integer[] lengths = new Integer[theList.length];
for (int i=0; i<theList.length; i++) {
titles[i] = theList[i].getTitle();
years[i] = theList[i].getYear();
lengths[i] = theList[i].getDuration();
}
tList.setItems(FXCollections.observableArrayList(titles));
yList.setItems(FXCollections.observableArrayList(years));
lList.setItems(FXCollections.observableArrayList(lengths));
tList.getSelectionModel().select(selectedDVD);
yList.getSelectionModel().select(selectedDVD);
lList.getSelectionModel().select(selectedDVD);
}
public static void main(String[] args) {
launch(args);
}
}
Have you checked that java files are being compiled correctly?
The class DVDCollection doesn't seem to be imported, the DVDCollectionApp class must be in a .java of the same name and if you're using an IDE be sure to do a clean&build.
I have a JavaFX application whose main job is to display different tables containing data loaded from CSV files. I can read CSV files into TableViews with no problems, and export them to CSVs as well. My problem is displaying the different TableViews.
When I load from a CSV into a TableView, I want to save that TableView for future processing. I also want to display a table showing the newly loaded data. My strategy is to have a global TableView, call it table, that is added to the main VBox, and update its contents to contain (and hence display) the elements of whichever TableView I just created by reading a file.
Is this a bad idea? It seems like a simple concept to me, but I can't find a way to easily "clone" the chosen TableView - i.e. transfer its contents to my displayed TableView called table.
My simple attempt at cloning TableViews is shown here. I also tried writing an extensive equateTable() method, which went nowhere.
//WHY DOESN'T THIS WORK?
table.setItems(reqTable.getItems());
Here's the rest of the code. Thanks to anyone who answers!
package FLOOR;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;
// --- Main Class
public class Main extends Application {
// --- All Tables
TableView<ObservableList<StringProperty>> reqTable = new TableView<>();
TableView<ObservableList<StringProperty>> tempTable = new TableView<>();
TableView<ObservableList<StringProperty>> ontTable = new TableView<>();
// --- Display Table
TableView<ObservableList<StringProperty>> table = new TableView<>();
// --- Main
public static void main(String[] args) {
launch(args);
}
// --- Start
#Override
public void start(Stage stage) {
// --- Stage & Scene
stage.setTitle("FLOOR");
Scene scene = new Scene(new VBox(), 900, 500);
MenuBar menuBar = new MenuBar();
// --- VBox
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
vbox.getChildren().addAll(table);
table.setVisible(false);
// --- Menus
// --- File Menu
// --- Import Submenu
Menu menuFile = new Menu("File");
Menu importMenu = new Menu("Import");
MenuItem reqOption = new MenuItem("Requirements");
MenuItem tempOption = new MenuItem("Templates");
MenuItem ontOption = new MenuItem("Ontologies");
importMenu.getItems().addAll(reqOption, tempOption, ontOption);
//Import Requirements
reqOption.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Requirements CSV");
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
populateTable(reqTable, file.getAbsolutePath());
}
getRequirementsPage();
}
});
//Import Templates
tempOption.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Templates CSV");
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
populateTable(tempTable, file.getAbsolutePath());
}
getTemplatesPage();
}
});
//Import Ontologies
ontOption.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Ontology CSV");
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
populateTable(ontTable, file.getAbsolutePath());
}
getOntologiesPage();
}
});
//Export
MenuItem export = new MenuItem("Export");
export.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Requirements CSV");
File file = fileChooser.showSaveDialog(stage);
if (file != null) {
exportTable(reqTable, file.getAbsolutePath());
}
}
});
//Exit
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(importMenu, export, new SeparatorMenuItem(), exit);
// --- Menu Bar
menuBar.getMenus().addAll(menuFile);
// --- Show FLOOR
((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
stage.setScene(scene);
stage.show();
}
// --- Methods
// Table Getters
private void getRequirementsPage() {
table.getItems().clear();
//WHY DOESN'T THIS WORK?
table.setItems(reqTable.getItems());
table.setVisible(true);
}
private void getTemplatesPage() {
table.getItems().clear();
table.setItems(tempTable.getItems());
table.setVisible(true);
}
private void getOntologiesPage() {
table.getItems().clear();
table.setItems(ontTable.getItems());
table.setVisible(true);
}
//populateTable
private void populateTable(
final TableView<ObservableList<StringProperty>> table,
final String filename) {
table.getItems().clear();
table.getColumns().clear();
table.setPlaceholder(new Label("Loading..."));
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
CSVReader reader = new CSVReader(new FileReader(filename));
String [] nextLine;
int count = 1;
while ((nextLine = reader.readNext()) != null) {
if (count == 1) {
final String[] headers = nextLine;
Platform.runLater(new Runnable() {
#Override
public void run() {
for (int column = 0; column < headers.length; column++) {
table.getColumns().add(
createColumn(column, headers[column]));
}
}
});
} else {
final String[] dataValues = nextLine;
Platform.runLater(new Runnable() {
#Override
public void run() {
// Add additional columns if necessary:
for (int columnIndex = table.getColumns().size(); columnIndex < dataValues.length; columnIndex++) {
table.getColumns().add(createColumn(columnIndex, ""));
}
// Add data to table:
ObservableList<StringProperty> data = FXCollections
.observableArrayList();
for (String value : dataValues) {
data.add(new SimpleStringProperty(value));
}
table.getItems().add(data);
}
});
}
count++;
}
reader.close();
return null;
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
//exportTable
private void exportTable(
final TableView<ObservableList<StringProperty>> table,
final String filename) {
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(filename), ',');
} catch (IOException e) {
e.printStackTrace();
}
int numRows = table.getItems().size();
int numCols = table.getColumns().size();
String[] dataWrite = new String[numCols];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
dataWrite[j] = table.getItems().get(i).get(j).getValue();
}
writer.writeNext(dataWrite);
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
//createColumn
private TableColumn<ObservableList<StringProperty>, String> createColumn(
final int columnIndex, String columnTitle) {
TableColumn<ObservableList<StringProperty>, String> column = new TableColumn<>();
String title;
if (columnTitle == null || columnTitle.trim().length() == 0) {
title = "Column " + (columnIndex + 1);
} else {
title = columnTitle;
}
column.setText(title);
column
.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<StringProperty>, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(
CellDataFeatures<ObservableList<StringProperty>, String> cellDataFeatures) {
ObservableList<StringProperty> values = cellDataFeatures.getValue();
if (columnIndex >= values.size()) {
return new SimpleStringProperty("");
} else {
return cellDataFeatures.getValue().get(columnIndex);
}
}
});
return column;
}
}