JavaFX基于Derby数据库的饼图无法显示,寻求技术帮助
Hey there! Let's troubleshoot why your PieChart isn't displaying—since your bar chart works, we're already close to fixing this. Here are the key checks and adjustments you need to make:
1. Verify FXML Binding & SceneBuilder Config
First, double-check your FXML setup:
- Make sure the
fx:idof your PieChart in SceneBuilder exactly matches the field name in your controller (piechart—Java is case-sensitive, so no typos here!). - Ensure the PieChart has non-zero
prefWidthandprefHeightvalues in SceneBuilder. If either is set to 0, the chart will be invisible. Set something like 400x300 for testing. - Confirm your FXML root includes the correct namespace:
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.chart.PieChart?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="your.package.DocumentController"> <PieChart fx:id="piechart" prefHeight="300.0" prefWidth="400.0" /> </AnchorPane>
2. Use PieChart-Specific Data Structure
This is the most common pitfall: PieChart uses a different data format than bar charts. Bar charts rely on XYChart.Series, but PieChart needs an ObservableList<PieChart.Data> (key-value pairs for labels and values).
Update your controller's initialize method to convert your Derby data to this format:
import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.chart.PieChart; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ResourceBundle; public class DocumentController implements Initializable { @FXML private PieChart piechart; // Make sure this matches fx:id in FXML @Override public void initialize(URL url, ResourceBundle rb) { ObservableList<PieChart.Data> pieData = FXCollections.observableArrayList(); // Replace with your Derby database connection logic try (Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/yourDBName", "username", "password")) { String query = "SELECT category_column, value_column FROM your_table"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String category = rs.getString("category_column"); double value = rs.getDouble("value_column"); pieData.add(new PieChart.Data(category, value)); } // Assign data to the PieChart piechart.setData(pieData); piechart.setTitle("Your Chart Title"); // Optional: Add a title } catch (Exception e) { e.printStackTrace(); } } }
3. Don't Rely on SceneBuilder's Static Preview
Important note: SceneBuilder's preview mode doesn't execute your controller's initialize method (it's just a static view). If you're only checking the SceneBuilder preview, you won't see dynamically loaded data. Run your full JavaFX application to test if the PieChart displays correctly.
4. Confirm Controller Initialization
Ensure your initialize method is properly implemented:
- It must be
public voidwith parametersURLandResourceBundle(no missing or mismatched parameters). - Your controller class must be correctly referenced in the FXML's
fx:controllerattribute.
内容的提问来源于stack exchange,提问作者Inga

