Advertisement
YauhenMardan

Untitled

Dec 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.util.*;
  10.  
  11. public class WindowApp extends JFrame {
  12. private final static int LIST_X=300;
  13. private final static int LIST_Y=500;
  14.  
  15. JMenu menu;
  16. JMenuBar menuBar;
  17. JMenuItem menuItem1;
  18. JMenuItem menuItem2;
  19. JScrollPane scrollPaneStudents;
  20. JScrollPane scrollPaneTask;
  21. JPanel mainPanel;
  22. JPanel northPanel;
  23. JButton addDataButton;
  24. JButton chooseFileButton;
  25. JButton sortButton;
  26. JList<Student> listStudents;
  27. JList<Student> listTask;
  28.  
  29. public ArrayList<Student> getModelStudents() {
  30. return studentArrayList;
  31. }
  32.  
  33. DefaultListModel<Student> modelStudents;
  34. ArrayList<Student> studentArrayList;
  35. ArrayList<Student> studentTempArrayList;
  36.  
  37. WindowApp(){
  38. //panel
  39. mainPanel=new JPanel();
  40. mainPanel.setLayout(new BorderLayout());
  41. //buttons
  42. addDataButton=new JButton("Add new students...");
  43. chooseFileButton=new JButton("Choose text file...");
  44. sortButton=new JButton("Sort");
  45. addDataButton.setPreferredSize(new Dimension(LIST_X,20));
  46. sortButton.setPreferredSize(new Dimension(LIST_X,20));
  47. //
  48. northPanel=new JPanel();
  49. northPanel.add(addDataButton, BorderLayout.WEST);
  50. mainPanel.add(chooseFileButton,BorderLayout.SOUTH);
  51. northPanel.add(sortButton,BorderLayout.EAST);
  52. mainPanel.add(northPanel,BorderLayout.NORTH);
  53. //model
  54. studentArrayList=new ArrayList<>();
  55. //lists
  56. listStudents=new JList<>();
  57. scrollPaneStudents= new JScrollPane(listStudents);
  58. scrollPaneStudents.setPreferredSize(new Dimension(LIST_X,LIST_Y));
  59. mainPanel.add(scrollPaneStudents,BorderLayout.WEST);
  60. listTask=new JList<>();
  61. scrollPaneTask=new JScrollPane(listTask);
  62. scrollPaneTask.setPreferredSize(new Dimension(LIST_X,LIST_Y));
  63. mainPanel.add(scrollPaneTask,BorderLayout.EAST);
  64. //menu bar
  65. menuBar = new JMenuBar();
  66. menu= new JMenu("File");
  67. menuItem1=new JMenuItem("Add student...");
  68. menuItem2=new JMenuItem("Choose file...");
  69. menu.add(menuItem1);
  70. menu.add(menuItem2);
  71. menuBar.add(menu);
  72. setJMenuBar(menuBar);
  73.  
  74. //window
  75. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  76. setResizable(false);
  77. setContentPane(mainPanel);
  78. pack();
  79. //listeners
  80. sortButton.addActionListener(new ActionListener() {
  81. @Override
  82. public void actionPerformed(ActionEvent e) {
  83. MySort2 mySort2=new MySort2();
  84. studentTempArrayList=new ArrayList<>();
  85. Iterator<Student> interalIt;
  86. Iterator<Student> externalIt=studentArrayList.iterator();
  87. while (externalIt.hasNext()){
  88. Student studentExternal=externalIt.next();
  89. interalIt=studentArrayList.iterator();
  90. while (interalIt.hasNext()){
  91. Student internalStudent=interalIt.next();
  92. if(mySort2.compare(internalStudent,studentExternal)==0){
  93. studentTempArrayList.add(internalStudent);
  94. }
  95. }
  96. }
  97. HashSet<Student> tempSet = new HashSet<>(studentTempArrayList);
  98. studentTempArrayList=new ArrayList<>(tempSet);
  99. MySort1 mySort1=new MySort1();
  100. Collections.sort(studentTempArrayList,mySort1);
  101. updateTaskList();
  102. }
  103. });
  104. addDataButton.addActionListener(new ActionListener() {
  105. @Override
  106. public void actionPerformed(ActionEvent e) {
  107. AddDataDialog dialog= new AddDataDialog(WindowApp.this);
  108. dialog.setVisible(true);
  109. }
  110. });
  111. menuItem1.addActionListener();
  112. menuItem2.addActionListener();
  113. chooseFileButton.addActionListener(new ActionListener() {
  114. @Override
  115. public void actionPerformed(ActionEvent e) {
  116. JFileChooser fileOpen = new JFileChooser();
  117. int ret = fileOpen.showOpenDialog(null);
  118. if(ret ==JFileChooser.APPROVE_OPTION) {
  119. try {
  120. int recBookNum;
  121. String surname;
  122. int course;
  123. int group;
  124. Scanner scanner = new Scanner(new File(fileOpen.getSelectedFile().getAbsolutePath()));
  125. while (scanner.hasNext()){
  126. recBookNum=scanner.nextInt();
  127. surname=scanner.next();
  128. course=scanner.nextInt();
  129. group=scanner.nextInt();
  130. studentArrayList.add(new Student(recBookNum,surname,course,group));
  131. updateList();
  132. }
  133. }
  134. catch (FileNotFoundException exception){
  135. JOptionPane.showMessageDialog(WindowApp.this,
  136. "Cannot open the file.",
  137. "Inane warning",
  138. JOptionPane.WARNING_MESSAGE);
  139. }
  140. }
  141. }
  142. });
  143. }
  144. public void updateList(){
  145. modelStudents = new DefaultListModel<>();
  146. Iterator<Student> iterator=studentArrayList.iterator();
  147. while (iterator.hasNext()){
  148. modelStudents.addElement(iterator.next());
  149. }
  150. listStudents.setModel(modelStudents);
  151. }
  152. public void updateTaskList(){
  153. modelStudents = new DefaultListModel<>();
  154. Iterator<Student> iterator=studentTempArrayList.iterator();
  155. while (iterator.hasNext()){
  156. modelStudents.addElement(iterator.next());
  157. }
  158. listTask.setModel(modelStudents);
  159. }
  160. class
  161. }
  162. class MySort1 implements Comparator<Student> {
  163.  
  164. public int compare(Student a, Student b) {
  165. if(a.getCourse()>b.getCourse()){
  166. return 1;
  167. }
  168. else if (a.getCourse()<b.getCourse()) {
  169. return -1;
  170. }
  171. else{
  172. if(a.getGroup()>b.getGroup()){
  173. return 1;
  174. }
  175. else if (a.getGroup()<b.getGroup()) {
  176. return -1;
  177. }
  178. else{
  179. return a.getSurname().compareTo(b.getSurname());
  180. }
  181. }
  182. }
  183. }
  184. class MySort2 implements Comparator<Student> {
  185.  
  186. public int compare(Student a, Student b) {
  187. if(a==b) return -1;
  188. return a.getSurname().compareTo(b.getSurname());
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement