-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookLibrary.java
More file actions
164 lines (128 loc) · 4.47 KB
/
Copy pathBookLibrary.java
File metadata and controls
164 lines (128 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
/**
* Te-Feng (Dylan) Pan
* CS 162J Lab 6B : Book Library
* This class print information about selected book(s).
*/
public class BookLibrary extends JFrame
{
private JList bookList;
private JTextArea selectedBookList;
private JButton button;
private JPanel bookPanel;
private JPanel selectionPanel;
private JPanel buttonPanel;
private Book[] books;
private final int WINDOW_HEIGHT = 600;
private final int WINDOW_WIDTH = 500;
public BookLibrary() throws Exception
{
super("Welcome to the Eugene Library!");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildBookPanel();
buildSelectedBookPanel();
buildButtonPanel();
add(bookPanel, BorderLayout.NORTH);
add(selectionPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
private void buildButtonPanel()
{
// Create a panel to hold the button.
buttonPanel = new JPanel();
// Create the button.
button = new JButton("Get Book Info");
// Add an action listener to the button.
button.addActionListener(new ButtonListener());
// Add the button to the panel.
buttonPanel.add(button);
}
private void buildSelectedBookPanel()
{
// Create a panel to hold the list.
selectionPanel = new JPanel();
// Create the list.
selectedBookList = new JTextArea(25, 30);
// Add the list to a scroll pane.
JScrollPane selectedBookScrollPane =
new JScrollPane(selectedBookList);
// Add the scroll pane to the panel.
selectionPanel.add(selectedBookScrollPane);
}
public static Book[] getBinaryFile() throws Exception
{
final int ITEMS = 5; // Number of items
// Create the stream objects to data files of Books objects.
FileInputStream inStream = new FileInputStream("Books.dat");
ObjectInputStream bookInputFile = new ObjectInputStream(inStream);
// Create an array to hold objects.
Book[] items = new Book[ITEMS];
// Read the serialized objects from the file.
for (int i = 0; i < items.length; i++)
{
items[i] = (Book) bookInputFile.readObject();
}
// Close the file.
bookInputFile.close();
return items;
}
public static String[] getTitles(Book[] books)
{
String[] titles = new String[books.length];
for(int i = 0; i < books.length; i++)
{
titles[i] = books[i].getTitle();
}
return titles;
}
private void buildBookPanel() throws Exception
{
books = getBinaryFile();
String[] bookTitles = getTitles(books);
// Create a panel to hold the list.
bookPanel = new JPanel();
// Create the list.
bookList = new JList(bookTitles);
bookList.setFixedCellWidth(300);
// Set the list to multiple interval selection mode.
bookList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Set the number of visible rows to 5.
bookList.setVisibleRowCount(5);
// Add the list to a scroll pane.
JScrollPane bookListScrollPane =
new JScrollPane(bookList);
// Add the scroll pane to the panel.
bookPanel.add(bookListScrollPane);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
String end = selectedBookList.getText();
if (bookList.getSelectedValue() == null)
selectedBookList.replaceRange("No items selected", 0, end.length());
else
{
// Get the items that were selected.
int[] selections = bookList.getSelectedIndices();
selectedBookList.replaceRange(books[selections[0]].toString() + "\n\n", 0, end.length());
for (int i = 1; i < selections.length; i++)
{
selectedBookList.append(books[selections[i]].toString() + "\n\n");
}
}
}
}
public static void main(String[] args) throws Exception
{
new BookLibrary();
}
}