I build a gridpane in scenebuilder.I have a image view in every cell.I want to build a dynamic gallery pictures.I want to remove the last image in every row and add it to the first column in next row?How can I do it?I am beginer in javafx, please help me :(
Thank you
Not tested, but this should work:
// if you know how many columns you have (and their indexes) this step is unnecessary:
int minColIndex = Integer.MAX_VALUE ;
int maxColIndex = Integer.MIN_VALUE ;
for (Node node : gridPane.getChildren()) {
int colIndex = GridPane.getColumnIndex(node);
if (colIndex < minColIndex) minColIndex = colIndex ;
if (colIndex > maxColIndex) maxColIndex = colIndex ;
}
// Update row and column indexes:
for (Node node : gridPane.getChildren()) {
int colIndex = GridPane.getColumnIndex(node);
if (colIndex == maxColIndex) {
int rowIndex = GridPane.getRowIndex(node);
GridPane.setRowIndex(node, rowIndex+1);
GridPane.setColIndex(node, minColIndex);
} else {
GridPane.setColIndex(node, colIndex + 1) ;
}
}
Are you sure a TilePane wouldn't suit your needs better, though?
Related
I have a problem when I'm trying to a loop in a DataTable that a dataset contains.
I'm doing a loop like this:
for(int i = 0; i<ds.Tables[0].Rows.Count - 1 ; i++)
The problem is that I can't get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I'll have an out of range exception.
This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:
if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])
So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it's out of the table.
So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn't work.
if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)
If anyone has an idea, let me know, and of course if it is not clear enough let me know, I'll give more information and more code.
Thanks for your help and your time!
Check if it's the last record, first.
I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.
Try to make each line of code do one thing, and one thing only, like check if it is the last row:
var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;
for(int i = 0; i < lastRow ; i++)
{
if (i == lastRow){
// This is the last row. Handle the last row here.
}
else
{
// Handle all other rows here
var currentRecord = data[i];
var nextRecord = data[i + 1];
if (currentRecord["Release_No"] != nextRecord["Release_No"])
{
// Handle unique Releases...
}
}
}
Use less than or equal to like this
for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)
I hope this may get what you want.
Something like this is better ?
var lastRow = data.Count - 1;
var data = ds.Tables[0].Rows;
for(int i = 0; i< lastRow; i++)
{
testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);
if ( i == lastRow)
{
if (DBNull.Value.Equals(data[i]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
else
{
var col = ds.Tables[0].Columns;
var currentRecord = data[i];
var nextRecord = data[i + 1];
if(currentRecord["Release_No"] != nextRecord["Release_No"])
{
for (int j = col[2].Ordinal; j < col.Count; j++)
{
if (DBNull.Value.Equals(data[i][j]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i][j]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
}
}
}
I have a QTableWidget with some columns and rows and want to add a filter for a particular column.
For that, I've added a QLineEdit in my Window.
I'm already able to filter the rows, when I add only one number in the QLineEdit:
for(int i=0; i<tableWidget->rowCount(); i++)
{
if(!tableWidget->item(i, column)->text().contains(lineEdit->text()))
{
tableWidget->hideRow(i);
}
}
(The slot is connected to the textEdited-Signal of the LineEdit)
What I want to do now:
When I write something like this in the QLineEdit: 10-30; Hide all rows, which doesnt have the number between 10 and 30 (>=10; <=30).
Somebody has an idea, how I can solve this?
This is my decision.
Check if lineEdit text contains two numbers.
QString test = ui->lineEdit->text();
QStringList lst = test.split('-');
if (lst.size() == 2)
Transform they in to integers.
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
Now let's go to tableWidget and drop in less than or equal to these two numbers.
for (int i = 1; i <= 100; i++) {
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(i)));
}
connect(ui->lineEdit, &QLineEdit::textChanged, this, [=](const QString &test) {
QStringList lst = test.split('-');
if (lst.size() == 2) {
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
int temp = ui->tableWidget->item(i, 0)->text().toInt();
if (temp < low || temp > high) {
ui->tableWidget->hideRow(i);
} else {
ui->tableWidget->showRow(i);
}
}
}
});
Okay so I'm trying to highlight all the nodes in a vertical column in a gridpane when I mouse over any node in the column. So right now I'm getting the columnIndex of the node my mouse is over and then creating an array of all nodes that share that column index. Return that array to the main method and then change the background color of the nodes in the array to a color.
This is the mouse over function:
for (Node node : officeHoursGridPane.getChildren()) {
node.setOnMouseEntered((MouseEvent t) -> {
node.setStyle("-fx-background-color:#f9f3c5;");
Node source = (Node)t.getSource();
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
//ystem.out.println("Column #: " + colIndex + "\nRow #: " + rowIndex);
for(int c = 0; c <= colIndex; c++){
Node[] colNode = getNodeByColumnIndex(colIndex, officeHoursGridPane);
int colCount=0;
for(int v = 0; v <= colNode.length; v++){
Node vertNode = colNode[v];
vertNode.setStyle("-fx-background-color:#f9f3c5;");
}
}
});
node.setOnMouseExited((MouseEvent t) -> {
node.setStyle("-fx-background-color:#ffffff;");
});
}
This is my Node[] builder:
public Node[] getNodeByColumnIndex (final int column, GridPane gridPane) {
Node[] result = null;
ObservableList<Node> childrens = gridPane.getChildren();
int count = 0;
for (Node node : childrens) {
if(GridPane.getColumnIndex(node) == column) {
result[count] = node;
count++;
if(count > column){
break;
}
}
}
return result;
}
You should find all the nodes with the same column index in the gridpane's children:
for (Node node : officeHoursGridPane.getChildren()) {
node.setOnMouseEntered(e -> officeHoursGridPane.getChildren().forEach(c -> {
Integer targetIndex = GridPane.getColumnIndex(node);
if (GridPane.getColumnIndex(c) == targetIndex) {
c.setStyle("-fx-background-color:#f9f3c5;");
}
}));
node.setOnMouseExited(e -> officeHoursGridPane.getChildren().forEach(c -> {
Integer targetIndex = GridPane.getColumnIndex(node);
if (GridPane.getColumnIndex(c) == targetIndex) {
c.setStyle("-fx-background-color:#ffffff;");
}
}));
}
Note, that in order to not highlight extra nodes you probably should also check the row index as well.
I have done this in java where I sum the values of the price column/row. But I am wondering how to do this in JavaFX.
I want to sum everything in column 1 and display it in a inputField, I used this to do it in java but how do you do this in JavaFX?
tableview.getValueAt(i, 1).toString();
Here is what I'm trying to do:
int sum = 0;
for (int i = 0; i < tableview.getItems().size(); i++) {
sum = sum + Integer.parseInt(tableview.getValueAt(i, 1).toString());
}
sumTextField.setText(String.valueOf(sum));
If you really have a TableView<Integer>, which seems to be what you are saying in the comments, you can just do
TableView<Integer> table = ... ;
int total = 0 ;
for (Integer value : table.getItems()) {
total = total + value;
}
or, using a Java 8 approach:
int total = table.getItems().stream().summingInt(Integer::intValue);
If you have a more standard set up with an actual model class for your table, then you would need to iterate through the items list and call the appropriate get method on each item, then add the result to the total. E.g. something like
TableView<Item> table = ...;
int total = 0 ;
for (Item item : table.getItems()) {
total = total + item.getPrice();
}
or, again in Java 8 style
int total = table.getItems().stream().summingInt(Item::getPrice);
Both of these assume you have an Item class with a getPrice() method, and the column in question is displaying the price property of each item.
public void totalCalculation (){
double TotalPrice = 0.0;
TotalPrice = Yourtable.getItems().stream().map(
(item) -> item.getMontant()).reduce(TotalPrice, (accumulator, _item) -> accumulator + _item);
TexfieldTotal.setText(String.valueOf(TotalPrice));
}
//getMontant is the getter of your column
I need to get a list of all display methods in a table, and I can't seem to find anything about this on the web.
Anyone know how to do this?
On display methods displayType is set to DisplayFunctionType::Get.
DictTable dt = new DictTable(tableNum(VendTable));
DictMethod dm;
DisplayFunctionType dft;
DictType dEdt;
int mtdCnt = dt.objectMethodCnt();
int i;
setPrefix(strFmt("Table: %1", dt.name()));
for (i = 1; i <= mtdCnt; i++)
{
dm = dt.objectMethodObject(i);
dft = dm.displayType();
if (dft == DisplayFunctionType::Get)
{
dEdt = new DictType(dm.returnId());
info(strFmt("Method: %1 (Label: %2)", dm.name(), dEdt.label()));
}
}