YauhenMardan

Untitled

Dec 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 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. JScrollPane scrollPaneStudents;
  16. JScrollPane scrollPaneTask;
  17. JPanel mainPanel;
  18. JPanel northPanel;
  19. JButton addDataButton;
  20. JButton chooseFileButton;
  21. JButton sortButton;
  22. JList<Student> listStudents;
  23. JList<Student> listTask;
  24.  
  25. public ArrayList<Student> getModelStudents() {
  26. return studentArrayList;
  27. }
  28.  
  29. DefaultListModel<Student> modelStudents;
  30. ArrayList<Student> studentArrayList;
  31. ArrayList<Student> studentTempArrayList;
  32.  
  33. WindowApp(){
  34. //panel
  35. mainPanel=new JPanel();
  36. mainPanel.setLayout(new BorderLayout());
  37. //buttons
  38. addDataButton=new JButton("Add new students...");
  39. chooseFileButton=new JButton("Choose text file...");
  40. sortButton=new JButton("Sort");
  41. addDataButton.setPreferredSize(new Dimension(LIST_X,20));
  42. sortButton.setPreferredSize(new Dimension(LIST_X,20));
  43. //
  44. northPanel=new JPanel();
  45. northPanel.add(addDataButton, BorderLayout.WEST);
  46. mainPanel.add(chooseFileButton,BorderLayout.SOUTH);
  47. northPanel.add(sortButton,BorderLayout.EAST);
  48. mainPanel.add(northPanel,BorderLayout.NORTH);
  49. //model
  50. studentArrayList=new ArrayList<>();
  51. //lists
  52. listStudents=new JList<>();
  53. scrollPaneStudents= new JScrollPane(listStudents);
  54. scrollPaneStudents.setPreferredSize(new Dimension(LIST_X,LIST_Y));
  55. mainPanel.add(scrollPaneStudents,BorderLayout.WEST);
  56. listTask=new JList<>();
  57. scrollPaneTask=new JScrollPane(listTask);
  58. scrollPaneTask.setPreferredSize(new Dimension(LIST_X,LIST_Y));
  59. mainPanel.add(scrollPaneTask,BorderLayout.EAST);
  60. //window
  61. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  62. setResizable(false);
  63. setContentPane(mainPanel);
  64. pack();
  65. //listeners
  66. sortButton.addActionListener(new ActionListener() {
  67. @Override
  68. public void actionPerformed(ActionEvent e) {
  69. studentTempArrayList=new ArrayList<>();
  70. Iterator<Student> interalIt;
  71. Iterator<Student> externalIt=studentArrayList.iterator();
  72. while (externalIt.hasNext()){
  73. Student studentExternal=externalIt.next();
  74. interalIt=studentArrayList.iterator();
  75. while (interalIt.hasNext()){
  76. Student internalStudent=interalIt.next();
  77. if(internalStudent.equals(studentExternal)){
  78. studentTempArrayList.add(internalStudent);
  79. }
  80. }
  81. }
  82. MySort mySort=new MySort();
  83. Collections.sort(studentTempArrayList,mySort);
  84. updateTaskList();
  85. }
  86. });
  87. addDataButton.addActionListener(new ActionListener() {
  88. @Override
  89. public void actionPerformed(ActionEvent e) {
  90. AddDataDialog dialog= new AddDataDialog(WindowApp.this);
  91. dialog.setVisible(true);
  92. }
  93. });
  94. chooseFileButton.addActionListener(new ActionListener() {
  95. @Override
  96. public void actionPerformed(ActionEvent e) {
  97. JFileChooser fileOpen = new JFileChooser();
  98. int ret = fileOpen.showOpenDialog(null);
  99. if(ret ==JFileChooser.APPROVE_OPTION) {
  100. try {
  101. int recBookNum;
  102. String surname;
  103. int course;
  104. int group;
  105. Scanner scanner = new Scanner(new File(fileOpen.getSelectedFile().getAbsolutePath()));
  106. while (scanner.hasNext()){
  107. recBookNum=scanner.nextInt();
  108. surname=scanner.next();
  109. course=scanner.nextInt();
  110. group=scanner.nextInt();
  111. studentArrayList.add(new Student(recBookNum,surname,course,group));
  112. updateList();
  113. }
  114. }
  115. catch (FileNotFoundException exception){
  116. JOptionPane.showMessageDialog(WindowApp.this,
  117. "Cannot open the file.",
  118. "Inane warning",
  119. JOptionPane.WARNING_MESSAGE);
  120. }
  121. }
  122. }
  123. });
  124. }
  125. public void updateList(){
  126. modelStudents = new DefaultListModel<>();
  127. Iterator<Student> iterator=studentArrayList.iterator();
  128. while (iterator.hasNext()){
  129. modelStudents.addElement(iterator.next());
  130. }
  131. listStudents.setModel(modelStudents);
  132. }
  133. public void updateTaskList(){
  134. modelStudents = new DefaultListModel<>();
  135. Iterator<Student> iterator=studentTempArrayList.iterator();
  136. while (iterator.hasNext()){
  137. modelStudents.addElement(iterator.next());
  138. }
  139. listTask.setModel(modelStudents);
  140. }
  141. }
  142. class MySort implements Comparator<Student> {
  143.  
  144. public int compare(Student a, Student b) {
  145. if(a.getCourse()>b.getCourse()){
  146. return 1;
  147. }
  148. else if (a.getCourse()<b.getCourse()) {
  149. return -1;
  150. }
  151. else{
  152. if(a.getGroup()>b.getGroup()){
  153. return 1;
  154. }
  155. else if (a.getGroup()<b.getGroup()) {
  156. return -1;
  157. }
  158. else{
  159. return a.getSurname().compareTo(b.getSurname());
  160. }
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment