Advertisement
Nick-O-Rama

BirdListGUI

May 4th, 2015
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.util.Scanner;
  9.  
  10. public class BirdListGUI extends JFrame{
  11.    
  12.     private JPanel listPanel;
  13.     private JPanel buttonPanel;
  14.     private JList birdList;
  15.     private JLabel label;
  16.     private JButton revButton;
  17.    
  18.     public BirdListGUI() {
  19.         super("Bird List");
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         setLayout(new BorderLayout());
  22.        
  23.         //build panels
  24.         buildListPanel();
  25.         buildButtonPanel();
  26.        
  27.         //add panels
  28.         add(listPanel, BorderLayout.WEST);
  29.         add(buttonPanel, BorderLayout.SOUTH);
  30.         pack();
  31.         setVisible(true);
  32.     }
  33.    
  34.     public void buildListPanel() {
  35.         listPanel = new JPanel();
  36.         label = new JLabel("Birds");
  37.        
  38.         BirdList list = new BirdList();
  39.         try {
  40.             Scanner input1 = new Scanner(new File("birds1.txt"));
  41.             Scanner input2 = new Scanner(new File("birds2.txt"));
  42.             while (input1.hasNext()) //Populate list
  43.                 list.insert(input1.nextLine());
  44.             while (input2.hasNext()) //Remove non-indigenous birds
  45.                 list.remove(input2.nextLine());
  46.         }
  47.         catch(FileNotFoundException e) {
  48.             System.out.println(e.getMessage());
  49.         }
  50.         list.sort();
  51.         String stringList[] = new String[list.getSize()];
  52.         for (int i = 0; i < stringList.length; i++)
  53.             stringList[i] = list.printSingle(i);
  54.        
  55.         birdList = new JList(stringList);
  56.         birdList.setVisibleRowCount(6);
  57.         JScrollPane birdListScrollPane = new JScrollPane(birdList);
  58.         listPanel.add(label);
  59.         listPanel.add(birdListScrollPane);
  60.     }
  61.    
  62.     public void buildButtonPanel() {
  63.         buttonPanel = new JPanel();
  64.         revButton = new JButton("Reverse Order");
  65.         revButton.addActionListener(new ButtonListener());
  66.         buttonPanel.add(revButton);
  67.     }
  68.    
  69.     class ButtonListener implements ActionListener {
  70.  
  71.         @Override
  72.         public void actionPerformed(ActionEvent e) {
  73.             BirdList list = new BirdList();
  74.             try {
  75.                 Scanner input1 = new Scanner(new File("birds1.txt"));
  76.                 Scanner input2 = new Scanner(new File("birds2.txt"));
  77.                 while (input1.hasNext()) //Populate list
  78.                     list.insert(input1.nextLine());
  79.                 while (input2.hasNext()) //Remove non-indigenous birds
  80.                     list.remove(input2.nextLine());
  81.             }
  82.             catch(FileNotFoundException ex) {
  83.                 System.out.println(ex.getMessage());
  84.             }
  85.             list.sortRev();
  86.             String stringList[] = new String[list.getSize()];
  87.             for (int i = 0; i < stringList.length; i++)
  88.                 stringList[i] = list.printSingle(i);
  89.             birdList.setListData(stringList);
  90.         }
  91.        
  92.     }
  93.  
  94.     public static void main(String[] args) {
  95.         new BirdListGUI();
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement