Guest User

Untitled

a guest
Jun 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import javax.swing.table.*;
  4. import java.awt.event.*;
  5. import java.awt.print.*;
  6. import java.io.*;
  7. import java.net.URLDecoder;
  8.  
  9. class TablePanel extends JPanel {
  10. Table[][][] ttable;
  11. DefaultTableModel[] model = new DefaultTableModel[15];
  12. JComboBox<String> comboBox = new JComboBox<String>();
  13. JButton print = new JButton("Print");
  14. JTable Table;
  15. String path, decodedPath, file;
  16.  
  17. public TablePanel(Table[][][] t, int stgrp, InputData input, JMenuItem save) {
  18. double[] time = {9.30, 10.30, 11.30, 12.30, 14.30, 15.30, 16.30};
  19. String[] day = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
  20. ttable = t;
  21. int stno;
  22. setLayout(new BorderLayout());
  23.  
  24. for(stno=0;stno<stgrp;stno++) {
  25. model[stno] = new DefaultTableModel();
  26. comboBox.addItem(input.stgrp[stno].code);
  27. }
  28. add(comboBox, BorderLayout.SOUTH);
  29.  
  30. Table = new JTable(model[0]) {
  31. public boolean isCellEditable(int rowIndex, int colIndex) {
  32. return false;
  33. }
  34. };
  35.  
  36. for(stno=0;stno<stgrp;stno++) {
  37. model[stno].addColumn("");
  38. for(int i=1;i<=5;i++) {
  39. model[stno].addColumn(day[i-1]);
  40. }
  41. }
  42.  
  43. for(stno=0;stno<stgrp;stno++) {
  44. for(int i=1;i<=4;i++) {
  45. double x=time[i-1]+1;
  46. model[stno].addRow(new Object[]{time[i-1]+"0-"+x+"0",ttable[stno][i-1][0].course+" "+ttable[stno][i-1][0].ins+" "+ttable[stno][i-1][0].room,ttable[stno][i-1][1].course+" "+ttable[stno][i-1][1].ins+" "+ttable[stno][i-1][1].room,ttable[stno][i-1][2].course+" "+ttable[stno][i-1][2].ins+" "+ttable[stno][i-1][2].room,ttable[stno][i-1][3].course+" "+ttable[stno][i-1][3].ins+" "+ttable[stno][i-1][3].room,ttable[stno][i-1][4].course+" "+ttable[stno][i-1][4].ins+" "+ttable[stno][i-1][4].room});
  47. }
  48.  
  49. model[stno].addRow(new Object[]{"13.30-14.30", "-- -- --", "-- -- --", "-- -- --", "-- -- --", "-- -- --"});
  50.  
  51. for(int i=5;i<=7;i++) {
  52. double x=time[i-1]+1;
  53. model[stno].addRow(new Object[]{time[i-1]+"0-"+x+"0",ttable[stno][i-1][0].course+" "+ttable[stno][i-1][0].ins+" "+ttable[stno][i-1][0].room,ttable[stno][i-1][1].course+" "+ttable[stno][i-1][1].ins+" "+ttable[stno][i-1][1].room,ttable[stno][i-1][2].course+" "+ttable[stno][i-1][2].ins+" "+ttable[stno][i-1][2].room,ttable[stno][i-1][3].course+" "+ttable[stno][i-1][3].ins+" "+ttable[stno][i-1][3].room,ttable[stno][i-1][4].course+" "+ttable[stno][i-1][4].ins+" "+ttable[stno][i-1][4].room});
  54. }
  55. }
  56.  
  57. comboBox.addItemListener(new ItemListener() {
  58. public void itemStateChanged(ItemEvent e) {
  59. Table.setModel(model[comboBox.getSelectedIndex()]);
  60. }
  61. });
  62.  
  63. add(Table, BorderLayout.CENTER);
  64. add(Table.getTableHeader(), BorderLayout.NORTH);
  65.  
  66. add(print, BorderLayout.EAST);
  67. print.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. try {
  70. boolean complete = Table.print();
  71. if (complete) {
  72. JOptionPane.showMessageDialog(null, "Printing Complete!", "Printing Result", JOptionPane.INFORMATION_MESSAGE);
  73. }
  74. else {
  75. JOptionPane.showMessageDialog(null, "Printing Cancelled!", "Printing Result", JOptionPane.INFORMATION_MESSAGE);
  76. }
  77. }
  78. catch (PrinterException pe) {
  79. }
  80. }
  81. });
  82.  
  83. save.addActionListener(new ActionListener() {
  84. public void actionPerformed(ActionEvent e) {
  85. path = TablePanel.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  86. try {
  87. decodedPath = URLDecoder.decode(path, "UTF-8");
  88. }
  89. catch(Exception ee) {
  90. }
  91. file = decodedPath + comboBox.getSelectedItem().toString() + "TimeTable.xls";
  92. toExcel(Table, new File(file));
  93. }
  94. });
  95. }
  96.  
  97. public void toExcel(JTable Table, File file) {
  98. try {
  99. TableModel model = Table.getModel();
  100. FileWriter excel = new FileWriter(file);
  101.  
  102. for(int i = 0; i < model.getColumnCount(); i++){
  103. excel.write(model.getColumnName(i) + "t");
  104. }
  105. excel.write("n");
  106.  
  107. for(int i=0; i< model.getRowCount(); i++) {
  108. for(int j=0; j < model.getColumnCount(); j++) {
  109. excel.write(model.getValueAt(i,j).toString()+"t");
  110. }
  111. excel.write("n");
  112. }
  113. excel.close();
  114. }
  115.  
  116. catch(IOException e) {
  117. JOptionPane.showMessageDialog(null, "File failed to save!", "ERROR!", JOptionPane.ERROR_MESSAGE);
  118. }
  119.  
  120. JOptionPane.showMessageDialog(null, "TimeTable successfully saved to " + file, "SUCCESSFUL!", JOptionPane.INFORMATION_MESSAGE);
  121. }
  122. }
Add Comment
Please, Sign In to add comment