Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package sem3task3;
  8.  
  9. import java.io.DataInputStream;
  10. import java.io.DataOutputStream;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.ObjectOutputStream;
  15. import java.net.URL;
  16. import java.util.ResourceBundle;
  17. import javafx.beans.value.ObservableValue;
  18. import javafx.event.ActionEvent;
  19. import javafx.fxml.FXML;
  20. import javafx.fxml.Initializable;
  21. import javafx.scene.control.Button;
  22. import javafx.scene.control.Label;
  23. import javafx.scene.control.TextField;
  24.  
  25. /**
  26.  *
  27.  * @author AlbinSkola
  28.  */
  29. public class FXMLDocumentController implements Initializable {
  30.    
  31.     @FXML
  32.     private Label label;
  33.    
  34.     @FXML
  35.     private TextField tf1;
  36.    
  37.     @FXML
  38.     private TextField tf2;
  39.            
  40.     @FXML
  41.     private TextField tf3;
  42.            
  43.     @FXML
  44.     private TextField tf4;
  45.            
  46.     @FXML
  47.     private TextField tf5;    
  48.            
  49.     @FXML
  50.     private TextField tf6;
  51.    
  52.     @FXML
  53.     private TextField tf7;
  54.            
  55.     @FXML
  56.     private TextField tf8;    
  57.    
  58.     @FXML
  59.     private Button b1;
  60.    
  61.     @FXML
  62.     private Button b2;
  63.    
  64.     File file = new File("binary.dat");
  65.     String sumBin;
  66.            
  67.     @FXML
  68.     private void handleButtonAction1(ActionEvent event) {
  69.         //Convert content
  70.         int sum = 0;
  71.        
  72.         try{
  73.             if(tf1.getText().equals("1"))
  74.             sum += 1;
  75.            
  76.             if(tf2.getText().equals("1"))
  77.                 sum += 2;
  78.            
  79.             if(tf3.getText().equals("1"))
  80.                 sum += 4;
  81.            
  82.             if(tf4.getText().equals("1"))
  83.                 sum += 8;
  84.            
  85.             if(tf5.getText().equals("1"))
  86.                 sum += 16;
  87.            
  88.             if(tf6.getText().equals("1"))
  89.                 sum += 32;
  90.            
  91.             if(tf7.getText().equals("1"))
  92.                 sum += 64;
  93.            
  94.             if(tf8.getText().equals("1"))
  95.                 sum += 128;
  96.            
  97.             System.out.println(sum);
  98.            
  99.         }catch (Exception ex) {
  100.             ex.printStackTrace();
  101.         }
  102.        
  103.         try (DataOutputStream dataOut = new DataOutputStream(new FileOutputStream(file))) {
  104.             dataOut.writeByte(sum);
  105.         } catch (Exception ex) {
  106.             System.out.println("Något gick fel");
  107.             ex.printStackTrace();
  108.         }
  109.     }
  110.    
  111.     @FXML
  112.     private void handleButtonAction2(ActionEvent event) {
  113.         try (DataInputStream dataIn = new DataInputStream(new FileInputStream("binary.dat"))) {
  114.             int sum1 = dataIn.readUnsignedByte();
  115.             System.out.println(sum1);
  116.             sumBin = Integer.toBinaryString(sum1);
  117.             System.out.println(sumBin);
  118.         } catch (Exception ex) {
  119.             System.out.println("Något gick fel");
  120.             ex.printStackTrace();
  121.         }
  122.     }
  123.    
  124.     @Override
  125.     public void initialize(URL url, ResourceBundle rb) {
  126.        
  127.         tf1.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
  128.     if(newValue.length() != 0){
  129.         char c = newValue.charAt(newValue.length()-1);
  130.         if (!Character.isDigit(c)) {
  131.             tf1.setText(oldValue);
  132.         }
  133.        
  134.         if (tf1.getText().length() > 1) {
  135.             tf1.setText(oldValue);
  136.         }
  137.        
  138.         if (tf1.getText().equals("2")) {
  139.             tf1.setText(oldValue);
  140.         }
  141.     }
  142. });
  143.        
  144.        
  145.        
  146.        
  147.        
  148.     }    
  149.    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement