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
Related
I am having trouble populating a TableView with data. I am not using proper properties I am just trying to retrieve the property from my get...() method for every field in my Client class.
Here is my client class:
public class Client {
private Integer ID;
public String firstName;
public String lastName;
public String email;
public String phoneNumber;
public Integer age;
public Client() {}
public Client(String firstName, String lastName, String email, String phoneNumber, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
this.age = age;
}
public void setID(Integer ID) {
this.ID = ID;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getID() {
return ID;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public Integer getAge() {
return age;
}
#Override
public String toString(){
return "ID: " + this.ID + " First Name: " +
this.firstName + " Last Name: " + this.lastName + " Email: " +
this.email + " Phone: " + this.phoneNumber + " Age: " + this.age;
}
}
And here is my controller where I am trying to populate the TableView:
public class ClientWindowController implements Initializable {
private final String fxml = "add_client_window.fxml";
private final String fxml1 = "update_client_window.fxml";
private final String fxml2 = "remove_client_window.fxml";
#FXML
Button addClient;
#FXML
Button updateClient;
#FXML
Button removeClient;
#FXML
TableView<Client> clientTableView;
#FXML
private TableColumn<Client, String> firstNameColumn;
#FXML
private TableColumn<Client, String> lastNameColumn;
#FXML
private TableColumn<Client, String> phoneNumberColumn;
#FXML
private TableColumn<Client, String> emailColumn;
#FXML
private TableColumn<Client, Integer> ageColumn;
#FXML
private TableColumn<Client, Integer> IDColumn;
ObservableList<Client> observableList = FXCollections.observableArrayList();
#FXML
public void openAddClientWindow() throws IOException {
App.openWindow(fxml, 500, 500);
}
#FXML
public void openUpdateClientWindow() throws IOException {
App.openWindow(fxml1, 500, 500);
}
#FXML
public void openRemoveClientWindow() {
App.openWindow(fxml2, 250, 200);
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ClientDAO clientDAO = new ClientDAO();
List<Client> clients = clientDAO.findAll();
observableList = FXCollections.observableArrayList(clients);
IDColumn.setCellValueFactory(new PropertyValueFactory<>("ID"));
firstNameColumn.setCellValueFactory(new PropertyValueFactory<Client, String>("firstName"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
emailColumn.setCellValueFactory(new PropertyValueFactory<>("email"));
phoneNumberColumn.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
clientTableView.setItems(observableList);
}
}
The error im getting:
Apr 22, 2021 12:58:46 AM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'ID' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory#241b95d with provided class type: class org.example.model.Client
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class org.example.model.Client (in module org.example) because module org.example does not open org.example.model to javafx.base
I want to set the background color of a cell in a TableView. I have a for loop and I add the CSS rule, but I'm changing the background color in the entire column.
I want the cell to have a red background when the cell value is "ALTA". I want a yellow background when the value is "MEDIA". And I want a green background when the cell value is "BAJA".
This loop changes the entire column, I need only need to change the color of the cell:
for (RespuestaIn o : tablarincidentes.getItems()) {
String a= tprioridad.getCellData(o);
if (a.equals("ALTA")) {
tprioridad.setStyle("-fx-background-color:red");
}
}
This is my class:
public class RespuestaIn {
private IntegerProperty IdRincidente;
private StringProperty ConcecutivoRin;
private ObjectProperty <LocalDate> fechaprogramada;
private StringProperty tiempoe;
private ObjectProperty <LocalDate> fechaejecutada;
private StringProperty horaSol;
private StringProperty tiempor;
private StringProperty tareaejecutada;
private StringProperty novedades;
private StringProperty prioridad;
private StringProperty verificacion;
private StringProperty concecutivoincidente;
private StringProperty concecutivoincidenteid;
public RespuestaIn (Integer IdRincidente, String ConcecutivoRin, LocalDate fechaprogramada, String tiempoe , LocalDate fechaejecutada , String horaSol ,String tiempor ,String tareaejecutada ,String novedades ,String prioridad ,String verificacion,String concecutivoincidente,String concecutivoincidenteid ) {
this.IdRincidente=new SimpleIntegerProperty (IdRincidente);
this.ConcecutivoRin= new SimpleStringProperty(ConcecutivoRin);
this.fechaprogramada= new SimpleObjectProperty<LocalDate> (fechaprogramada);
this.tiempoe= new SimpleStringProperty (tiempoe);
this.fechaejecutada= new SimpleObjectProperty<LocalDate> (fechaejecutada);;
this.horaSol= new SimpleStringProperty (horaSol);
this.tiempor= new SimpleStringProperty (tiempor);
this.tareaejecutada= new SimpleStringProperty (tareaejecutada);
this.novedades= new SimpleStringProperty (novedades);
this.prioridad= new SimpleStringProperty (prioridad);
this.verificacion= new SimpleStringProperty (verificacion);
this.concecutivoincidente= new SimpleStringProperty (concecutivoincidente);
this.concecutivoincidenteid= new SimpleStringProperty (concecutivoincidenteid);
}
public Integer getIdRincidente() {
return IdRincidente.get();
}
public void setIdRincidente(Integer idRincidente) {
this.IdRincidente = new SimpleIntegerProperty ();
}
public String getConcecutivoRin() {
return ConcecutivoRin.get();
}
public void setConcecutivoRin(StringProperty concecutivoRin) {
this.ConcecutivoRin = new SimpleStringProperty();
}
public LocalDate getFechaprogramada() {
return fechaprogramada.get();
}
public void setFechaprogramada(LocalDate fechaprogramada) {
this.fechaprogramada = new SimpleObjectProperty<> (fechaprogramada);
}
public ObjectProperty<LocalDate> fechaprogramadaProperty() {
return fechaprogramada;
}
public String getTiempoe() {
return tiempoe.get();
}
public void setTiempoe(StringProperty tiempoe) {
this.tiempoe = new SimpleStringProperty();
}
public LocalDate getFechaejecutada() {
return fechaejecutada.get();
}
public void setFechaejecutada(LocalDate fechaejecutada) {
this.fechaejecutada = new SimpleObjectProperty<> (fechaejecutada);
}
public ObjectProperty<LocalDate> fechaejecutadaProperty() {
return fechaejecutada;
}
public String getHoraSol() {
return horaSol.get();
}
public void setHoraSol(StringProperty horaSol) {
this.horaSol = new SimpleStringProperty();
}
public String getTiempor() {
return tiempor.get();
}
public void setTiempor(StringProperty tiempor) {
this.tiempor = new SimpleStringProperty();
}
public String getTareaejecutada() {
return tareaejecutada.get();
}
public void setTareaejecutada(StringProperty tareaejecutada) {
this.tareaejecutada = new SimpleStringProperty();
}
public String getNovedades() {
return novedades.get();
}
public String getConcecutivoincidente() {
return concecutivoincidente.get();
}
public void setConcecutivoincidente(StringProperty concecutivoincidente) {
this.concecutivoincidente = new SimpleStringProperty();
}
public String getConcecutivoincidenteid() {
return concecutivoincidenteid.get();
}
public void setConcecutivoincidenteid(StringProperty concecutivoincidenteid) {
this.concecutivoincidenteid = new SimpleStringProperty();
}
public String getPrioridad() {
return prioridad.get();
}
public void setPrioridad(StringProperty prioridad) {
this.prioridad = new SimpleStringProperty();
}
public String getVerificacion() {
return verificacion.get();
}
public void setVerificacion(StringProperty verificacion) {
this.verificacion = new SimpleStringProperty();
}
}
This is the RincidentesController:
#FXML private TableView<RespuestaIn> tablarincidentes;
#FXML private TableColumn<RespuestaIn, Integer> idrin;
#FXML private TableColumn<RespuestaIn, String> concecutivorin;
#FXML private TableColumn<RespuestaIn, LocalDate> fechapro;
#FXML private TableColumn<RespuestaIn, String> testimado;
#FXML private TableColumn<RespuestaIn, LocalDate> fejecutada;
#FXML private TableColumn<RespuestaIn, String > hsolucion;
#FXML private TableColumn<RespuestaIn, String > trespuesta;
#FXML private TableColumn<RespuestaIn, String > tejecutada;
#FXML private TableColumn<RespuestaIn, String > novedades;
#FXML private TableColumn<RespuestaIn, String > tprioridad;
#FXML private TableColumn<RespuestaIn, String > tverificacion;
#FXML private TableColumn<RespuestaIn, String > cnsinc;
#FXML private TableColumn<RespuestaIn, String > cnsincid;
idrin.setCellValueFactory(new PropertyValueFactory <RespuestaIn,Integer>("IdRincidente"));
concecutivorin.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("ConcecutivoRin"));
fechapro.setCellValueFactory(CellData -> CellData.getValue().fechaprogramadaProperty());
testimado.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tiempoe"));
fejecutada.setCellValueFactory(CellData -> CellData.getValue().fechaejecutadaProperty());
hsolucion.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("horaSol"));
trespuesta.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tiempor"));
tejecutada.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tareaejecutada"));
novedades.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("novedades"));
tprioridad.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("prioridad"));
tverificacion.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("verificacion"));
cnsinc.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("concecutivoincidente"));
cnsincid.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("concecutivoincidenteid"));
I have a problem with javafx. It doesn't show any data from my DB I tried to enter some data manually but it doesn't work neither. I saw all the post around this issue but none of them bring me a fix.
Thanks a lot for your help and sorry for my English.
Main class :
private ObservableList<Index> LstClient = FXCollections.observableArrayList();
public MainClass {
LstClient.add(new Index("Lesinge"));
}
public ObservableList<Index> getLstClient(){return LstClient;}
Here my controller :
#FXML
private TableView<Index> personneTable;
#FXML
private TableColumn<Index, String> NomColonne;
private Mainclass main;
public PersonneMapping() { }
public void setMainApp(Mainclass mainApp) {
this.main = mainApp;
personneTable.setItems(main.getLstClient());
#FXML
public void Initialize () {
NomColonne.setCellValueFactory(cellData -> cellData.getValue().getNom())
}
Here my Index
private StringProperty Nom = new SimpleStringProperty();
public Index( String Nom ) {
this.Nom.set(Nom);
}
public StringProperty getNom() {
return Nom;
}
public void setNom(StringProperty Nom) {
this.Nom = Nom;
}
}
My fxml files is good it has the good controller and fx id on each columns
This are the colid specify in fxml file, I don't know the way. If I press the add button inputted data are not showing in the table view. Some blank row has been added. I want to add data in tableview from user.
#FXML
private TableColumn<AddItemDetails, String> colofiice_name;
#FXML
private TableColumn<AddItemDetails, String> colref_name;
#FXML
private TableColumn<AddItemDetails, String> colch_item_name;
#FXML
private TableColumn<AddItemDetails, String> colch_item_code;
#FXML
private TableColumn<AddItemDetails, String> colch_unit;
#FXML
private TableColumn<AddItemDetails, String> colch_qty;
#FXML
private TableColumn<AddItemDetails, String> colch_rec_by;
#FXML
private TableColumn<AddItemDetails, String> colch_desig;
#FXML
private TableColumn<AddItemDetails, String> colch_addr;
#FXML
private TableColumn<AddItemDetails, String> colch_remark;
#FXML
private TableColumn<AddItemDetails, String> colch_no;
#FXML
private TableColumn<AddItemDetails, String> colch_action;
private JFXCheckBox CB;
I write this code in controller
public void AddTableView(ActionEvent event)throws SQLException{
AddItemDetails additem = new AddItemDetails();
additem.setOfficeName(select_office.getValue());
additem.setRefNo(challan_select_item.getValue());
additem.setItemName(txt_ref_no.getText());
additem.setItemCode(txt_item_code.getText());
additem.setItemUnit(txt_unit.getText());
additem.setItemQty(txt_qty.getText());
additem.setReceiveBY(txt_rec_by.getText());
additem.setDesignation(txt_desig.getText());
additem.setChNo(txt_chllan.getText());
additem.setAddress(txt_addr.getText());
additem.setRemarks(txt_remark.getText());
additem.setAction(CB);
tableview2.getItems().addAll(additem);
}catch(Exception e){
e.printStackTrace();
}
this my AddItemDetails Class
package inventory_system_app;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class AddItemDetails {
private String office_name;
private String ref_no;
private String I_name;
private String I_code;
private String I_unit;
private String I_qty;
private String rec_by;
private String desig;
private String ch_no;
private String address;
private String ch_remarks;
private JFXCheckBox Action;
public AddItemDetails(String office_name,String ref_no,String I_name,String
I_code,String I_unit, String I_qty,String rec_by,String desig,String
ch_no,String address, String ch_remarks, String value){
this.office_name = office_name;
this.ref_no = ref_no;
this.I_name = I_name;
this.I_code = I_code;
this.I_unit = I_unit;
this.I_qty = I_qty;
this.rec_by = rec_by;
this.desig = desig;
this.ch_no = ch_no;
this.address = address;
this.ch_remarks = ch_remarks;
this.Action = new JFXCheckBox();
}
AddItemDetails() {
}
public String getOfficeName(){
return office_name;
}
public String getRefNo(){
return ref_no;
}
public String getItemName(){
return I_name;
}
public String getItemCode(){
return I_code;
}
public String getItemUnit(){
return I_unit;
}
public String getItemQty(){
return I_qty;
}
public String getReceiveBY(){
return rec_by;
}
public String getDesignation(){
return desig;
}
public String getChNo(){
return ch_no;
}
public String getAddress(){
return address;
}
public String getRemarks(){
return ch_remarks;
}
public JFXCheckBox getAction() {
return Action;
}
public void setOfficeName(String value){
this.office_name= value;
}
public void setRefNo(String value){
this.ref_no=value;
}
public void setItemName(String value){
this.I_name=value;
}
public void setItemCode(String value){
this.I_code=value;
}
public void setItemUnit(String value){
this.I_unit=value;
}
public void setItemQty(String value){
this.I_qty=value;
}
public void setReceiveBY(String value){
this.rec_by=value;
}
public void setDesignation(String value){
this.desig=value;
}
public void setChNo(String value){
this.ch_no=value;
}
public void setAddress(String value){
this.address=value;
}
public void setRemarks(String value){
this.ch_remarks=value;
}
public void setAction(JFXCheckBox Action) {
this.Action = Action;
}
}
Steps to make your code working:
1.
In the AddItemDetails replace every String field which you want to show in the table with StringPropertyes like:
public class AddItemDetails {
private StringProperty office_name;
private StringProperty ref_no;
private StringProperty I_name;
.
.
.
public AddItemDetails(String office_name,String ref_no,String I_name,...){
this.office_name = new SimpleStringProperty(office_name);
this.ref_no = new SimpleStringProperty(ref_no);
this.I_name = new SimpleStringProperty(I_name);
.
.
.
// + getters
Setting the cellValueFactory-s for each column:
colofiice_name.setCellValueFactory(data -> data.getValue().office_nameProperty());
colref_name.setCellValueFactory(data -> data.getValue().colref_nameProperty());
item_name.setCellValueFactory(data -> data.getValue().item_nameProperty());
Adding a new item to the tableView:
tableView.getItems().add(new AddItemDetails("Office","Ref","I_name",...));
After these steps it should work.
EDIT
You can use an
ObservableList<AddItemDetails> myData = FXCollecctions.observableArraylist();
for storing the data, then:
tableView.setItems(myData);
then you can add and remove elements to and from this list, and it will update the data in tableview too.Like:
mydata.add(new AddItemDetails(...));
myData.remove(...);
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.