Advertisement
Guest User

GUITHOMASALADDINE

a guest
Dec 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14.  
  15. import javax.swing.BoxLayout;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JScrollPane;
  23. import javax.swing.JTextArea;
  24. import javax.swing.JTree;
  25. import javax.swing.filechooser.FileSystemView;
  26. import javax.swing.tree.DefaultMutableTreeNode;
  27. import javax.swing.tree.TreeSelectionModel;
  28.  
  29. public class GUIExplorateur extends JFrame {
  30. /**
  31. *
  32. */
  33. private static final long serialVersionUID = 2098411530562087885L;
  34.  
  35. private JTextArea jtarea;
  36. private JMenu menu, menu2, menu3, submenu;
  37. private JMenuItem bar1, bar2, bar3, subbar, subbar2;
  38. private JFileChooser jfc;
  39. private JScrollPane scrollPaneTree, scrollPaneArea;
  40. private JTree tree;
  41.  
  42. public GUIExplorateur() {
  43. this("Explorateur de fichiers V0.1");
  44. }
  45.  
  46. public GUIExplorateur(String title) {
  47. super(title);
  48. // ************************************ ICI DEBUT DU PARAMETRAGE MENU ************************************//
  49. JMenuBar mb = new JMenuBar();
  50. menu = new JMenu("File");
  51. menu2 = new JMenu("Search");
  52. menu3 = new JMenu("More");
  53. submenu = new JMenu("Settings");
  54. bar1 = new JMenuItem("New");
  55. bar1.addActionListener(new ActionFileSaver());
  56. bar2 = new JMenuItem("Open");
  57. bar2.addActionListener(new ActionFileChooser());
  58. bar3 = new JMenuItem("HALP ME");
  59. subbar = new JMenuItem("HAHA");
  60. subbar2 = new JMenuItem("YOUHOU");
  61. menu.add(bar1);
  62. menu.add(bar2);
  63. menu.add(bar3);
  64. submenu.add(subbar);
  65. submenu.add(subbar2);
  66. menu.add(submenu);
  67. mb.add(menu);
  68. mb.add(menu2);
  69. mb.add(menu3);
  70. this.setJMenuBar(mb);
  71. // ************************************ ICI FIN DU PARAMETRAGE MENU ************************************//
  72.  
  73. // ********************************** ICI DEBUT DU PARAMETRAGE JTREE *********************************//
  74.  
  75. DefaultMutableTreeNode DossierRacine = new DefaultMutableTreeNode("DossierRacine");
  76. DefaultMutableTreeNode Dossier1 = new DefaultMutableTreeNode("Dossier1");
  77. Dossier1.add(new DefaultMutableTreeNode("Fichier.txt"));
  78. Dossier1.add(new DefaultMutableTreeNode("Fichier.zip"));
  79. Dossier1.add(new DefaultMutableTreeNode("Fichier.jpeg"));
  80. Dossier1.add(new DefaultMutableTreeNode("Fichier.png"));
  81. DefaultMutableTreeNode Dossier2 = new DefaultMutableTreeNode("Dossier2");
  82. Dossier2.add(new DefaultMutableTreeNode("Fichier.doc"));
  83. Dossier2.add(new DefaultMutableTreeNode("Fichier.csv"));
  84. Dossier2.add(new DefaultMutableTreeNode("Fichier.css"));
  85. Dossier2.add(new DefaultMutableTreeNode("Fichier.zip"));
  86. Dossier2.add(new DefaultMutableTreeNode("Fichier.jar"));
  87. DossierRacine.add(Dossier1);
  88. DossierRacine.add(Dossier2);
  89. tree = new JTree(DossierRacine);
  90. tree.setPreferredSize(new Dimension(150,100));
  91. scrollPaneTree = new JScrollPane(tree);
  92. this.getContentPane().add(scrollPaneTree, BorderLayout.LINE_START);
  93. // ************************************ ICI FIN DU PARAMETRAGE JTREE ************************************//
  94. // ******************************** ICI DEBUT DU PARAMETRAGE JTEXTAREA **********************************//
  95.  
  96. String details = "Détails : - MimeType - Chemin de Répertoire - Correspondance Signatures -Extension - Nom du fichier -etc \n";
  97. String errors = "Erreurs liés au fichier : ..................................................................................................";
  98. jtarea = new JTextArea(details + errors);
  99. scrollPaneArea = new JScrollPane(jtarea);
  100. jtarea.setEditable(false);
  101. jtarea.setPreferredSize(new Dimension(400,100));
  102. this.getContentPane().add(scrollPaneArea, BorderLayout.PAGE_END);
  103. // ******************************** ICI FIN DU PARAMETRAGE JTEXTAREA **********************************//
  104.  
  105. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106. this.setSize(750, 500);
  107. this.setLocation(850, 450);
  108. this.setVisible(true);
  109. }
  110.  
  111. class ActionFileChooser implements ActionListener {
  112. public void actionPerformed(ActionEvent e) {
  113. jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
  114. int returnValue = jfc.showOpenDialog(null);
  115. if (returnValue == JFileChooser.APPROVE_OPTION) {
  116. try {
  117. BufferedReader selectedFile = new BufferedReader(new FileReader(jfc.getSelectedFile()));
  118. String str;
  119. while ((str = selectedFile.readLine()) != null) {
  120. System.out.println(str);
  121. }
  122. selectedFile.close();
  123. } catch (FileNotFoundException e1) {
  124. System.out.println("File is not found!");
  125. } catch (IOException e1) {
  126. System.out.println("IOException");
  127. System.err.println(e1.getMessage());
  128. }
  129. }
  130. }
  131. }
  132.  
  133. class ActionFileSaver implements ActionListener {
  134. public void actionPerformed(ActionEvent e) {
  135. jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
  136. int returnValue = jfc.showSaveDialog(null);
  137. try {
  138. if (returnValue == JFileChooser.APPROVE_OPTION) {
  139. PrintWriter selectedFile = new PrintWriter(new FileWriter(jfc.getSelectedFile()));
  140. selectedFile.println(jfc.getSelectedFile());
  141. selectedFile.close();
  142. }
  143. } catch (IOException e1) {
  144. System.out.println("IOException");
  145. }
  146. }
  147. }
  148.  
  149. /*public static void main(String[] args) {
  150. new GUIExplorateur();
  151. }*/
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement