Skip to content

Instantly share code, notes, and snippets.

@trashgod
Last active December 31, 2023 13:58
Show Gist options
  • Save trashgod/2fac616057dcdf1b4e444b5a45954673 to your computer and use it in GitHub Desktop.
Save trashgod/2fac616057dcdf1b4e444b5a45954673 to your computer and use it in GitHub Desktop.
TableView summary
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* https://stackoverflow.com/q/69594347/230513
*/
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("TVCTestModel.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException ex) {
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
launch(args);
}
}
#!/bin/sh
JFX="--module-path /Users/Shared/javafx-sdk-17.0.9/lib --add-modules ALL-MODULE-PATH"
javac $JFX *.java && java $JFX MainApp
public class TestModel {
private String itemName = null;
private int pricePerUnit = 0;
private double quantity = 0.0;
private double amount = 0.0;
public TestModel() {
}
public TestModel(String argitemName, int argpricePerUnit, double argquantity, double argamount) {
itemName = argitemName;
pricePerUnit = argpricePerUnit;
quantity = argquantity;
amount = argamount;
}
public void setItemName(String argitemName) {
itemName = argitemName;
}
public void setPricePerUnit(int argpricePerUnit) {
pricePerUnit = argpricePerUnit;
}
public void setQuantity(double argquantity) {
quantity = argquantity;
}
public void setAmount(double argamount) {
amount = argamount;
}
public String getItemName() {
return itemName;
}
public int getPricePerUnit() {
return pricePerUnit;
}
public double getQuantity() {
return quantity;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return this.itemName + " " + this.pricePerUnit + " " + this.quantity + " " + this.amount;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TVCTestModel">
<center>
<TableView fx:id="tableView" prefHeight="300.0">
<columns>
<TableColumn fx:id="itemName" text="Item Name" />
<TableColumn fx:id="pricePerUnit" text="Price Per Unit" />
<TableColumn fx:id="quantity" text="Quantity" />
<TableColumn fx:id="amount" text="Amount" />
</columns>
</TableView>
</center>
<bottom>
<HBox alignment="CENTER_RIGHT" style="-fx-spacing: 5px;">
<children>
<Label fx:id="labelQ"/>
<Label fx:id="labelA"/>
</children>
</HBox>
</bottom>
</BorderPane>
import java.net.URL;
import java.text.NumberFormat;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class TVCTestModel implements Initializable {
@FXML
private TableColumn<TestModel, String> itemName;
@FXML
private TableColumn<TestModel, Integer> pricePerUnit;
@FXML
private TableColumn<TestModel, Double> quantity;
@FXML
private TableColumn<TestModel, Double> amount;
@FXML
private TableView<TestModel> tableView;
@FXML
private Label labelQ;
@FXML
private Label labelA;
private ObservableList<TestModel> objList = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
this.itemName.setCellValueFactory(new PropertyValueFactory<>("itemName"));
this.pricePerUnit.setCellValueFactory(new PropertyValueFactory<>("pricePerUnit"));
this.quantity.setCellValueFactory(new PropertyValueFactory<>("quantity"));
this.amount.setCellValueFactory(new PropertyValueFactory<>("amount"));
objList.add(new TestModel("Item 1", 10, 4, 400));
objList.add(new TestModel("Item 2", 20, 5, 1000));
objList.add(new TestModel("Item 3", 30, 6, 1800));
objList.add(new TestModel("Item 4", 400, 7, 2800));
tableView.setItems(objList);
double sumQuantity = 0;
double sumAmout = 0;
for (TestModel o : objList) {
sumQuantity += o.getQuantity();
sumAmout += o.getAmount();
}
labelQ.setText("Quantity: " + NumberFormat.getNumberInstance().format(sumQuantity));
labelA.setText("Amount: " + NumberFormat.getCurrencyInstance().format(sumAmout));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment