If you want to periodically display new data in your GUI, then you could use "Timeline" (all classes from the javafx.*-packages), e.g. for updating every 1.0 second:
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(1.0), e -> {
/*
* read new data and update GUI nodes here ...
*/
})
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
If you use FXML, a good point to call this piece of code would be in your FXML controller class:
public class ControllerForFXML implements Initializable {
@Override
void initialize(URL location, ResourceBundle resources) {
/*
* Timeline code from above here ...
*/
}
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class FileTest {
public static void main(String[] args) throws IOException {
Path pathToRead = Paths.get("C:\\...\\dataIn.csv");
// Check if file exists
if(!Files.exists(pathToRead)) {
System.out.println("file doesn't exist :-(");
return;
};
// read a file line by line
Files.lines(pathToRead).forEach(line -> {
// Process the String line here!!
System.out.println(line);
});
// write a file line by line
List<String> lines = Arrays.asList("line 1", "line 2", "line 3");
Path pathToWrite = Paths.get("C:\\...\\dataOut.csv");
Files.write(pathToWrite, lines);
}
}
In Files.write() you can use also add Options, e.g. StandardOpenOption.CREATE or StandardOpenOption.APPEND. If omitted like above you get StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING.
Search for a node in a non-binary tree (any node can have multiple children 0-n) and exit from recursion immediately, when the first hit is found. We give an example with the search of a TreeItem with a given name in a TreeView of JavaFX, which can be easily adapted to any tree like structure:
As a prerequisite install Xtext & Xtend 2.5.1 on Eclipse: Help > Install new software... > work with: http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
Install e(fx)clipse: http://www.eclipse.org/efxclipse/install.html (e.g. for version 0.9.0: Help > Install new software... > work with: http://download.eclipse.org/efxclipse/updates-released/0.9.0/site, check the Kepler-version)
If it says "command not found" or similar try this: Add the bin\ directory (e.g. C:\Program Files\Java\jdk1.7.x_xx\bin) of the installed JDK to the PATH environment variable, as described here: http://www.java.com/en/download/help/path.xml
Download Eclipse IDE (same bit version as for the JRE, i.e. 32 or 64 bit) and unzip
Modularity / Loose Coupling (through "separation of duties/concerns" and dependency injection)
Testability (test the different components independently)
Maintainability (e.g. changes can be made in one component without touching the others)
What's in common?
View: responsible for presentation in GUI (GUI components like buttons, labels etc. and layout & styling)
Model: data and behavior (domain objects and logic, services for database interaction)
Controller / Presenter / ViewModel: responsible for separation of the latter two (glue logic!)
What's different?
The associations between the three! (see the picture below) First of all, the MVVM pattern is just a specialization of the MVP, that is more loosely coupled through the concept of data binding.
The difference between MVP/MVVM and MVC is, that the View and the Model are completely decoupled - that's a good thing :-)
The black arrows represent a direct association, the red dotted arrows an indirect association (e.g. Observer Pattern/Events/Data Binding)
A good talk explaining this is "MVC,MVP and MVVM: A Comparison of Architectural Patterns" by Joseph Homnick:
When you need to retrieve the plain Connection from JPA, unfortunately, there is no JPA standard way (unless you get a configured datasource with JNDI). This is needed when you use libraries or legacy applications which use JDBC instead of JPA.
Hibernate 3.x and JPA 1.0
With Hibernate you can get the org.hibernate.session. In pre Hibernate 4.o there was the connection() method which now is removed.
Calculating the determinant of a quadratic matrix A=(a_ij) of size n using Laplace's formula with expanding along the i-th row is given by
where A_ij is the so-called minor (it is A with the i-th row and j-th column removed). Note that it is a recursive definition; it terminates since the size of the matrices decreases and the determinant of a number is the number itself.
In Java it can be implemented like this (for i=1, i.e. the first row):
package de.bigdev;
public class Determinant {
/**
* Determinant of a matrix using Laplace's formula with expanding along the
* 0th row. It is not checked whether the matrix is quadratic!
*
* @param m Matrix
* @return determinant
*/
public static double det(double[][] m) {
int n = m.length;
if (n == 1) {
return m[0][0];
} else {
double det = 0;
for (int j = 0; j < n; j++) {
det += Math.pow(-1, j) * m[0][j] * det(minor(m, 0, j));
}
return det;
}
}
/**
* Computing the minor of the matrix m without the i-th row and the j-th
* column
*
* @param m input matrix
* @param i removing the i-th row of m
* @param j removing the j-th column of m
* @return minor of m
*/
private static double[][] minor(final double[][] m, final int i, final int j) {
int n = m.length;
double[][] minor = new double[n-1][n-1];
// index for minor matrix position:
int r = 0, s = 0;
for (int k = 0; k < n; k++) {
double[] row = m[k];
if (k != i) {
for (int l = 0; l < row.length; l++) {
if (l != j) {
minor[r][s++] = row[l];
}
}
r++;
s = 0;
}
}
return minor;
}
private static void printMatrix(double[][] m) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m.length; i++) {
double[] row = m[i];
sb.append("[");
for (int j = 0; j < row.length; j++) {
sb.append(" ");
sb.append(row[j]);
}
sb.append(" ]\n");
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
public static void main(String[] args) {
System.out.println("Determinant");
System.out.println("===========");
double[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
double[][] b = { { 1 } };
double[][] c = { { 1, 2 }, { 3, 4 } };
System.out.println("");
System.out.println("Testing minor");
System.out.println("=============");
printMatrix(a);
System.out.println("deleting row 3, and column 2:");
printMatrix(minor(a, 2, 1));
System.out.println("");
System.out.println("Testing det");
System.out.println("===========");
printMatrix(b);
System.out.println("has det=" + det(b) +"\n");
printMatrix(c);
System.out.println("has det=" + det(c) +"\n");
printMatrix(a);
System.out.println("has det=" + det(a) +"\n");
}
}
When starting to learn object orientated programming, a good first example is the following: Modelling a 2-dimensional vector over the real numbers could result in this
package de.bigdev;
public class Vector {
private double x;
private double y;
public Vector(double x, double y) {
super();
this.x = x;
this.y = y;
}
/**
* Addition of two vectors
*
* @param v
* @return the sum with v
*/
public Vector add(Vector v) {
return new Vector(x + v.x, y + v.y);
}
/**
* Scalar Mulitplication with a constant
*
* @param c
* @return the scalar multiplication with c
*/
public Vector scalarMult(double c) {
return new Vector(c * x, c * y);
}
/**
* Length of two vectors
*
* @return length
*/
public double length() {
return Math.sqrt(x * x + y * y);
}
/**
* Scalar Product of two vectors
*
* @param v
* @return the scalar product with v
*/
public double scalarProd(Vector v) {
return this.x * v.getX() + this.y * v.getY();
}
/**
* Angle between two vectors
*
* @param v
* @return the angle with v in degrees
*/
public double angle(Vector v) {
return Math.acos(this.scalarProd(v) / (this.length() * v.length()))
* 180.0 / Math.PI;
}
public double getX() { return x; }
public double getY() { return y; }
@Override
public String toString() {
return "[" + x + ", " + y + "]";
}
/**
* for testing only. should be factored out into another class... or use
* JUnit!
*
* @param args
*/
public static void main(String[] args) {
Vector v = new Vector(1, 1);
Vector w = new Vector(-1, -1);
System.out.println("Vector algebra");
System.out.println("==============");
System.out.println("addition : " + v + " + " + w + " = " + v.add(w));
System.out.println("scalar multip.: 2 * " + v + " = " + v.scalarMult(2));
System.out.println("length : ||" +v + "|| = " + v.length());
System.out.println("scalar product: " + v + " * " + w + " = " + v.scalarProd(w));
System.out.println("angle : angle(" + v + ", " + w
+ ") = " + Math.round(v.angle(w)) + "°");
}
}
In the cubicles representin’ for my JAVA homies… In by nine, out when the deadlines are met, check it.
We code hard in these cubicles. My style’s nerd-chic, I’m a programmin’ freak.
We code hard in these cubicles. Only two hours to your deadline? Don’t sweat my technique.
Sippin’ morning coffee with that JAVA swirl. Born to code; my first words were “Hello World”
Since 95, been JAVA codin’ stayin’ proud. Started on floppy disks, now we take it to the cloud.
On my desktop, JAVA’s what’s bobbin’ and weavin’. We got another winning app before I get to OddEven.
Blazin’ code like a forest fire, climbin’ a tree. Setting standards like I Triple E….
Boot it on up, I use the force like Luke. Got so much love for my homeboy Duke.
GNU Public Licensed, it’s open source. Stop by my desk when you need a crash course
Written once and my script runs anywhere. Straight thuggin’, mean muggin’ in my Aeron chair.
All the best lines of code, you know I wrote ‘em. I’ll run you out of town on your dial-up modem.
‘Cause… We code hard in these cubicles. Me and my crew code hyphy hardcore.
We code hard in these cubicles. It’s been more than 10 years since I’ve seen the 404.
Inheriting a project can make me go beeee-serk. Ain’t got four hours to transfer their Framework.
The cleaners killed the lights, Man, that ain’t nice. Gonna knock this program out, just like Kimbo Slice
I program all night, just like a champ. Look alive under this IKEA lamp.
I code HARDER in the midnight hour, E7 on the vending machine fuels my power.
Ps3 to Smartphones, our code use never ends. JAVA’s there when I beat you in “Words with Friends”.
My developing skills are so fresh please discuss. You better step your game up on that C++.
We know better than to use Dot N-E-T. Even Dan Brown can’t code as hard as me.
You know JAVA’s gettin’ bigger, that’s a promise not a threat. Let me code it on your brain
so you’ll never forget.
We code hard in these cubicles, it’s the core component…of what we implement.
We code hard in these cubicles. Straight to your JAVA Runtime Environment.
We code hard in these cubicles. Keep the syntax light and the algorithm tight.
We code hard in these cubicles. Gotta use JAVA if it’s gonna run right.
We code hard in these cubicles. JAVA keeps adapting, you know it’s built to last.
We code hard in these cubicles. Robust and secure, so our swag’s on blast
CODE HARD
Yes it does! For integration with Blogger see here! I used the <pre class="brush:xxx">-tag which has some problems with html: you should escape the html characters, e.g. with QuickEscape.