Data doesn't appears on javafx - javafx

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

Related

JavaFX static ObservableList not refreshing ComboBox

What I'm trying to do is have a single class that maintains a static ObservableList of countries. I want to display these countries in a ComboBox. I've got this part working fine. Now, I also want to enable the user to add new countries to the list. So, there is a button beside the combo box that will show another dialog allowing entry of another country name. After the user enters the country name and clicks save, I would like the single static ObservableList to be updated with the new country and then it show up in the ComboBox. This part is not happening.
I'll show what DOES work, and what does not.
Saving a reference to the static list and updating that works. Like so:
public class CustomerController implements Initializable {
private ObservableList<Country> countryList;
#Override
public void initialize(URL url, ResourceBundle rb) {
countryList = Country.getCountryList();
comboCountry.setItems(countryList);
}
...
// Fired when clicking the "new country" button
#FXML
void handleNewCountry(ActionEvent event) {
Country country = new Country();
country.setCountry("Austria");
countryList.add(country);
}
}
This is what I would like to do, however it does not work:
public class CustomerController implements Initializable {
#FXML
private ComboBox<Country> comboCountry;
#Override
public void initialize(URL url, ResourceBundle rb) {
comboCountry.setItems(Country.getCountryList());
}
#FXML
void handleNewCountry(ActionEvent event) {
showScene("Country.fxml", "dialog.newCountry");
}
private void showScene(String sceneResource, String titleResource) {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(sceneResource),
resourceBundle
);
Scene scene = new Scene(loader.load());
getNewStage(resourceBundle.getString(titleResource), scene).showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
private Stage getNewStage(String title, Scene scene) {
Stage stage = new Stage();
stage.setTitle(title);
stage.setResizable(false);
stage.setScene(scene);
stage.initOwner(rootPane.getScene().getWindow());
stage.initModality(Modality.APPLICATION_MODAL);
return stage;
}
}
The Country class:
public class Country extends BaseModel {
private int countryID;
private StringProperty country;
private static ObservableList<Country> countryList; // The static observable list
public Country() {
countryList = FXCollections.observableArrayList();
country = new SimpleStringProperty();
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public StringProperty countryProperty() {
return this.country;
}
public String getCountry() {
return this.country.get();
}
public void setCountry(String country) {
this.country.set(country);
}
public boolean equals(Country country) {
if (this.getCountry().compareToIgnoreCase(country.getCountry()) != 0) {
return false;
}
return true;
}
public static ObservableList<Country> getCountryList() {
if (countryList.size() < 1) {
updateCountryList();
}
return countryList;
}
public static void updateCountryList() {
countryList.clear();
ArrayList<Country> daoList = CountryDao.listCountries();
for (Country country : daoList) {
countryList.add(country);
}
}
#Override
public String toString() {
return this.getCountry();
}
}
And the dialog for entering a new country:
public class CountryController implements Initializable {
#FXML
private TextField textCountry;
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void handleSave(ActionEvent event) {
Country country = new Country();
country.setCountry(textCountry.getText().trim());
CountryDao.insert(country); // Insert the country into the database
Country.updateCountryList(); // Update the static ObservableList
close();
}
#FXML
void handleCancel() {
close();
}
void close() {
final Stage stage = (Stage) textCountry.getScene().getWindow();
stage.close();
}
}
So, my theory is that somehow the ComboBox is creating a new instance of the ObservableList when setItems is called. I'm really not sure though. A static object should only have one instance, so updating it from anywhere should update that ComboBox. Anyone know what's up with this?
You're creating a new ObservableList instance every time the Country constructor is invoked. This way a list different to the one used with the ComboBox is modified.
If you really need to keep the list of countries in a static field (this is considered bad practice), you should make sure to only create a single ObservableList:
private static final ObservableList<Country> countryList = FXCollections.observableArrayList();
(Remove the assignment of this field from the constructor too.)

How to dynamically add data in tableview?

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(...);

Javafx how can I insert data to tableview

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

JavaFX Table Columns, SceneBuilder Not Populating

I've been looking at tutorials, and I can't seem to get a table to populate.
I'm using net beans and scenebuilder too.
Any help would be greatly appreciated! been struggling for 5 hours.
Here is my code for the Controller class:
public class FXMLDocumentController implements Initializable {
#FXML
private TableView<Table> table;
#FXML
private TableColumn<Table, String> countriesTab;
/**
* Initializes the controller class.
*/
ObservableList<Table> data = FXCollections.observableArrayList(
new Table("Canada"),
new Table("U.S.A"),
new Table("Mexico")
);
#Override
public void initialize(URL url, ResourceBundle rb) {
countriesTab.setCellValueFactory(new PropertyValueFactory<Table, String>("rCountry"));
table.setItems(data);
}
}
Here is my code for the Table
class Table {
public final SimpleStringProperty rCountry;
Table(String country){
this.rCountry = new SimpleStringProperty(country);
}
private SimpleStringProperty getRCountry(){
return this.rCountry;
}
}
Here is my main:
public class Assignment1 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
For PropertyValueFactory to find the property the item class (i.e. Table in this case) needs public as access modifier, not package private. The method returning the property needs to be public as well.
Furthermore the correct name for the method returning the property itself is <nameOfProperty>Property according to the conventions required for PropertyValueFactory to work.
Also since the actual type of the property is an implementation detail, it would be better design to use StringProperty as return type instead of SimpleStringProperty
public class Table {
private final SimpleStringProperty rCountry;
public Table(String country){
this.rCountry = new SimpleStringProperty(country);
}
public StringProperty rCountryProperty() {
return this.rCountry;
}
}
In case you used these modifiers to prevent write access to the property, you can still achieve this effect by using a ReadOnlyStringWrapper and return a ReadOnlyStringProperty:
public class Table {
private final ReadOnlyStringWrapper rCountry;
public Table(String country){
this.rCountry = new ReadOnlyStringWrapper (country);
}
public ReadOnlyStringProperty rCountryProperty() {
return this.rCountry.getReadOnlyProperty();
}
}
In case there is no write access to the property at all, simply using a getter for the property is enough. You do not need to use a StringProperty at all in this case:
public class Table {
private final String rCountry;
public Table(String country){
this.rCountry = country;
}
public String getRCountry() {
return this.rCountry;
}
}

Having trouble retrieving value from tableview

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.

Resources