Reputation: 133
I'm fairly new to programming and for school I have to create a program with a certain amount of classes. It's all going quite well for now, but I'm stuck on the graphic output of my data. The program is an expenses calculator. It is an frame devided in 6 JPanels. one of them should be a Piechart I created. The piechart works but won't show in my Frame. So I'm stuck on how to call in the right content to the panel. The code of the panel where I want to add the piechart is :
class CenterPaneel extends JPanel{
private static final long serialVersionUID = -835526987955952967L;
public CenterPaneel(){
setLayout( new GridLayout(2,3,10,10));
add( new Catagorie1Paneel() ); add( new Catagorie3Paneel() ); add( new SamenvattingPaneel() );
add( new Catagorie2Paneel() ); add( new Catagorie4Paneel() ); add( new DataPaneel());
}
And the code for the piechart is:
class DataPaneel extends JPanel{
private static final long serialVersionUID = -2980457707385367851L;
private final ObservableList<PieChart.Data> details = FXCollections.observableArrayList();
private BorderPane root;
private PieChart pieChart;
public void start(Stage primaryStage) {
primaryStage.setTitle("test");
details.addAll(new PieChart.Data("test1", 25),
new PieChart.Data("test 2", 25),
new PieChart.Data("test 3", 25),
new PieChart.Data("test 4", 25)
);
root = new BorderPane();
Scene scene = new Scene(root,600,500);
pieChart = new PieChart();
pieChart.setData(details);
pieChart.setTitle("test");
pieChart.setLegendSide(Side.BOTTOM);
pieChart.setLabelsVisible(true);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The output of the code so far is :
If someone could help me I would be very happy:)
Thanks in advance
Upvotes: 3
Views: 2143
Reputation:
Modify the constructor of CenterPaneel
this way:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pie_Chart extends JFrame {
public static void main(String[] args) {
Pie_Chart pc=new Pie_Chart();
pc.getContentPane().add(new CenterPaneel());
pc.setVisible(true);
}
}
class CenterPaneel extends JPanel{
private final static ObservableList<PieChart.Data> details = FXCollections.observableArrayList();
private static PieChart pieChart;
public CenterPaneel(){
setLayout( new GridLayout(2,3,10,10));
final JFXPanel dataPaneel = new JFXPanel();
ScrollPane sp = new ScrollPane();
details.addAll(new PieChart.Data("test1", 25),
new PieChart.Data("test 2", 25),
new PieChart.Data("test 3", 25),
new PieChart.Data("test 4", 25)
);
pieChart = new PieChart();
pieChart.setData(details);
pieChart.setTitle("test");
pieChart.setLegendSide(Side.BOTTOM);
pieChart.setLabelsVisible(true);
sp.setContent(pieChart);
Scene scene = new Scene(sp, 600, 500);
dataPaneel.setScene(scene);
add( new Catagorie1Paneel() ); add( new Catagorie3Paneel() ); add( new SamenvattingPaneel() );
add( new Catagorie2Paneel() ); add( new Catagorie4Paneel() ); add( dataPaneel);
}
}
I tested this with empty panels, except for the PieChart, and it worked!
Upvotes: 2
Reputation: 205785
As you are using a JavaFX PieChart
in a Swing application, you'll need to enclose the chart in a JFXPanel
. Instead of extending JPanel
, follow the outline shown here. A complete example that displays a JavaFX Label
is shown here. As an aside, your fragment never adds the pieChart
to the layout; maybe try something like this:
root.setCenter(pieChart);
Alternatively, add a ChartPanel
using jfreechart to your layout; a complete example, included in the distribution, is cited here.
Upvotes: 2