Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package sem3task3;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectOutputStream;
- import java.net.URL;
- import java.util.ResourceBundle;
- import javafx.beans.value.ObservableValue;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- /**
- *
- * @author AlbinSkola
- */
- public class FXMLDocumentController implements Initializable {
- @FXML
- private Label label;
- @FXML
- private TextField tf1;
- @FXML
- private TextField tf2;
- @FXML
- private TextField tf3;
- @FXML
- private TextField tf4;
- @FXML
- private TextField tf5;
- @FXML
- private TextField tf6;
- @FXML
- private TextField tf7;
- @FXML
- private TextField tf8;
- @FXML
- private Button b1;
- @FXML
- private Button b2;
- File file = new File("binary.dat");
- String sumBin;
- @FXML
- private void handleButtonAction1(ActionEvent event) {
- //Convert content
- int sum = 0;
- try{
- if(tf1.getText().equals("1"))
- sum += 1;
- if(tf2.getText().equals("1"))
- sum += 2;
- if(tf3.getText().equals("1"))
- sum += 4;
- if(tf4.getText().equals("1"))
- sum += 8;
- if(tf5.getText().equals("1"))
- sum += 16;
- if(tf6.getText().equals("1"))
- sum += 32;
- if(tf7.getText().equals("1"))
- sum += 64;
- if(tf8.getText().equals("1"))
- sum += 128;
- System.out.println(sum);
- }catch (Exception ex) {
- ex.printStackTrace();
- }
- try (DataOutputStream dataOut = new DataOutputStream(new FileOutputStream(file))) {
- dataOut.writeByte(sum);
- } catch (Exception ex) {
- System.out.println("Något gick fel");
- ex.printStackTrace();
- }
- }
- @FXML
- private void handleButtonAction2(ActionEvent event) {
- try (DataInputStream dataIn = new DataInputStream(new FileInputStream("binary.dat"))) {
- int sum1 = dataIn.readUnsignedByte();
- System.out.println(sum1);
- sumBin = Integer.toBinaryString(sum1);
- System.out.println(sumBin);
- } catch (Exception ex) {
- System.out.println("Något gick fel");
- ex.printStackTrace();
- }
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- tf1.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
- if(newValue.length() != 0){
- char c = newValue.charAt(newValue.length()-1);
- if (!Character.isDigit(c)) {
- tf1.setText(oldValue);
- }
- if (tf1.getText().length() > 1) {
- tf1.setText(oldValue);
- }
- if (tf1.getText().equals("2")) {
- tf1.setText(oldValue);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement