I've been teaching myself JavaFX and I'm developing a simple file transfer calculator. It calculates file sizes, transfer speeds and transfer times.
I want to be able to load my current options from a file (eg. Megabytes, Gigabytes etc) to several JavaFX ChoiceBoxes. I need:
- File Sizes
- Transfer Speeds
- Time Units
And I need to have information on how they convert since the user might want to know how much time in seconds it would take to transfer 1 TB of data through a 42 KB/s connection.
I thought of using a text file but it would be too much trouble formating in a way easy enough to be read by the file and it would be hard to automate a writing process. I thought of using an XML to do so but I have no idea of how to do use it for this purpose or if it would be a good idea. So, what would be the best way to load the options and the information on each one? And how to use it?
package application;
import java.io.ObjectInputStream.GetField;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
public class MainInterfaceController implements Initializable {
/*
* Declarations of the interface interactive bits FS - File Size TS -
* Transfer Speed TT - Transfer Time
*/
@FXML
private TextField FSTextField;
@FXML
private ChoiceBox<String> FSChoiceBox;
@FXML
private RadioButton FSRadioButton;
@FXML
private TextField TSTextField;
@FXML
private ChoiceBox<String> TSChoiceBox;
@FXML
private RadioButton TSRadioButton;
@FXML
private TextField TTTextField;
@FXML
private ChoiceBox<String> TTChoiceBox;
@FXML
private RadioButton TTRadioButton;
@FXML
private Button AboutButton;
@FXML
private Button CalculateButton;
// Variables & Data Sets
//These should be initialized in the init method so they can be populated with info from a file(?)
private ObservableList<String> fileSizes = FXCollections.observableArrayList("KB", "MB", "GB", "TB", "PB");
private ObservableList<String> transferSpeeds = FXCollections.observableArrayList("KB/s", "MB/s", "GB/s", "TB/s", "PB/s");
private ObservableList<String> timeUnits = FXCollections.observableArrayList("Seconds", "Minutes", "Hours");
@Override
public void initialize(URL location, ResourceBundle resources) {
populateChoiceBoxes();
setRadioButtonsActions();
CalculateButton.setOnAction(e -> calculateValues());
}
private void populateChoiceBoxes() {
FSChoiceBox.setItems(fileSizes);
TSChoiceBox.setItems(transferSpeeds);
TTChoiceBox.setItems(timeUnits);
}
private void setRadioButtonsActions() {
FSRadioButton.setOnAction(e -> clearRadioButtons(e));
TSRadioButton.setOnAction(e -> clearRadioButtons(e));
TTRadioButton.setOnAction(e -> clearRadioButtons(e));
}
private void clearRadioButtons(ActionEvent e) {
if (e.getSource() == FSRadioButton) {
TSRadioButton.setSelected(false);
TTRadioButton.setSelected(false);
}
if (e.getSource() == TSRadioButton) {
FSRadioButton.setSelected(false);
TTRadioButton.setSelected(false);
}
if (e.getSource() == TTRadioButton) {
FSRadioButton.setSelected(false);
TSRadioButton.setSelected(false);
}
}
private void calculateValues() {
if (FSRadioButton.isSelected()) {
try {
String TSText = TSTextField.getText();
double transferSpeed = Double.parseDouble(TSText);
String TTText = TTTextField.getText();
double transferTime = Double.parseDouble(TTText);
} catch (NumberFormatException e) {
Alert alert = new Alert(AlertType.ERROR, "The transfer speed/transfer time must be a number!");
alert.showAndWait();
}
}
}
private void convertSpeed(double transferSpeed, ChoiceBox<String> speedUnit) {
}
}
So far I have the code above. As you can see in the beggining I declare ObservableLists. That's what I want to automate and the final method for conversion. This would way the program would be more flexible and easily updatable.