1

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.

jmgd
  • 25
  • 6
  • Perhaps an ini file? – sMaN Aug 10 '16 at 00:12
  • http://stackoverflow.com/questions/190629/what-is-the-easiest-way-to-parse-an-ini-file-in-java – sMaN Aug 10 '16 at 00:14
  • @sMaN can you be more specific on how it would work? – jmgd Aug 10 '16 at 00:36
  • Essentially loading the data on startup from a .ini file as key/value pairs as described in the link. Ultimately @Stewart 's recommendation would be better IMO. I'm not sure how it would differ from a .properties file in terms of capabilities except an external library like ini4j is required. – sMaN Aug 10 '16 at 03:46
  • JAXB could be a good option for (un)marshalling data from/to a xml file. – fabian Aug 10 '16 at 08:31

2 Answers2

3

The standard way to do this in Java is with a .properties file, which marries up with class java.util.Properties

There is a standard format of either:

  • key=value
  • <entry key="key">value</entry>

The Properties class has standard methods for loading the properties in and accessing them from the Properties object.

Referencing the J2SE Javadoc an example might be:

Properties properties = new Properties();
try(InputStream is = this.getClass().getResourceAsStream("my.properties")) {
    properties.load(is);
}
String value = properties.getProperty("key");
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • For what I read the properties file would only let me use one key and one value. For example: MB - 1024 KB However what would I do if I needed to have more MB pairs? Like MB - 1024 KB - 1/1024 GB? – jmgd Aug 10 '16 at 00:27
  • Here https://www.mkyong.com/java/java-properties-file-examples/ are plenty of examples for multiple properties – sMaN Aug 10 '16 at 00:29
  • Many keys, but each key only once. Each key is unique. If you need many values to a key, then do it as a comma separated list, such as `key=v1,v2,v3` and then parse out the values separately. – Stewart Aug 10 '16 at 00:29
0

There are two different library's I would recommend for storing data in java. The first is json-simple its simple and easy to use and there is little setup required to get it working you can find it here there are also a lot of tutorials. Another option is to use yaml. The library snake yaml is really good for this and again is easy to setup and there are many tutorials online. you can find it here.

Hamish
  • 84
  • 6