JavaFX表格仅显示部分MySQL数据:ID/Month/Time正常,DayOfWeek等列无数据
Hey there! Let's break down your two technical problems and walk through possible fixes for each:
1. MySQL Data Only Displays Every Other Column
First up, the issue where your MySQL data is showing only every other column. Here are the most common culprits and how to check them:
- Check your SELECT query: If you're explicitly listing columns (like
SELECT id, month, time FROM schedule), double-check that you haven't accidentally omitted every other column. Try runningSELECT * FROM your_table_nameinstead to see if all columns appear—this will rule out a query-related mistake. - Client display settings: If you're using a tool like MySQL Workbench or phpMyAdmin, make sure you haven't hidden any columns. Look for a "Columns" menu or right-click the table header to verify all columns are set to show.
- Table structure and data integrity: Run
DESCRIBE your_table_name;to confirm all columns exist in the table. Then check if the missing columns actually have data—if they're allNULL, some clients might hide them by default, or your data insertion logic might be skipping those columns.
2. JavaFX Table Missing DayOfWeek & DayOfMonth Data
For your JavaFX table issue where ID, Month, and Time show up but DayOfWeek and DayOfMonth don't, let's focus on the most likely fixes based on the code snippet you shared:
Key Checks to Perform:
Verify PropertyValueFactory setup: Your ID column uses
new PropertyValueFactory<>("ID")—make sure you've completed the setup for the missing columns exactly like this. For example:// Complete setup for DayOfWeek column TableColumn<Schedule, String> dayOfWeekCol = new TableColumn<>("DayOfWeek"); dayOfWeekCol.setMinWidth(120); dayOfWeekCol.setCellValueFactory(new PropertyValueFactory<>("dayOfWeek")); // Complete setup for DayOfMonth column TableColumn<Schedule, Integer> dayOfMonthCol = new TableColumn<>("DayOfMonth"); dayOfMonthCol.setMinWidth(100); dayOfMonthCol.setCellValueFactory(new PropertyValueFactory<>("dayOfMonth"));Important: The string inside
PropertyValueFactory(like"dayOfWeek") must exactly match the property name in yourScheduleclass (case-sensitive!).Ensure Schedule class has proper getters:
PropertyValueFactoryrelies on standard JavaBean getter methods. YourScheduleclass must have these (adjust types based on your actual data):public class Schedule { // Your existing fields private Integer ID; private String month; private String time; private String dayOfWeek; private Integer dayOfMonth; // Existing getters public Integer getID() { return ID; } public String getMonth() { return month; } public String getTime() { return time; } // Required getters for missing columns public String getDayOfWeek() { return dayOfWeek; } public Integer getDayOfMonth() { return dayOfMonth; } // Optional: If using JavaFX Properties, add these too public StringProperty dayOfWeekProperty() { /* Implement with SimpleStringProperty */ } public IntegerProperty dayOfMonthProperty() { /* Implement with SimpleIntegerProperty */ } }Confirm data is present in Schedule objects: Before passing your
ObservableList<Schedule>to the table, log or debug thedayOfWeekanddayOfMonthvalues of each object. If they'reNULL, the issue is with how you're fetching data from MySQL, not the JavaFX table itself. Double-check your database query and data mapping code to ensure those values are being pulled correctly.
内容的提问来源于stack exchange,提问作者Jay

