Home>

Thank you for visiting. I am a Java beginner.

I've been exploring overseas sites for a full three days and searching for all kinds of methods.
I am in great trouble because I cannot implement it.
Can anyone help me? Any advice is welcome.

I am making a desktop application linked with MySQL with JavaFX.
I have experience implementing display in TableView with a fixed number of columns, but this time
I want to dynamically display data of any number of columns obtained from MySQL in TableViewI think.

(I want the user to select an arbitrary one from multiple search conditions, and automatically change the number of columns according to the number of columns in each data result and display it.)

Here is what I referred to this time.
Updated: Dynamic TableView Data From Database

When I perform a search, it does not appear in the TableView.

By the way, there is no particular error, but on the editor of Eclipse

--ObservableList is of raw type. References to the generic type ObservableList<E>must be parameterized
Type safety: The method setCellValueFactory (Callback) belongs to the raw type TableColumn. References to the generic type TableColumn<S, T>must be parameterized
Type safety: The method setItems (ObservableList) belongs to the raw type TableView. References to the generic type TableView<S>must be parameterized


And so on. However, I don't think it is a direct cause.

Corresponding source code

I thought I should post all the code, but because there are many classes and many methods that seem irrelevant,
hereApplicable class and method, fxmlI will post it.
If necessary, we will post more.

* Because priority is given to checking the operation of this operation display.
Reading the combo box choices and implementing the corresponding SQL statements is incomplete at this time.
Please note.

MySQLDao (database connection class)
package mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import controller.TableViewItem;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.util.Callback;
public class MySQLDao {
    private static final String url = "jdbc: mariadb: // localhost: 3306/pocketmoney?";
    private static final String user = "root";
    private static final String password = "root";
    // Get the data to be displayed in TableView of "Expenditure" history screen
    public ObservableList<ObservableList>searchTableViewPays (/ * String searchWord * /) throws SQLException {
        ObservableList<ObservableList>data;
        TableView tableview = new TableView ();
        final String SUM_QUERY = "SELECT paid_at, SUM (money) FROM pays GROUP BY paid_at;";data = FXCollections.observableArrayList ();
        try {Connection conn = DriverManager.getConnection (url, user, password);
        ResultSet rs = conn.createStatement (). executeQuery (SUM_QUERY);
        for (int i = 0;i<rs.getMetaData (). getColumnCount ();i ++) {
            final int j = i;
            TableColumn col = new TableColumn (rs.getMetaData (). getColumnName (i + 1));
            col.setCellValueFactory (new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String >>() {
                public ObservableValue<String>call (CellDataFeatures<ObservableList, String>param) {
                    System.out.println (param);
                    System.out.println (param.getValue ());
                    System.out.println (param.getValue (). get (j));
                    return new SimpleStringProperty (param.getValue (). get (j) .toString ());
                }
            });
            tableview.getColumns (). addAll (col);
            System.out.println ("Column [" + i + "]");
        }
        while (rs.next ()) {
            ObservableList<String>row = FXCollections.observableArrayList ();
            for (int i = 1;i<= rs.getMetaData (). getColumnCount ();i ++) {
                row.add (rs.getString (i));
            }
            System.out.println ("Row [1] added" + row);
            data.add (row);
        }
        } catch (SQLException e) {
            System.out.println ("SQLException:" + e.getMessage ());
            e.printStackTrace ();
        }
        return data;
    }
}
PaysReportController (controller class)
package controller;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import javafx.stage.Window;
import mysql.MySQLDao;
public class PaysReportController implements Initializable {
    @FXMLprivate TableView table;
    @FXML
    private ComboBox<String>cbBox;
    @FXML
    private Button SearchButton;
    @FXML
    private Button Homebutton;
    @Override
    public void initialize (URL location, ResourceBundle resources) {
    }
    @FXML
    void onSearchButton (ActionEvent event) {
        // Pass the condition selected in the combo box to Dao and display the result dynamically in TableView
        try {
            MySQLDao mysq = new MySQLDao ();
            table.setItems (mysq.searchTableViewPays ());
        } catch (SQLException e) {
            System.out.println ("SQLException:" + e.getMessage ());
            e.printStackTrace ();
        }
    }
    @FXML
    void onHomeButtonCliked (ActionEvent event) {
       /*
         * Close the currently displayed screen
         * /
        Scene s = ((Node) event.getSource ()). getScene ();
        Window window = s.getWindow ();
        window.hide ();
        //Screen transition
        try {
            FXMLLoader loader = new FXMLLoader (getClass (). GetResource ("/Main.fxml"));
            loader.setController (new MainController ());
            Parent root = loader.load ();
            Scene scene = new Scene (root);
            Stage stage = new Stage ();
            stage.setTitle ("pocket money management app");
            stage.setScene (scene);
            stage.show ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}
fxml
<? xml version = "1.0" encoding = "UTF-8"?><? import java.lang.String?><? import javafx.collections.FXCollections?><? import javafx.geometry.Rectangle2D?><? import javafx.scene.control.Button?><? import javafx.scene.control.ComboBox?><? import javafx.scene.control.Label?><? import javafx.scene.control.TableView?><? import javafx.scene.image.Image?><? import javafx.scene.image.ImageView?><? import javafx.scene.layout.AnchorPane?><? import javafx.scene.layout.Pane?><? import javafx.scene.text.Font?><children>    <Pane prefHeight = "80.0" prefWidth = "650.0"
            AnchorPane.leftAnchor = "0.0"
            AnchorPane.rightAnchor = "0.0">        <children>            <Label layoutX = "109.0" layoutY = "17.0" prefHeight = "47.0"
                    prefWidth = "271.0" text = "Expenditure so far" textFill = "# 997797">                                                                            </Label>            <ImageView fitHeight = "37.0" fitWidth = "41.0"
                    layoutX = "101.0" layoutY = "-31.0" pickOnBounds = "true"
                    preserveRatio = "true" x = "30.0" y = "50.0">                <image>                    <Image url = "@ ../images/search.png" />                </image>                <viewport>                    <Rectangle2D />                </viewport>            </ImageView>        </children>    </Pane>    <TableView fx: id = "table" layoutX = "80.0" layoutY = "191.0"
            prefHeight = "291.0" prefWidth = "329.0" AnchorPane.bottomAnchor = "110.0"
            AnchorPane.leftAnchor = "80.0" AnchorPane.rightAnchor = "79.0"
            AnchorPane.topAnchor = "191.0" />    <Button fx: id = "Homebutton" layoutX = "553.0" layoutY = "628.0"
            mnemonicParsing = "false" onAction = "# onHomeButtonCliked"
            prefHeight = "47.0" prefWidth = "51.0"
            textFill = "WHITE"
            AnchorPane.bottomAnchor = "34.0" AnchorPane.right Anchor = "38.0">                                                <graphic>            <ImageView fitHeight = "40.0" fitWidth = "33.0"
                    onDragDetected = "# onHomeButtonCliked" pickOnBounds = "true"
                    preserveRatio = "true" x = "30.0" y = "50.0">                <image>                    <Image url = "@ ../images/home.png" />                </image>                <viewport>                    <Rectangle2D /></viewport>            </ImageView>        </graphic>    </Button>    <ComboBox fx: id = "cbBox" layoutX = "107.0" layoutY = "115.0"
            prefHeight = "30.0" prefWidth = "214.0" AnchorPane.bottomAnchor = "440.0"
            AnchorPane.leftAnchor = "107.0">        <items>            <FXCollections fx: factory = "observableArrayList">                <String fx: value = "How much did you spend each day" />                <String fx: value = "total amount for each category" />                <String fx: value = "total amount" />                <String fx: value = "Expenditure of 3000 yen or more" />                <String fx: value = "Highest spending ever" />            </FXCollections>        </items>    </ComboBox>    <Button fx: id = "SearchButton" layoutX = "357.0" layoutY = "115.0"
            mnemonicParsing = "false" onAction = "# onSearchButton" prefHeight = "30.0"
            prefWidth = "52.0" text = "Search" AnchorPane.bottomAnchor = "440.0"
            AnchorPane.leftAnchor = "357.0" /></children></AnchorPane>
What I tried

I suspect the following two points as the cause.

(1) There is a method that is not executed in the PaysReportController class
                  public ObservableValue<String>call (CellDataFeatures<ObservableList, String>param) {
                        return new SimpleStringProperty (param.getValue (). get (j) .toString ());
                    }


This part will not be executed.

December 21, 2016 at 9:16 pm
Add this:
if (param.getValue (). get (j)! = null) {
return new SimpleStringProperty (param.getValue (). get (j) .toString ());
} else {
return new SimpleStringProperty ();
}
To the Table Colum Added Dynamically routine. Then the App does n’t crash if the cell is empty

  • How to dynamically generate the number of columns in JavaFX TableView
    I tried it with reference to this, but I could not implement it because there was no description of the association with the essential database.

  • docs.oracle TableView
    I also saw JavaDoc,
    Originally referred toUpdated: Dynamic TableView Data From Database
    I couldn't find a concrete implementation method above.

It is used as a reference baseHereUnlike the class structure of
my

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Due to an upload error, the content you originally wanted to upload is not reflected,
    Also, even if you try to edit, the number of characters is clearly limited even though it is clearly within the character limit, so you can not re-edit.
    Don't misunderstand the wrong question, so please let it be self-solved for the time being.
    The original question will be raised separately.
    I'm sorry.

    Report to the Terratail Secretariat.