Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.JScrollPane;
  4. import javax.swing.JTable;
  5. import javax.swing.table.TableModel;
  6. import javax.swing.table.DefaultTableModel;
  7. import javax.swing.table.TableColumn;
  8. import javax.swing.table.TableColumnModel;
  9. import javax.swing.JMenuBar;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuItem;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JLabel;
  14. import javax.swing.SwingConstants;
  15. import javax.swing.JTextField;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.awt.GridLayout;
  22. import java.awt.BorderLayout;
  23. import java.awt.event.MouseAdapter;
  24. import java.awt.event.MouseEvent;
  25. import java.awt.event.WindowEvent;
  26.  
  27. public class BMICalculator extends JFrame implements Runnable
  28. {
  29. JMenuBar mbar;
  30. JScrollPane pane;
  31. DefaultTableModel model;
  32. JTable table;
  33. boolean ascending;
  34. String col[] = {"Last","First","Height","Weight","BMI"};
  35. List<Person> al;
  36. public BMICalculator()
  37. {
  38. ascending = true;
  39. al = new ArrayList<Person>();
  40. al.add(new Person("Deora", "Pintoo", 74, 100));
  41. al.add(new Person("Morrison", "John", 67, 165));
  42. al.add(new Person("Baraldi", "Thomas", 69, 135));
  43. al.add(new Person("Zhang", "Justin", 71, 160));
  44. al.add(new Person("Ellwanger", "Robert", 75, 200));
  45. al.add(new Person("Bristol", "Henry", 71, 145));
  46. al.add(new Person("Sartin", "Aaron", 72, 170));
  47. al.add(new Person("Johnson", "Michael", 73, 175));
  48. al.add(new Person("Webb", "Alan", 69, 145));
  49. }
  50. public void run()
  51. {
  52. model = new DefaultTableModel(col,al.size())
  53. {
  54. @Override
  55. public boolean isCellEditable(int arg0, int arg1)
  56. {
  57. return false;
  58. }
  59. };
  60. table = new JTable(model);
  61. table.getTableHeader().setReorderingAllowed(false);
  62. pane = new JScrollPane(table);
  63. add(pane);
  64. makeMenu();
  65. makeTable();
  66. setVisible(true);
  67. setSize(500,400);
  68. setLayout(new GridLayout());
  69. setDefaultCloseOperation(EXIT_ON_CLOSE);
  70. }
  71. public void makeMenu()
  72. {
  73. mbar = new JMenuBar();
  74. setJMenuBar(mbar);
  75. JMenu sort = new JMenu("sort");
  76. mbar.add(sort);
  77. JMenuItem last = new JMenuItem("Last Name");
  78. sort.add(last);
  79. last.addActionListener( e -> {
  80. if(ascending)
  81. {
  82. Collections.sort(al);
  83. }
  84. else
  85. {
  86. Collections.sort(al);
  87. Collections.reverse(al);
  88. }
  89. makeTable();
  90. });
  91. JMenuItem first = new JMenuItem("First Name");
  92. sort.add(first);
  93. first.addActionListener( e -> {
  94. if(ascending)
  95. {
  96. Comparator<Person> firstFirst = (n0, n1) ->
  97. {
  98. if( !(n0.first.equalsIgnoreCase(n1.first)))
  99. {
  100. return n0.first.compareToIgnoreCase(n1.first);
  101. }
  102. return n0.last.compareToIgnoreCase(n1.last);
  103. };
  104. Collections.sort(al, firstFirst);
  105. }
  106. else
  107. {
  108. Comparator<Person> firstFirst = (n0, n1) ->
  109. {
  110. if( !(n1.first.equalsIgnoreCase(n0.first)))
  111. {
  112. return n1.first.compareToIgnoreCase(n0.first);
  113. }
  114. return n1.last.compareToIgnoreCase(n0.last);
  115. };
  116. Collections.sort(al, firstFirst);
  117. }
  118. makeTable();
  119. });
  120. JMenuItem height = new JMenuItem("Height");
  121. sort.add(height);
  122. height.addActionListener( e -> {
  123. if(ascending)
  124. {
  125. Comparator<Person> heightFirst = (n0, n1) ->
  126. {
  127. if( !(n0.height == n1.height))
  128. {
  129. return n0.height- n1.height;
  130. }
  131. else if ( !(n0.last.equalsIgnoreCase(n1.last)))
  132. {
  133. return n0.last.compareToIgnoreCase(n1.last);
  134. }
  135. return n0.first.compareToIgnoreCase(n1.first);
  136. };
  137. Collections.sort(al, heightFirst);
  138. }
  139. else
  140. {
  141. Comparator<Person> heightFirst = (n0, n1) ->
  142. {
  143. if( !(n1.height == n0.height))
  144. {
  145. return n1.height- n0.height;
  146. }
  147. else if ( !(n1.last.equalsIgnoreCase(n0.last)))
  148. {
  149. return n1.last.compareToIgnoreCase(n0.last);
  150. }
  151. return n1.first.compareToIgnoreCase(n0.first);
  152. };
  153. Collections.sort(al, heightFirst);
  154. }
  155. makeTable();
  156. });
  157. JMenuItem weight = new JMenuItem("Weight");
  158. sort.add(weight);
  159. weight.addActionListener( e -> {
  160. if(ascending)
  161. {
  162. Comparator<Person> weightFirst = (n0, n1) ->
  163. {
  164. if( !(n0.weight == n1.weight))
  165. {
  166. return n0.weight- n1.weight;
  167. }
  168. else if ( !(n0.last.equalsIgnoreCase(n1.last)))
  169. {
  170. return n0.last.compareToIgnoreCase(n1.last);
  171. }
  172. return n0.first.compareToIgnoreCase(n1.first);
  173. };
  174. Collections.sort(al, weightFirst);
  175. }
  176. else
  177. {
  178. Comparator<Person> weightFirst = (n0, n1) ->
  179. {
  180. if( !(n1.weight == n0.weight))
  181. {
  182. return n1.weight- n0.weight;
  183. }
  184. else if ( !(n1.last.equalsIgnoreCase(n0.last)))
  185. {
  186. return n1.last.compareToIgnoreCase(n0.last);
  187. }
  188. return n1.first.compareToIgnoreCase(n0.first);
  189. };
  190. Collections.sort(al, weightFirst);
  191. }
  192. makeTable();
  193. });
  194. JMenuItem bmi = new JMenuItem("BMI");
  195. sort.add(bmi);
  196. bmi.addActionListener( e -> {
  197. if(ascending)
  198. {
  199. Comparator<Person> weightFirst = (n0, n1) ->
  200. {
  201. if( !(n0.bmi == n1.bmi))
  202. {
  203. return n0.bmi- n1.bmi;
  204. }
  205. else if ( !(n0.last.equalsIgnoreCase(n1.last)))
  206. {
  207. return n0.last.compareToIgnoreCase(n1.last);
  208. }
  209. return n0.first.compareToIgnoreCase(n1.first);
  210. };
  211. Collections.sort(al, weightFirst);
  212. }
  213. else
  214. {
  215. Comparator<Person> weightFirst = (n0, n1) ->
  216. {
  217. if( !(n1.bmi == n0.bmi))
  218. {
  219. return n1.bmi- n0.bmi;
  220. }
  221. else if ( !(n1.last.equalsIgnoreCase(n0.last)))
  222. {
  223. return n1.last.compareToIgnoreCase(n0.last);
  224. }
  225. return n1.first.compareToIgnoreCase(n0.first);
  226. };
  227. Collections.sort(al, weightFirst);
  228. }
  229. makeTable();
  230. });
  231. JMenuItem way = new JMenuItem("Ascending");
  232. mbar.add(way);
  233. way.addActionListener( e -> {
  234. ascending = !ascending;
  235. if(ascending)
  236. {
  237. way.setText("Ascending");
  238. }
  239. else {
  240. way.setText("Descending");
  241. }
  242. });
  243. JMenuItem add = new JMenuItem("Add");
  244. mbar.add(add);
  245. add.addActionListener( e -> {
  246. JFrame f = new JFrame();
  247. JPanel p = new JPanel(new BorderLayout(5,5));
  248. JPanel labels = new JPanel(new GridLayout(0,1,2,4));
  249. labels.add(new JLabel("Last Name", SwingConstants.RIGHT));
  250. labels.add(new JLabel("First Name", SwingConstants.RIGHT));
  251. labels.add(new JLabel("Height", SwingConstants.RIGHT));
  252. labels.add(new JLabel("Weight", SwingConstants.RIGHT));
  253. p.add(labels, BorderLayout.WEST);
  254. JPanel controls = new JPanel(new GridLayout(0,1,2,2));
  255. JTextField lastName = new JTextField();
  256. controls.add(lastName);
  257. JTextField firstName = new JTextField();
  258. controls.add(firstName);
  259. JTextField newHeight = new JTextField();
  260. controls.add(newHeight);
  261. JTextField newWeight = new JTextField();
  262. controls.add(newWeight);
  263. p.add(controls, BorderLayout.CENTER);
  264. f.add(p);
  265. f.setSize(500,500);
  266. f.setVisible(true);
  267. int returnVal = JOptionPane.showConfirmDialog(
  268. f, p, "ADD", JOptionPane.YES_NO_OPTION);
  269. if (returnVal == JOptionPane.OK_OPTION) {
  270. f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
  271. String lastN = lastName.getText();
  272. String firstN = firstName.getText();
  273. int nHeight = Integer.parseInt(newHeight.getText());
  274. int nWeight = Integer.parseInt(newWeight.getText());
  275. al.add(new Person(lastN, firstN, nHeight, nWeight));
  276. model.addRow(new Object[]{null, null, null, null, null});
  277. makeTable();
  278. }
  279. if(returnVal == JOptionPane.NO_OPTION){
  280. f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
  281. }
  282. if(returnVal == JOptionPane.CLOSED_OPTION){
  283. f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
  284. }
  285. });
  286. JMenuItem remove = new JMenuItem("Remove");
  287. mbar.add(remove);
  288. remove.addActionListener( e -> {
  289. int k = 0;
  290. int[] rows = table.getSelectedRows();
  291. TableModel tm= table.getModel();
  292. while(rows.length>0)
  293. {
  294. while(k<rows.length)
  295. {
  296. al.remove(table.getSelectedRow() + k);
  297. k++;
  298. }
  299. ((DefaultTableModel)tm).removeRow(table.convertRowIndexToModel(rows[0]));
  300.  
  301. rows = table.getSelectedRows();
  302. }
  303. table.clearSelection();
  304. });
  305. }
  306. public void makeTable()
  307. {
  308. int k = 0;
  309. for(Person p:al)
  310. {
  311. table.setValueAt(p.getLast(),k,0);
  312. table.setValueAt(p.getFirst(),k,1);
  313. table.setValueAt(p.getHeight(),k,2);
  314. table.setValueAt(p.getWeight(),k,3);
  315. table.setValueAt(p.getBMI(),k,4);
  316. k++;
  317. }
  318. }
  319. public static void main(String[] args) {
  320. BMICalculator bc = new BMICalculator();
  321. javax.swing.SwingUtilities.invokeLater(bc);
  322. }
  323. public class Person implements Comparable<Person>
  324. {
  325. private String last, first;
  326. private int height, weight, bmi;
  327. public Person(String _l, String _f, int _h, int _w)
  328. {
  329. last = _l;
  330. first = _f;
  331. height = _h;
  332. weight = _w;
  333. bmi = (_w*703)/(_h*_h);
  334. }
  335. public int compareTo(Person that)
  336. {
  337. if( !(last.equalsIgnoreCase(that.last)))
  338. {
  339. return last.compareToIgnoreCase(that.last);
  340. }
  341. return first.compareToIgnoreCase(that.first);
  342. }
  343. public boolean equals(Object o)
  344. {
  345. if( !(o instanceof Person))
  346. {
  347. return false;
  348. }
  349. Person that = (Person) o;
  350. return that.first.equalsIgnoreCase(first)
  351. && that.last.equalsIgnoreCase(last);
  352. }
  353. public String getLast()
  354. {
  355. return last;
  356. }
  357. public String getFirst()
  358. {
  359. return first;
  360. }
  361. public int getHeight()
  362. {
  363. return height;
  364. }
  365. public int getWeight()
  366. {
  367. return weight;
  368. }
  369. public int getBMI()
  370. {
  371. return bmi;
  372. }
  373. }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement