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.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class BirdListGUI extends JFrame{
- private JPanel listPanel;
- private JPanel buttonPanel;
- private JList birdList;
- private JLabel label;
- private JButton revButton;
- 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 list = new BirdList();
- try {
- Scanner input1 = new Scanner(new File("birds1.txt"));
- Scanner input2 = new Scanner(new File("birds2.txt"));
- while (input1.hasNext()) //Populate list
- list.insert(input1.nextLine());
- while (input2.hasNext()) //Remove non-indigenous birds
- list.remove(input2.nextLine());
- }
- catch(FileNotFoundException e) {
- System.out.println(e.getMessage());
- }
- list.sort();
- String stringList[] = new String[list.getSize()];
- for (int i = 0; i < stringList.length; i++)
- stringList[i] = list.printSingle(i);
- birdList = new JList(stringList);
- birdList.setVisibleRowCount(6);
- JScrollPane birdListScrollPane = new JScrollPane(birdList);
- listPanel.add(label);
- listPanel.add(birdListScrollPane);
- }
- public void buildButtonPanel() {
- buttonPanel = new JPanel();
- revButton = new JButton("Reverse Order");
- revButton.addActionListener(new ButtonListener());
- buttonPanel.add(revButton);
- }
- class ButtonListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- BirdList list = new BirdList();
- try {
- Scanner input1 = new Scanner(new File("birds1.txt"));
- Scanner input2 = new Scanner(new File("birds2.txt"));
- while (input1.hasNext()) //Populate list
- list.insert(input1.nextLine());
- while (input2.hasNext()) //Remove non-indigenous birds
- list.remove(input2.nextLine());
- }
- catch(FileNotFoundException ex) {
- System.out.println(ex.getMessage());
- }
- list.sortRev();
- String stringList[] = new String[list.getSize()];
- for (int i = 0; i < stringList.length; i++)
- stringList[i] = list.printSingle(i);
- birdList.setListData(stringList);
- }
- }
- public static void main(String[] args) {
- new BirdListGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement