Create a Java FX GUI program that allows the user to select and open a file from
ID: 3576125 • Letter: C
Question
Create a Java FX GUI program that allows the user to select and open a file from a menu and then presents a chart of the file's data. Must do the following Program includes a menu Menu allows selection of a file via a file chooser Menu allows selection of several different types of charts Charts correctly display the data from the selected file The goal of this project is to write a Java FX GUI program that uses a menu to control the various program functions and display a chart. The menu should provide functionality to allow the user to select a file and present the file's data in a chart. Additionally the menu should allow the user to select from 4 different JavaFX (BarChart, AreaChart, LineChart, StackedChart) charts. Please use the provided CSV file for the data. The data covers program files written in different languages spread across four quarters.
I get an error: cannot find symbol private List datalist = new ArrayList();
Someone asked me for the DataModel class file, but I do not have one.
ChartMaker.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ChartMaker extends Application {
private Label lbl;
private List datalist = new ArrayList<>();
private BarChart myChart = null;
public static void main( String[] args ) {
launch( args );
}
public void start( Stage myStage ){
myStage.setTitle( "Greg's ChartMaker" );
BorderPane rootNode = new BorderPane();
Scene myScene = new Scene(rootNode, 600, 600);
myStage.setScene( myScene );
MenuBar mb = new MenuBar();
Menu fileMenu = new Menu( "_File" );
MenuItem menuOpen = new MenuItem("Open");
MenuItem menuExit = new MenuItem("Exit");
menuExit.setAccelerator(KeyCombination.keyCombination("shortcut+q"));
fileMenu.getItems().addAll(menuOpen, menuExit);
mb.getMenus().add( fileMenu );
Menu chartMenu = new Menu( "_Chart" );
RadioMenuItem menuBarChart = new RadioMenuItem("Bar chart");
RadioMenuItem menuAreaChart = new RadioMenuItem("Area chart");
RadioMenuItem menuLineChart = new RadioMenuItem("Line chart");
RadioMenuItem menuStackedChart = new RadioMenuItem("Stacked chart");
ToggleGroup tg = new ToggleGroup();
menuBarChart.setToggleGroup( tg );
menuAreaChart.setToggleGroup( tg );
menuLineChart.setToggleGroup( tg );
menuStackedChart.setToggleGroup( tg );
chartMenu.getItems().addAll(menuBarChart, menuAreaChart, menuLineChart, menuStackedChart);
mb.getMenus().add( chartMenu );
menuOpen.setOnAction( (ae) -> {
FileChooser f = new FileChooser();
f.setTitle( "Select a file" );
File selectedFile = f.showOpenDialog( myStage );
if( selectedFile != null ){
//read the data from the file
datalist = readData(selectedFile);
}
});
menuBarChart.setOnAction( (ae) -> {
if (null != datalist) {
doBarChart(datalist);
}
});
menuExit.setOnAction( (ae)-> {
Platform.exit();
});
rootNode.setTop(mb);
rootNode.setCenter(myChart);
myStage.show();
}
public List readData( File selectedFile) {
try {
datalist =
Files.lines( Paths.get(selectedFile.toString())).
map(l -> new DataModel(l)).
collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return datalist;
}
public void doBarChart( List datalist){
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Languages");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Program Files");
myChart = new BarChart(xAxis, yAxis);
myChart.setTitle("My Chart");
XYChart.Series spring = new XYChart.Series<>();
XYChart.Series summer = new XYChart.Series<>();
XYChart.Series fall = new XYChart.Series<>();
XYChart.Series winter = new XYChart.Series<>();
for( Object o : datalist) {
DataModel d = (DataModel)o;
if ("Spring" == d.getQuarter()) {
spring.setName(d.getQuarter());
spring.getData().add(new XYChart.Data(d.getLanguage(), d.getCount()));
} else if ("Summer" == d.getQuarter()) {
summer.setName(d.getQuarter());
summer.getData().add(new XYChart.Data(d.getLanguage(), d.getCount()));
} else if ("Fall" == d.getQuarter()) {
fall.setName(d.getQuarter());
fall.getData().add(new XYChart.Data(d.getLanguage(), d.getCount()));
} else {
winter.setName(d.getQuarter());
winter.getData().add(new XYChart.Data<>(d.getLanguage(), d.getCount()));
}
}
myChart.getData().add(spring);
myChart.getData().add(summer);
myChart.getData().add(fall);
myChart.getData().add(winter);
myChart.setAnimated(true);
}
}
data.csv Winter Python 42 Winter C++ 150 Winter Java 125 Winter C# 105 Spring Python 65 Spring C++ 110 Spring Java 178 Spring C# 120 Summer Python 56 Summer C++ 109 Summer Java 145 Summer C# 204 Fall Python 64 Fall C++ 95 Fall Java 168 Fall C# 139
Explanation / Answer
ChartMaker.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ChartMaker extends Application {
private Label lbl;
private List<DataModel> datalist = new ArrayList<>();
private BarChart<String, Number> myChart = null;
public static void main( String[] args ) {
launch( args );
}
public void start( Stage myStage ){
//Get things set up
myStage.setTitle( "Greg's ChartMaker" );
BorderPane rootNode = new BorderPane();
Scene myScene = new Scene(rootNode, 600, 600);
myStage.setScene( myScene );
//Add a menubar
MenuBar mb = new MenuBar();
//Add file menu stuff to it
Menu fileMenu = new Menu( "_File" );
MenuItem menuOpen = new MenuItem("Open");
MenuItem menuExit = new MenuItem("Exit");
menuExit.setAccelerator(KeyCombination.keyCombination("shortcut+q"));
fileMenu.getItems().addAll(menuOpen, menuExit);
mb.getMenus().add( fileMenu );
//Add chart menu stuff to it
Menu chartMenu = new Menu( "_Chart" );
RadioMenuItem menuBarChart = new RadioMenuItem("Bar chart");
RadioMenuItem menuAreaChart = new RadioMenuItem("Area chart");
RadioMenuItem menuLineChart = new RadioMenuItem("Line chart");
RadioMenuItem menuStackedChart = new RadioMenuItem("Stacked chart");
ToggleGroup tg = new ToggleGroup();
menuBarChart.setToggleGroup( tg );
menuAreaChart.setToggleGroup( tg );
menuLineChart.setToggleGroup( tg );
menuStackedChart.setToggleGroup( tg );
chartMenu.getItems().addAll(menuBarChart, menuAreaChart, menuLineChart, menuStackedChart);
mb.getMenus().add( chartMenu );
//Selecting the 'open' menu item opens a file selection dialog
menuOpen.setOnAction( (ae) -> {
FileChooser f = new FileChooser();
f.setTitle( "Select a file" );
File selectedFile = f.showOpenDialog( myStage );
if( selectedFile != null ){
//read the data from the file
datalist = readData(selectedFile);
}
});
//Selecting the Chart>BarChart menu item builds a bar chart
menuBarChart.setOnAction( (ae) -> {
if (null != datalist) {
doBarChart(datalist);
}
});
//Selecting the exit menu item exits the application
menuExit.setOnAction( (ae)-> {
Platform.exit();
});
//Add it to the top of the BorderPane
rootNode.setTop(mb);
//Draw the chart in the window
rootNode.setCenter(myChart);
//Draw the window
myStage.show();
}
public List readData( File selectedFile) {
//Now do some of that lambda stuff to get the file contents
try {
datalist =
Files.lines( Paths.get(selectedFile.toString())). //Stream of each line
map(l -> new DataModel(l)). // DataModel object for each line
collect(Collectors.toList()); //put the Datamodel objects in a list
} catch (IOException e) {
e.printStackTrace();
}
return datalist;
}
public void doBarChart( List datalist){
//Build the chart
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Languages");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Program Files");
myChart = new BarChart<String, Number>(xAxis, yAxis);
myChart.setTitle("My Chart");
XYChart.Series<String, Number> spring = new XYChart.Series<>();
XYChart.Series<String, Number> summer = new XYChart.Series<>();
XYChart.Series<String, Number> fall = new XYChart.Series<>();
XYChart.Series<String, Number> winter = new XYChart.Series<>();
for( Object o : datalist) {
DataModel d = (DataModel)o;
if ("Spring" == d.getQuarter()) {
spring.setName(d.getQuarter());
spring.getData().add(new XYChart.Data<String, Number>(d.getLanguage(), d.getCount()));
} else if ("Summer" == d.getQuarter()) {
summer.setName(d.getQuarter());
summer.getData().add(new XYChart.Data<String, Number>(d.getLanguage(), d.getCount()));
} else if ("Fall" == d.getQuarter()) {
fall.setName(d.getQuarter());
fall.getData().add(new XYChart.Data<String, Number>(d.getLanguage(), d.getCount()));
} else {
winter.setName(d.getQuarter());
winter.getData().add(new XYChart.Data<>(d.getLanguage(), d.getCount()));
}
}
myChart.getData().add(spring);
myChart.getData().add(summer);
myChart.getData().add(fall);
myChart.getData().add(winter);
myChart.setAnimated(true);
}
}
DataModel.java
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.concurrent.atomic.DoubleAccumulator;
public class DataModel {
private String quarter;
private String language;
private Integer count;
public DataModel(String quarter, String language, int count) {
this.quarter = quarter;
this.language = language;
this.count = count;
}
//A constructor that takes in a complete line from the csv file
public DataModel(String s){
//Winter,Python,42
Pattern commaPattern = Pattern.compile(",");
List<String> data = commaPattern.splitAsStream(s).collect(Collectors.toList());
this.quarter = data.get(0);
this.language = data.get(1);
this.count = Integer.parseInt(data.get(2));
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getQuarter() {
return quarter;
}
public void setQuarter(String quarter) {
this.quarter = quarter;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}