Results
public class Results {
//Variables
private final StringProperty id;
private final StringProperty firstName;
private final StringProperty name;
...
private final IntegerProperty[] points;
...
...
public StringProperty idProperty() {
return id;
}
...
public IntegerProperty[] getPoints() {
return points;
}
public IntegerProperty propertyAt (int index) {
return points [index];
}
}
However I can’t figure out how to do the same with the IntegerPropertiy[]
#FXML
private TableView<Results> tableView;
#FXML
private TableColumn<Results, String> idColumn;
#FXML
private TableColumn<Results, String> nameColumn;
...
...
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
The table looks like this at the moment:
Points[0] is supposed to be Points 1 and so forth
table
So basically the question is if there is a way I can use the IntegerProperty[] points in a similar way to the other properties and if someone could explain it to me in a simple manner.
If your columns are defined in FXML:
#FXML
private TableColumn<Results, Number> pointsColumn1 ;
// etc...
#FXML
private void initialize() {
// ...
pointsColumn1.setCellValueFactory(cd -> cd.getValue().propertyAt(0));
// etc....
}
Of course it's probably easier to create those columns in Java, rather than in FXML:
#FXML
private void initialize() {
int numPoints = 8 ;
for (int i = 1 ; i <= numPoints ; i++) {
TableColumn<Results, Number> column = new TableColumn<>("Points "+i);
final int index = i - 1 ;
column.setCellValueFactory(cd -> cd.getValue().propertyAt(index));
tableView.getColumns().add(column);
}
// ...
}
Related
I'm trying to do some reports by hiding some rows from a tableview (I made in scenebuilder) by using a checkbox. If the object from tableview doesn't have in the column a specific String, it becomes invisible. example: By clicking the checkbox, hide all rows that dont have the exact value "Barcelona" in table_adress. I tried to do this, but I keep complicating myself. Any ideas?
the function
#FXML
void CakeRequestsFromBarcelona(ActionEvent event) throws IOException {
for(Object cake:table.getItems()) {
for (TableColumn column : table.getColumns()) {
//not sure how to write it
}
}
}
initialize
#FXML
private TableColumn<CakeRequest, Integer> table_ID;
#FXML
private TableColumn<CakeRequest, String> table_adress;
#FXML
private TableColumn<CakeRequest, String> table_design;
#FXML
private TableColumn<CakeRequest, String> table_flavour;
#FXML
private TableColumn<CakeRequest, String> table_model;
#FXML
private TableColumn<CakeRequest, String> table_name;
#FXML
private TableColumn<CakeRequest, String> table_phonenumber;
#FXML
private TableView<CakeRequest> table;
public void initialize(){
//cakeRequest
table_ID.setCellValueFactory(
p-> new SimpleIntegerProperty(p.getValue().getID()).asObject()
);
table_name.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerName())
);
table_adress.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getOwnerAddress())
);
table_phonenumber.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getPhoneNumber())
);
table_flavour.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getFlavour())
);
table_design.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getDesign())
);
table_model.setCellValueFactory(
p -> new SimpleStringProperty(p.getValue().getModel())
);
}
CakeRequest
public class CakeRequest implements Identifiable<Integer>, Serializable{
private int ID;
private String OwnerName;
private String OwnerAddress;
private String PhoneNumber;
private String Model;
private String Flavour;
private String Design;
Use a FilteredList and change the predicate when the check box is checked/unchecked:
private ObservableList<CakeRequest> data = FXCollections.observableArrayList();
private FilteredList<CakeRequest> filteredData = new FilteredList<>(data, request -> true);
public void initialize(){
// existing code...
table.setItems(filteredData);
}
#FXML
void cakeRequestsFromBarcelona(ActionEvent event) {
if (checkBox.isChecked()) {
filteredData.setPredicate(request -> request.getOwnerAddress().contains("Barcelona"));
} else {
filteredData.setPredicate(request -> true);
}
}
iam new here so i dont know exactly what to do but i have this problem my tableview.setitems(oblist);
when i start my program the table view doesnt show me anything just strange lines inside the area
public class FXMLTableController implements Initializable {
private Connection con;
#FXML
private TableView<LOC> tableview;
#FXML
private TableColumn<LOC, Integer> ttidLoc;
#FXML
private TableColumn<LOC,Integer> ttidProd;
#FXML
private TableColumn<LOC, Float> ttPrixJour;
#FXML
private TableColumn<LOC,Integer> ttMaxJour;
#FXML
private TableColumn<LOC,Integer> ttidClient;
ObservableList<LOC> oblist=FXCollections.observableArrayList();
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
con = DataBase.getInstance().getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from locations");
while(rs.next()) {
oblist.add(new LOC(rs.getInt("idLoc"),rs.getInt("idProd"),rs.getFloat("PrixJour"),rs.getInt("MaxJour"),rs.getInt("idClient")));
}
ttidLoc.setCellValueFactory(new PropertyValueFactory<>("idLoc"));
ttidProd.setCellValueFactory(new PropertyValueFactory<>("idProd"));
ttPrixJour.setCellValueFactory(new PropertyValueFactory<>("PrixJour"));
ttMaxJour.setCellValueFactory(new PropertyValueFactory<>("MaxJour"));
ttidClient.setCellValueFactory(new PropertyValueFactory<>("idClient"));
tableview.setItems(oblist);
} catch (SQLException ex) {
Logger.getLogger(FXMLTableController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
here is my entities code that i work with
public class LOC {
private int idLoc;
private int idProd;
private float PrixJour;
private int MaxJour;
private int idClient;
public LOC(int idLoc, int idProd, float PrixJour, int MaxJour, int idClient) {
this.idLoc = idLoc;
this.idProd = idProd;
this.PrixJour = PrixJour;
this.MaxJour = MaxJour;
this.idClient=idClient;
}
public LOC( int idProd, float PrixJour, int MaxJour, int idClient) {
this.idProd = idProd;
this.PrixJour = PrixJour;
this.MaxJour = MaxJour;
this.idClient=idClient;
btw im using scenebuilder and i trying to open the tableview in another scene when clicking a button on the first one
U can also use your own implementation of a CellValueFactory this way u do not rely on the reflection logic of the PropertyValueFactory. As an example:
ttidLoc.setCellValueFactory(param -> {
LOC value = param.getValue();
return new SimpleObjectProperty<>(value.idLoc);
});
I have a question with javafx. I have a project that have operation with a bib file. I face a problem when I want to insert a class data(Entry) to table view. But it still empty in tableview. The operation is type a name and search from bib file, then show the result on Table view.
Thanks
The gui image
I have a class
public class Entry implements Comparable<Entry>{
public static final int ignoreOrder=-1;
public static final int OrderByName=0;
public static final int OrderByTitle=1;
public static final int OrderByYear=2;
private String name;
private String title="";
private String year="0";
private int order=OrderByName;
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public Entry(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
Fill the tableview, but it still empty.
#FXML
private TableView<Entry> table_id;
#FXML
private TableColumn<Entry, String> Col_BKey;
#FXML
private TableColumn<Entry, String> Col_counts;
#FXML
private TableColumn<Entry, String> Col_EntryType;
#FXML
private TableColumn<Entry, String> Col_Title;
#FXML
private TableColumn<Entry, String> Col_Year;
#FXML
private TableColumn<Entry, String> Col_Booktitle;
#FXML
private TableColumn<Entry, String> Col_Author;
public void loadFile(ActionEvent event){
int orderModel=0;
if(title_radio.isSelected()){
orderModel=1;
}else if(year_radio.isSelected()){
orderModel=2;
}
ReadingProcessor readingProcessor=new ReadingProcessor(new OrderedLinkedList(orderModel));
entryList=readingProcessor.read(filePath);
}
public void SelectOKAction(ActionEvent event) {
String searchStr=select_text.getText();
if(searchStr==null||searchStr.trim().equals("")){
AlertBox.display("Wrong", "Please enter entry's name");
return;
}
Entry entry=entryList.search(searchStr);
if(entry==null){//NOT FOOUND
AlertBox.display("Wrong", "Entry not exists!");
}
ObservableList<Entry> list = FXCollections.observableArrayList(entry);
table_id.setItems(list);
}
The only thing missing is setCellValueFactory
If you can change all your fields from String into SimpleStringProperty for example private String title; into private SimpleStringProperty title then you can simply add
Col_Title.setCellValueFactory(new PropertyValueFactory<Row, String>("title"));
otherwise you have to lookup for a more complex setCellValueFactory
I tried all to populate a TableView with data. The next code inserts a new row in table but the data not appear the table. I tried to find an explication for this without success.
Please help. I can't what is wrong.
In controller.java
#FXML private TableView<TempTableData> tempTable;
#FXML private TableColumn<TempTableData,String> columnTime;
#FXML private TableColumn<TempTableData,Float> columnTempOne;
#FXML private TableColumn<TempTableData,Float> columnTempTwo;
#FXML private TableColumn<TempTableData,Float> columnTempThree;
#FXML protected void initialize() {
columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
new PropertyValueFactory<TempTableData,String>("Time"));
columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 1"));
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 2"));
columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 3"));
tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}
TempTableData.java
public class TempTableData {
private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;
public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
this.time = new SimpleStringProperty(time);
this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
return time.get();
}
public void setTime(String time) {
this.time.set(time);
}
public float getDataSensorOne() {
return dataSensorOne.get();
}
public void setDataSensorOne(float dataSensorOne) {
this.dataSensorOne.set(dataSensorOne);
}
public float getDataSensorTwo() {
return dataSensorTwo.get();
}
public void setDataSensorTwo(float dataSensorTwo) {
this.dataSensorTwo.set(dataSensorTwo);
}
public float getDataSensorThree() {
return dataSensorThree.get();
}
public void setDataSensorThree(float dataSensorThree) {
this.dataSensorThree.set(dataSensorThree);
}
public String toString(){
String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]",
time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
return string;
}
}
why you creating table columns again ? ,as they already created with #FXML annotation (injection!).
remove column instance creation lines from your code and everything will be fine
// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();
make sure your cell factory values are spelt exactly as the corresponding model class values.
e.g.
private final .... SimpleStringProperty time;
and
new ...... PropertyValueFactory("time"));
I'm having having trouble getting a correct output from tableview. I'm using a button to set one item from tableview to a label. However, it prints "StringProperty [Value pineapples]" where I would like it to be just "pineapples".
The tableview gives them correctly.
public class ProductListController implements Initializable {
#FXML public TableView<Model> tableview ;
#FXML private TableColumn<Model, Number> ProductID;
#FXML private TableColumn<Model, String> ProductName;
#FXML private TableColumn<Model, Number> ProductPrice;
#FXML private Label lblProduct;
#FXML private Label lblPrice;
#FXML
private void btnActionShow(ActionEvent event) {
assert tableview !=null : " ";
ProductID.setCellValueFactory(cellData -> cellData.getValue().ProductIDProperty());
ProductName.setCellValueFactory(cellData -> cellData.getValue().ProductNameProperty());
ProductPrice.setCellValueFactory(cellData -> cellData.getValue().ProductPriceProperty());
buildData();
}
private ObservableList<Model> data;
public void buildData(){
data = FXCollections.observableArrayList();
try{
Connection conn = DriverManager.getConnection
("jdbc:derby://localhost:1527/Stock", "*****", "*****");
Statement stmt = conn.createStatement();
String SQL = "SELECT * FROM PRODUCTS";
ResultSet rs = stmt.executeQuery(SQL);
while (rs.next()) {
Model mod = new Model();
mod.ProductID.set(rs.getInt("ID"));
mod.ProductName.set(rs.getString("NAME"));
mod.ProductPrice.set(rs.getInt("SELL_PRICE"));
data.add(mod);
}
tableview.setItems(data);
}
catch ( SQLException err) {
System.out.println(err.getMessage() );
}
}
//Button to fetch data from Tableview. Sets the data not the way I want.
#FXML
private void btnConfirmAction(ActionEvent event) {
Model model = tableview.getSelectionModel().getSelectedItem();
String prd;
prd = model.getProductName().toString();
lblProduct.setText(prd);
}
#FXML
private void btnNextAction(ActionEvent event) {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/appl/Discount.fxml"));
Parent parent = loader.load();
DiscountController discountcontr = loader.getController();
discountcontr.setProduct(tableview.getSelectionModel().getSelectedItem().getProductName().toString());
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Model
public class Model {
public SimpleIntegerProperty ProductID = new SimpleIntegerProperty();
public SimpleStringProperty ProductName = new SimpleStringProperty ();
public SimpleIntegerProperty ProductPrice = new SimpleIntegerProperty();
private final SimpleBooleanProperty Checked = new SimpleBooleanProperty(false);
public SimpleBooleanProperty checkedProperty() {
return this.Checked;
}
public java.lang.Boolean getChecked() {
return this.checkedProperty().get();
}
public void setChecked(final java.lang.Boolean checked) {
this.checkedProperty().set(checked);
}
public SimpleIntegerProperty getProductID() {
return ProductID;
}
public SimpleStringProperty getProductName() {
return ProductName;
}
public SimpleIntegerProperty getProductPrice() {
return ProductPrice;
}
Since getProductName() returns a SimpleStringProperty, you need to retrieve the String from it using the get(). Just use :
String prd = model.getProductName().get();
Your model is implemented incorrectly. You should use the following pattern:
public class Model {
private SimpleStringProperty productName = new SimpleStringProperty();
public SimpleStringProperty productNameProperty() {
return productName ;
}
public final String getProductName() {
return productNameProperty().get();
}
public final void setProductName(String productName) {
productNameProperty().set(productName);
}
}
and similarly for the other properties.
If you use the e(fx)clipse plugin, you can generate the methods automatically from the property definition by right-clicking, choosing "Source" and then "Generate JavaFX Getters and Setters". I think NetBeans has similar functionality.