Title: CS3006N Week 9 more GUI Programming
1CS3006N Week 9more GUI Programming
2Warning
- Do not look at this week until you have
understood last weeks lecture - This week will continue with the example from
last week so grab the final code from last weeks
lecture I.e. the frame
3The List
- Lists allow you to select from a fixed choice of
items. - llnew List()
- ll.setBounds(10,100,100,100)
- ll.add("choice 1")
- ll.add("choice 2")
- ll.add("choice 3")
- f.add(ll)
X, Y, (within frame) width, Height
Add the possible choices to the list
Add list to the frame
4The List
- You can find out the text at any position in the
list using getItem(position) - You can find out how man items are in the list
using getItemCount() - You can find out which item is selected using
getSelectedIndex() which returns the position of
the selected item - or
- getSelectedItem() which returns the text of the
selected item
5The List
- You can force an entry to be selected by using
select(position) or select(item text) - You can delete an entry using remove(position) or
remove(item text) - You can delete all the items using removeAll()
6The List
- Like all the GUI components examined last week
lists have - setVisible(true or false)
- setEnabled(true or false)
7List Events
- Lists can generate many events
- ActionPerformed if an item is double clicked on
- itemStateChanged if an item is selected or
deselected - To handle ActionPerformed events requires your
class to implement an ActionListener - To handle itemStateChanged events requires your
class implements an ItemListener
8ActionListener
- public class main implements ActionListener
-
-
- public void actionPerformed(ActionEvent e)
-
- if (e.getSource()ll)
-
- l.setText( ouch )
-
-
-
- ll.addActionListener(this)
Handle the button press
Change the label to say ouch!
This code is added to the main method to attach
the action listener to the List
9ItemListener
public class main implements ItemListener publ
ic void itemStateChanged(ItemEvent e)
if (e.getStateChange()e.SELECTED )
l.setText("Selected
"e.getItem())
if (e.getStateChange()e.DESELECTED )
l.setText("DESelected"e.getItem())
ll.addItemListener(this)
Handle item is selected
Handle item is deselected
10Choice
- Choice is related to List in that it displays a
set of possible options and generates
itemStateChanged events - Adding items, getting the selected items etc are
the same as List - Choice does not generate
- ActionPerformed events
11Choice
- cnew Choice()
- c.setBounds(10,100,100,100)
- c.add("choice 1")
- c.add("choice 2")
- c.add("choice 3")
- c.addItemListener(this)
- f.add(c)
12Choice
- Since both List and Choice generate
itemStateChanged events inside the
itemStateChaged method you will need to check who
originated the event. - public void itemStateChanged(ItemEvent e)
- if (e.getSource()c)
- if (e.getStateChange()e.SELECTED )
- l.setText("Selected
"e.getItem()) -
- if (e.getStateChange()e.DESELECTED
) - l.setText("DESelected
"e.getItem()) -
-
-
13Checkbox
- A checkbox asks a simple yes or no question
- cbnew Checkbox()
- cb.setBounds(10,100,100,100)
- cb.setLabel("Pass IM257?")
- cb.addItemListener(this)
- It can also generate an
- itemStateChanged event
14Checkbox
- You can set the checkbox to on or off using
setState(true or false) - You can find out of the checkbox is on or off
using getState() - Checkbox supports getLabel(), setLabel(new
label), setVisible(true or false) and
setEnabled(true or false)
15CheckboxGroup
- The CheckboxGroup only allows one of the grouped
checkboxes to be on at any one time - cgnew CheckboxGroup()
- cb1new Checkbox("Fail IM257",cg,false)
- cb1.setBounds(10,100,100,20)
- cb2new Checkbox("Pass IM257",cg,false)
- cb2.setBounds(10,120,100,20)
- cb3new Checkbox("Avoid IM257",cg,false)
- cb3.setBounds(10,140,100,20)
- f.add(l)
- f.add(cb1)
- f.add(cb2)
- f.add(cb3)
Construct the manager
Manager becomes a parameter to the constructor
16CheckboxGroup
- You can find out the selected checkbox by using
getSelectedCheckbox() - You can set the selected checkbox using
setSelectedCheckbox(index)
17CheckboxGroup
- CheckboxGroup has not events of its own so you
will still need to attach listeners to the
individual Checkboxes - CheckboxGroup does not have
- setVisible(true or false)
- setEnabled(true or false)
- Again you need to talk to the individual
Checkboxes
18Color without a u!
- All the components we have examined have methods
to set the background and foreground colours
using setBackground(Color) and setForeground(Color
) - The Color object allows various ways of
specifying a colour - There are a set of predefined colours Color.blue
etc - You can specify the RGB values for the colour
- Color cnew Color(10,10,10)
Red, green,blue
19Further Reading
- Look at www.java.sun.com
- Look for the Java tutorial and look at AWT
programming - Look for the online documentation for the
java.awt package