Guest User

Untitled

a guest
May 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5.  
  6. public class SwingFC implements ActionListener {
  7.  
  8. JTextField jtfFirst; // holds the first file name
  9. JTextField jtfSecond; // holds the second file name
  10.  
  11. JButton jbtnComp; // button to compare the files
  12.  
  13. JLabel jlabFirst, jlabSecond; // displays prompts
  14. JLabel jlabResult; // displays results and error messages
  15.  
  16. SwingFC() {
  17.  
  18. // Create a new JFrame container.
  19. JFrame jfrm = new JFrame("Compare Files");
  20.  
  21. // Specify FlowLayout for the layout manager.
  22. jfrm.setLayout(new FlowLayout());
  23.  
  24. // Give the frame an initial size.
  25. jfrm.setSize(200, 190);
  26.  
  27. // Terminate the program when the user closes the application.
  28. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.  
  30. // Create the text fields for the file names..
  31. jtfFirst = new JTextField(14);
  32. jtfSecond = new JTextField(14);
  33.  
  34. // Set the action commands for the text fields.
  35. jtfFirst.setActionCommand("fileA");
  36. jtfSecond.setActionCommand("fileB");
  37.  
  38. // Create the Compare button.
  39. JButton jbtnComp = new JButton("Compare");
  40.  
  41. // Add action listener for the Compare button.
  42. jbtnComp.addActionListener(this);
  43.  
  44. // Create the labels.
  45. jlabFirst = new JLabel("First file: ");
  46. jlabSecond = new JLabel("Second file: ");
  47. jlabResult = new JLabel("");
  48.  
  49. // Add the components to the content pane.
  50. jfrm.add(jlabFirst);
  51. jfrm.add(jtfFirst);
  52. jfrm.add(jlabSecond);
  53. jfrm.add(jtfSecond);
  54. jfrm.add(jbtnComp);
  55. jfrm.add(jlabResult);
  56.  
  57. // Display the frame.
  58. jfrm.setVisible(true);
  59. }
  60.  
  61. // Compare the files when the Compare button is pressed.
  62. public void actionPerformed(ActionEvent ae) {
  63. int i=0, j=0;
  64.  
  65. // First, confirm that both file names have
  66. // been entered.
  67. if(jtfFirst.getText().equals("")) {
  68. jlabResult.setText("First file name missing.");
  69. return;
  70. }
  71. if(jtfSecond.getText().equals("")) {
  72. jlabResult.setText("Second file name missing.");
  73. return;
  74. }
  75.  
  76. // Compare files. Use try-with-resources to manage the files.
  77. try (FileInputStream f1 = new FileInputStream(jtfFirst.getText());
  78. FileInputStream f2 = new FileInputStream(jtfSecond.getText()))
  79. {
  80. // Check the contents of each file.
  81. do {
  82. i = f1.read();
  83. j = f2.read();
  84. if(i != j) break;
  85. } while(i != -1 && j != -1);
  86.  
  87. if(i != j)
  88. jlabResult.setText("Files are not the same.");
  89. else
  90. jlabResult.setText("Files compare equal.");
  91. } catch(IOException exc) {
  92. jlabResult.setText("File Error");
  93. }
  94. }
  95.  
  96. public static void main(String args[]) {
  97. // Create the frame on the event dispatching thread.
  98. SwingUtilities.invokeLater(new Runnable() {
  99. public void run() {
  100. new SwingFC();
  101. }
  102. });
  103. }
  104. }
Add Comment
Please, Sign In to add comment