1. Lab 6
CSE 115
Spring 2013
Victoria Minorczyk
vaminorc@buffalo.edu
Davis 302 | Thursday 1-1:50pm
2. Java Collections
• There are some classes in the Java Libraries which implement the interface Collection<E>.
• A collection can manage several references to the same type of object at once!
• For this lab, you will use the ArrayList<String> collection to store adverbs, nouns, ect…
• ArrayList<String> has a ‘get(int index)’ method, which returns the element at the
specified position in the list.
• Collections also has a static method named ‘shuffle’. It is a static method, so to invoke it,
you use the syntax <class name>.<method name>. For example
java.util.Collections.suffle(list). The parameter ‘list’ is the list to be shuffled. You will use
this method in your lab.
3. Layout Managers
• Java provides several classes (in the Java Libraries)
which implement the ‘LayoutManager’ interface.
• Remember, classes in the Java Libraries are classes
defined for you that you can use
• Graphical components (ie. Jbuttons, Jlabels…) can
provide information about their preferred size and
alignment, but a container’s (ie. JFrame’s) layout
manager has the ‘final say’ on the size and position
of ALL components within that container (ie.
JFrame).
• There are LOTS and LOTS of Layout Managers!
4. Layout Managers
A container will not follow a certain layout until:
-You create the container
-You create the layout (instantiate it)
-Set the layout of the container
For example:
JFrame f = new JFrame();
GridBagLayout g = new GridBagLayout();
f.setLayout(g);
5. BoxLayout Manager
GridBagLayout is a very complicated layout manager.
For this lab, you use a much simpler layout called a
‘BoxLayout’ Manager.
With this Layout, you can stacks components in a
container OR place them in a row
For this lab, you will want to place them in a row. Keep
reading to find out how!
6. BoxLayout Manager
The box layout manager constructor has TWO
parameters. The first is the container you want to set the
layout of. The second parameter specifies where each
component will be added to container.
For this lab, Dr. Alphonce requires you add components
along the BoxLayout.X-AXIS
Lets look at the example on the next slide…
7. public class WindowWithBoxLayout {
public WindowWithBoxLayout(){
//create frame
JFrame f = new JFrame("BoxLayout Demo");
f.setVisible(true);
//create layout
BoxLayout layout = new BoxLayout(f.getContentPane(),BoxLayout.X_AXIS);
f.setLayout(layout); //Notice the arguments above ^ for BoxLayout
//create some buttons
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
//add buttons to frame
f.add(b1);
f.add(b2);
f.add(b3);
f.pack();
}
8. How to define EventHandlers!
Every class that is to be an “EventHandler” MUST
have BOTH of the following:
-implement a Listener (ie. ActionListener or Mouse
Listener)
-define the methods listed in that Listener Interface
*Remember, an interface specifies a list of method
signatures that a class is REQUIRED to have (even if
the method body is left blank!
For example…
9. public class EventHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
//code that should be executed when the button is
//pressed goes HERE!
}
}
*Remember EventHandlers MUST be added to your JButtons! Otherwise
your JButtons will NOT do ANYTHING!
For example:
JButon b = new JButton();
EventHandler listener = new EventHandler();
b.addActionListener( listener );