Advertisement
Nick-O-Rama

newBirdListGUI

May 6th, 2015
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3.  
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ItemEvent;
  8. import java.awt.event.ItemListener;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.util.Arrays;
  12. import java.util.Scanner;
  13.  
  14. public class BirdListGUI extends JFrame {
  15.  
  16.     private JPanel listPanel;
  17.     private JPanel buttonPanel;
  18.     private JTextArea birdList;
  19.     private JLabel label;
  20.     private JButton revButton;
  21.     private JButton displayButton;
  22.     private String stringList;
  23.     private JCheckBox check;
  24.  
  25.     public BirdListGUI() {
  26.         super("Bird List");
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         setLayout(new BorderLayout());
  29.  
  30.         // build panels
  31.         buildListPanel();
  32.         buildButtonPanel();
  33.  
  34.         // add panels
  35.         add(listPanel, BorderLayout.WEST);
  36.         add(buttonPanel, BorderLayout.SOUTH);
  37.         pack();
  38.         setVisible(true);
  39.     }
  40.  
  41.     public void buildListPanel() {
  42.         listPanel = new JPanel();
  43.         label = new JLabel("Birds");
  44.  
  45.         birdList = new JTextArea(20, 30);
  46.         birdList.setEditable(false);
  47.         listPanel.add(label);
  48.         listPanel.add(birdList);
  49.     }
  50.  
  51.     public void buildButtonPanel() {
  52.         buttonPanel = new JPanel();
  53.         revButton = new JButton("Reverse Order");
  54.         revButton.addActionListener(new ButtonListener());
  55.         displayButton = new JButton("Display");
  56.         displayButton.addActionListener(new DisplayListener());
  57.         check = new JCheckBox("Remove birds");
  58.         buttonPanel.add(check);
  59.         buttonPanel.add(revButton);
  60.         buttonPanel.add(displayButton);
  61.     }
  62.  
  63.     public class DisplayListener implements ActionListener {
  64.  
  65.         @Override
  66.         public void actionPerformed(ActionEvent arg0) {
  67.             BirdList list = new BirdList();
  68.             try {
  69.                 Scanner input1 = new Scanner(new File("birds1.txt")); //Populate complete bird list
  70.                 while (input1.hasNext())
  71.                     list.insert(input1.nextLine());
  72.             } catch (FileNotFoundException ex) { //Catch file not found exception
  73.                 System.out.println(ex.getMessage());
  74.             }
  75.             list.sort();
  76.             String stringList = list.toString();
  77.             birdList.setText(stringList);
  78.             if (check.isSelected()) { //If selected, remove non-indigenous birds
  79.                 try {
  80.                     Scanner input2 = new Scanner(new File("birds2.txt"));
  81.                     while (input2.hasNext())
  82.                         list.remove(input2.nextLine());
  83.                 } catch (FileNotFoundException ex) {
  84.                     System.out.println(ex.getMessage());
  85.                 }
  86.                 list.sort();
  87.                 stringList = list.toString();
  88.                 birdList.setText(stringList);
  89.             }
  90.         }
  91.     }
  92.  
  93.     public class ButtonListener implements ActionListener {
  94.         boolean sorted = true;
  95.  
  96.         public void actionPerformed(ActionEvent e) {
  97.             BirdList list = new BirdList();
  98.             if (check.isSelected()) {
  99.                 try {
  100.                     Scanner input1 = new Scanner(new File("birds1.txt"));
  101.                     Scanner input2 = new Scanner(new File("birds2.txt"));
  102.                     while (input1.hasNext())
  103.                         list.insert(input1.nextLine());
  104.                     while (input2.hasNext())
  105.                         list.remove(input2.nextLine());
  106.                 } catch (FileNotFoundException ex) {
  107.                     System.out.println(ex.getMessage());
  108.                 }
  109.                 String stringList = list.toString();
  110.                 birdList.setText(stringList);
  111.             }
  112.             if (!check.isSelected()) {
  113.                 try {
  114.                     Scanner input1 = new Scanner(new File("birds1.txt"));
  115.                     while (input1.hasNext())
  116.                         list.insert(input1.nextLine());
  117.                 } catch (FileNotFoundException ex) {
  118.                     System.out.println(ex.getMessage());
  119.                 }
  120.                 String stringList = list.toString();
  121.                 birdList.setText(stringList);
  122.             }
  123.  
  124.             if (sorted) { //When Reverse button is pressed, list will alternate from sorted ascending and descending
  125.                 list.sortRev();
  126.                 stringList = list.toString();
  127.                 birdList.setText(stringList);
  128.                 sorted = false;
  129.             } else if (!sorted) {
  130.                 list.sort();
  131.                 stringList = list.toString();
  132.                 birdList.setText(stringList);
  133.                 sorted = true;
  134.             }
  135.         }
  136.  
  137.     }
  138.  
  139.     public static void main(String[] args) {
  140.         new BirdListGUI();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement