SlideShare a Scribd company logo
1 of 9
Lab 6
CSE 115
Spring 2013
Victoria Minorczyk
vaminorc@buffalo.edu
Davis 302 | Thursday 1-1:50pm
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.
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!
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);
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!
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…
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();
}
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…
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 );

More Related Content

Viewers also liked (13)

The acropolis
The acropolisThe acropolis
The acropolis
 
Hinduism
HinduismHinduism
Hinduism
 
A. Prestininzi - NHAZCA Open Day
A. Prestininzi - NHAZCA Open DayA. Prestininzi - NHAZCA Open Day
A. Prestininzi - NHAZCA Open Day
 
Cse 241
Cse 241Cse 241
Cse 241
 
Crm guide
Crm guideCrm guide
Crm guide
 
F. Bozzano - NHAZCA Open Day
F. Bozzano - NHAZCA Open DayF. Bozzano - NHAZCA Open Day
F. Bozzano - NHAZCA Open Day
 
χελιδονισματα- the swallow song
χελιδονισματα- the swallow songχελιδονισματα- the swallow song
χελιδονισματα- the swallow song
 
Pohatu2013 meettheteacher
Pohatu2013 meettheteacherPohatu2013 meettheteacher
Pohatu2013 meettheteacher
 
Hinduismm
HinduismmHinduismm
Hinduismm
 
Lab 7 – 8 (Part 1)
Lab 7 – 8 (Part 1)Lab 7 – 8 (Part 1)
Lab 7 – 8 (Part 1)
 
Audio Branding Presentation
Audio Branding PresentationAudio Branding Presentation
Audio Branding Presentation
 
D. Mosna - NHAZCA Open Day
D. Mosna - NHAZCA Open DayD. Mosna - NHAZCA Open Day
D. Mosna - NHAZCA Open Day
 
Car Tow
Car TowCar Tow
Car Tow
 

Similar to Lab 6

Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
arpitaeron555
 
3.2 stacks and arrays
3.2   stacks and arrays3.2   stacks and arrays
3.2 stacks and arrays
allenbailey
 
Java collections
Java collectionsJava collections
Java collections
padmad2291
 
Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
PrabhaK22
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
Krishna Sujeer
 

Similar to Lab 6 (20)

Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
3.2 stacks and arrays
3.2   stacks and arrays3.2   stacks and arrays
3.2 stacks and arrays
 
Fewd week6 slides
Fewd week6 slidesFewd week6 slides
Fewd week6 slides
 
A04
A04A04
A04
 
Java util
Java utilJava util
Java util
 
Java collections
Java collectionsJava collections
Java collections
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
 
ECET 370 Effective Communication/tutorialrank.com
 ECET 370 Effective Communication/tutorialrank.com ECET 370 Effective Communication/tutorialrank.com
ECET 370 Effective Communication/tutorialrank.com
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
ECET 370 Redefined Education--ecet370.com
ECET 370 Redefined Education--ecet370.comECET 370 Redefined Education--ecet370.com
ECET 370 Redefined Education--ecet370.com
 
ECET 370 Education Planning--ecet370.com
 ECET 370 Education Planning--ecet370.com ECET 370 Education Planning--ecet370.com
ECET 370 Education Planning--ecet370.com
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Ecet 370 week 2 lab 2
Ecet 370 week 2 lab 2Ecet 370 week 2 lab 2
Ecet 370 week 2 lab 2
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Lab 6

  • 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 );