Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Arrays;
- import java.util.Scanner;
- public class BirdListGUI extends JFrame {
- private JPanel listPanel;
- private JPanel buttonPanel;
- private JTextArea birdList;
- private JLabel label;
- private JButton revButton;
- private JButton displayButton;
- private String stringList;
- private JCheckBox check;
- public BirdListGUI() {
- super("Bird List");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- // build panels
- buildListPanel();
- buildButtonPanel();
- // add panels
- add(listPanel, BorderLayout.WEST);
- add(buttonPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- public void buildListPanel() {
- listPanel = new JPanel();
- label = new JLabel("Birds");
- birdList = new JTextArea(20, 30);
- birdList.setEditable(false);
- listPanel.add(label);
- listPanel.add(birdList);
- }
- public void buildButtonPanel() {
- buttonPanel = new JPanel();
- revButton = new JButton("Reverse Order");
- revButton.addActionListener(new ButtonListener());
- displayButton = new JButton("Display");
- displayButton.addActionListener(new DisplayListener());
- check = new JCheckBox("Remove birds");
- buttonPanel.add(check);
- buttonPanel.add(revButton);
- buttonPanel.add(displayButton);
- }
- public class DisplayListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- BirdList list = new BirdList();
- try {
- Scanner input1 = new Scanner(new File("birds1.txt")); //Populate complete bird list
- while (input1.hasNext())
- list.insert(input1.nextLine());
- } catch (FileNotFoundException ex) { //Catch file not found exception
- System.out.println(ex.getMessage());
- }
- list.sort();
- String stringList = list.toString();
- birdList.setText(stringList);
- if (check.isSelected()) { //If selected, remove non-indigenous birds
- try {
- Scanner input2 = new Scanner(new File("birds2.txt"));
- while (input2.hasNext())
- list.remove(input2.nextLine());
- } catch (FileNotFoundException ex) {
- System.out.println(ex.getMessage());
- }
- list.sort();
- stringList = list.toString();
- birdList.setText(stringList);
- }
- }
- }
- public class ButtonListener implements ActionListener {
- boolean sorted = true;
- public void actionPerformed(ActionEvent e) {
- BirdList list = new BirdList();
- if (check.isSelected()) {
- try {
- Scanner input1 = new Scanner(new File("birds1.txt"));
- Scanner input2 = new Scanner(new File("birds2.txt"));
- while (input1.hasNext())
- list.insert(input1.nextLine());
- while (input2.hasNext())
- list.remove(input2.nextLine());
- } catch (FileNotFoundException ex) {
- System.out.println(ex.getMessage());
- }
- String stringList = list.toString();
- birdList.setText(stringList);
- }
- if (!check.isSelected()) {
- try {
- Scanner input1 = new Scanner(new File("birds1.txt"));
- while (input1.hasNext())
- list.insert(input1.nextLine());
- } catch (FileNotFoundException ex) {
- System.out.println(ex.getMessage());
- }
- String stringList = list.toString();
- birdList.setText(stringList);
- }
- if (sorted) { //When Reverse button is pressed, list will alternate from sorted ascending and descending
- list.sortRev();
- stringList = list.toString();
- birdList.setText(stringList);
- sorted = false;
- } else if (!sorted) {
- list.sort();
- stringList = list.toString();
- birdList.setText(stringList);
- sorted = true;
- }
- }
- }
- public static void main(String[] args) {
- new BirdListGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement