I’m try to fill tableview2(fx:id tableview) row from data(ObservableList). But when it finish, tableview2 just show empty record. In console i see all row from data(ObservableList). I know what many question about that was in stackoverflow, sorry about that(and my english), but pls can you ask me where is error in my code? Thank you for help or attention).
UPDATE(add MainPayer.java and update PayerController.java): Here is my code.
PayerController.java:
public class PayerController {
@FXML
private TableColumn<MainPayer,Long> tablePayerid2;
@FXML
private TableColumn<MainPayer,Long> tableIsAccepted2;
@FXML
private TableColumn<MainPayer,Long> tableYear2;
@FXML
private TableColumn<MainPayer,Long> tableQuater2;
@FXML
private TableColumn<MainPayer,Long> tableEnspeopley2;
@FXML
private TableColumn<MainPayer,String> tableShortName2;
@FXML
private TableColumn<MainPayer,String> tableDistrict2;
@FXML
private TableColumn<MainPayer,String> tableSettlement2;
@FXML
private TableColumn<MainPayer,String> tableStreet2;
@FXML
private TableColumn<MainPayer,String> tableHouseNum2;
@FXML
private TableColumn<MainPayer,String> tableMailIndex2;
@FXML
private TableView<MainPayer> tableview2;
private ObservableList<MainPayer> data;
@FXML
private void handleRunMainQuery() throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String selectTableSQL = "SELECT F.PAYERID, F.ISACCEPTED, F.RYEAR, F.QUATER, F.ENSPEOPLEY, S.CSHORTNAME, T.DISTRICT, T.SETTLEMENT, T.STREETNAME, T.HOUSENUM, \n" +
" T.MAILINDEX\n" +
"FROM FFUNDSIDE F, FPAYER S, FLOCATIONHISTORY T,\n" +
" (SELECT PAYERID, MAX(LADATE) AS LADATE\n" +
" FROM FLOCATIONHISTORY\n" +
" GROUP BY PAYERID) X\n" +
"WHERE F.PAYERID = S.PAYERID AND S.PAYERID = T.PAYERID AND T.PAYERID = X.PAYERID AND T.LADATE = X.LADATE AND (F.PAYERID BETWEEN \n" +
" 400000000 AND 499999999) AND (F.RYEAR = 2018) AND (F.QUATER = 3) AND (F.ENSPEOPLEY > 10)";
data = FXCollections.observableArrayList();
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery(selectTableSQL);
tablePayerid2.setCellValueFactory(data -> data.getValue().payerid2Property().asObject());
tableIsAccepted2.setCellValueFactory(data -> data.getValue().isAccepted2Property().asObject());
tableYear2.setCellValueFactory(data -> data.getValue().year2Property().asObject());
tableQuater2.setCellValueFactory(data -> data.getValue().quater2Property().asObject());
tableEnspeopley2.setCellValueFactory(data -> data.getValue().enspeopley2Property().asObject());
tableShortName2.setCellValueFactory(data -> data.getValue().shortName2Property());
tableDistrict2.setCellValueFactory(data -> data.getValue().district2Property());
tableSettlement2.setCellValueFactory(data -> data.getValue().settlement2Property());
tableStreet2.setCellValueFactory(data -> data.getValue().street2Property());
tableHouseNum2.setCellValueFactory(data -> data.getValue().houseNum2Property());
tableMailIndex2.setCellValueFactory(data -> data.getValue().mailIndex2Property());
while (rs.next()) {
ObservableList<MainPayer> row = FXCollections.observableArrayList();
data.add(new MainPayer(rs.getLong(1),rs.getLong(2),rs.getLong(3),rs.getLong(4),rs.getLong(5),
rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),
rs.getString(11)));
}
tableview2.setItems(data);
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
MainPayer.java
public class MainPayer {
private final LongProperty payerid2;
private final LongProperty isAccepted2;
private final LongProperty year2;
private final LongProperty quater2;
private final LongProperty enspeopley2;
private final StringProperty shortName2;
private final StringProperty district2;
private final StringProperty settlement2;
private final StringProperty street2;
private final StringProperty houseNum2;
private final StringProperty mailIndex2;
public MainPayer(Long payerid2, Long isAccepted2, Long year2, Long quater2, Long enspeopley2, String shortName2, String district2,
String settlement2, String street2, String houseNum2, String mailIndex2) {
this.payerid2 = new SimpleLongProperty(payerid2);
this.isAccepted2 = new SimpleLongProperty(isAccepted2);
this.year2 = new SimpleLongProperty(year2);
this.quater2 = new SimpleLongProperty(quater2);
this.enspeopley2 = new SimpleLongProperty(enspeopley2);
this.shortName2 = new SimpleStringProperty(shortName2);
this.district2 = new SimpleStringProperty(district2);
this.settlement2 = new SimpleStringProperty(settlement2);
this.street2 = new SimpleStringProperty(street2);
this.houseNum2 = new SimpleStringProperty(houseNum2);
this.mailIndex2 = new SimpleStringProperty(mailIndex2);
}
public LongProperty payerid2Property() {
return payerid2;
}
public LongProperty isAccepted2Property() {
return isAccepted2;
}
public LongProperty year2Property() {
return year2;
}
public LongProperty quater2Property() {
return quater2;
}
public LongProperty enspeopley2Property() {
return enspeopley2;
}
public StringProperty shortName2Property() {
return shortName2;
}
public StringProperty district2Property() {
return district2;
}
public StringProperty settlement2Property() {
return settlement2;
}
public StringProperty street2Property() {
return street2;
}
public StringProperty houseNum2Property() {
return houseNum2;
}
public StringProperty mailIndex2Property() {
return mailIndex2;
}
}
MainScene.fxml
<TableView fx:id="tableview2" layoutX="210.0" layoutY="72.0" prefHeight="345.0" prefWidth="981.0">
<placeholder>
<Label text="Go ahead" />
</placeholder>
<columns>
<TableColumn fx:id="tablePayerid2" minWidth="20.0" prefWidth="104.0" text="A" />
<TableColumn fx:id="tableIsAccepted2" minWidth="40.0" prefWidth="74.0" text="B" />
<TableColumn fx:id="tableYear2" minWidth="1.0" prefWidth="75.0" text="C" />
<TableColumn fx:id="tableQuater2" minWidth="1.0" prefWidth="75.0" text="D" />
<TableColumn fx:id="tableEnspeopley2" minWidth="15.0" prefWidth="75.0" text="E" />
<TableColumn fx:id="tableShortName2" minWidth="15.0" prefWidth="75.0" text="F" />
<TableColumn fx:id="tableDistrict2" prefWidth="75.0" text="J" />
<TableColumn fx:id="tableSettlement2" prefWidth="75.0" text="K" />
<TableColumn fx:id="tableStreet2" prefWidth="111.0" text="L" />
<TableColumn fx:id="tableHouseNum2" minWidth="1.0" prefWidth="67.0" text="M" />
<TableColumn fx:id="tableMailIndex2" minWidth="2.0" prefWidth="58.0" text="N" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
There are a few problems you are facing.
First of all you should understand the base concepts how TableView
works in JavaFx.
Instead of linking tutorials, maybe I can point out what did you miss, and how to fix those problems.
Simply explained the TableView
has two "parts" one is the UI part which you can see, which contains the columns, headers, rows, etc. The other side is the model which populates the table. Once you did create the UI side, you have to assign a model to the table to get expected data added to the table.
In a TableView
a row is not a list of items, but an instance of a Model which have fields for each column. (Of course it can be a list but for you it makes things just more complicated)
There are a few steps if you respect your can get the expected results:
User
class with fields you want to load from db like palayerId, isAccepted, year and so on.
Every field should be a Property
corresponding its type, if it is String, it should be StringProperty, if its long then LongProperty, and so on.Pass this model as generic to the TableView
like you have: private TableView tableView2
-> private TableView<User> tableView2
So every row in the table is an instance of the User, you can define the whole table data in an ObservableList like you have tried, ObservableList<User> data
then as you did table.setItems(data)
.
@FXML
field for every column like you did for the tableView, and tell every column which data it has to display like: Let's say you have a column with Strings:
@FXML
private TableColumn<User,String> colWithString;
Then you can tell it like:
colWithString.setCellValueFactory(data -> data.getValue().myStringProperty());
instead of myStringProperty
of course you put the appropriate property there is needed.
User
to the data
you have earlier created like: data.add(new User(rs.getString(1),rs.getString(2),rs.getLong(3),...))
And that's it, if you respect these steps and modify accordingly the data from DB should be displayed.