More Interfaces and JList - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

More Interfaces and JList

Description:

... collection to have the methods JList needs to show and select elements ... TODO Auto-generated method stub. 3-14. Show collection in a GUI. import java.awt. ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 17
Provided by: rickmercer
Category:
Tags: interfaces | jlist | more

less

Transcript and Presenter's Notes

Title: More Interfaces and JList


1
More Interfacesand JList
  • Rick Mercer

2
Displaying an Image
  • Use JOptionPane to display message
  • JOptionPane.showMessageDialog(null,
  • "Hello, World!")
  • Note icon to the left (i for information

3
ImageIcon implements Icon
  • JOptionPane.showMessageDialog(null, "Hello,
    World!", "Message",
  • JOptionPane.INFORMATION_MESSAGE, new
    ImageIcon("home34.jpg"))

4
Here is the method heading
  • public static void showMessageDialog(Component par
    entComponent,
  • Object message,
  • String title,
  • int messageType,
  • Icon icon)
  • The 5th parameter is Icon, an interface
  • The argument must be an instance of a class that
    implements the Icon interface
  • Such as ImageIcon

5
Demo javax.swing.Timer
  • // Use a javax.swing.Timer to show seconds tick
    by
  • import java.awt.event.ActionEvent
  • import java.awt.event.ActionListener
  • import javax.swing.JFrame
  • import javax.swing.JLabel
  • import javax.swing.Timer
  • public class TimerDemo extends JFrame
  • public static void main(String args)
  • JFrame window new TimerDemo()
  • window.setVisible(true)
  • private Timer timer
  • private int currentSecond
  • private JLabel secondsGoneBy
  • new JLabel("", JLabel.CENTER)

6
A listener to show seconds tick
  • public TimerDemo()
  • setSize(50, 80)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • getContentPane().add(secondsGoneBy)
  • // Construct a timer with the delay and
    listener

7
Other Events and Components?
  • Yes indeed
  • There are many more graphical components
  • There are many more types of events
  • There are other layout managers
  • We can draw and do animations?
  • But what else do we need for Jukebox?
  • A JList to show Songs that can be selected with a
    button click

8
(No Transcript)
9
JList Objects
  • JList provides a view of objects in a graphical
    manner Users can select single or multiple
    selections
  • Can determine the selected cell or set any cell
    to be "selected"
  • // Returns the first selected index returns
    -1
  • // if there is no selected item.
  • public int getSelectedIndex()
  • // Selects a single cell in this JList //
    and highlights the element in that cell
  • public void setSelectedIndex(int index)

10
Adapt with ListModel
  • JLists store a list that must implement the
    ListModel interface
  • You need to adapt your collection to have the
    methods JList needs to show and select elements
  • Constructor and setModel of JList require an
    object from a class that implements ListModel
  • // Construct a view with any class that //
    implements ListModel
  • public JList(ListModel dataModel)
  • // Or later set the model to a ListModel
  • public void setModel(ListModel dataMmodel)

11
First a test
  • import static org.junit.Assert.
  • import org.junit.Test
  • public class AccountCollectionTest
  • _at_Test
  • public void testGetElementAtAndGetSize()
  • // Plan to start with four section leaders
  • AccountCollection allAccounts new
    AccountCollection()
  • assertEquals(4, allAccounts.getSize())
  • // I would like to possible add others after
    initialization
  • allAccounts.addBankAccount(new
    BankAccount("Another", 20.00))
  • allAccounts.addBankAccount(new
    BankAccount("Last one", 30.00))
  • assertEquals(6, allAccounts.getSize())
  • // These are NOT in order like your
    SongCollection must be
  • BankAccount currentAccount
    allAccounts.getElementAt(0)
  • assertEquals("Jessica", allAccounts.getElementA
    t(0).getID())
  • assertEquals("Andreea", allAccounts.getElementA
    t(1).getID())

12
Have your collection implement ListModel
  • import java.util.ArrayList
  • import javax.swing.ListModel
  • import javax.swing.event.ListDataListener
  • public class AccountCollection implements
    ListModel
  • private ArrayListltBankAccountgt list
  • // Wierd initialization. Always begin with
    these four
  • public AccountCollection()
  • list new ArrayListltBankAccountgt()
  • list.add(new BankAccount("Jessica", 100))
  • list.add(new BankAccount("Andreea", 100))
  • list.add(new BankAccount("Mark", 100))
  • list.add(new BankAccount("Mike", 100))
  • public void addBankAccount(BankAccount a)
  • list.add(a)

13
Implement Two methods only
  • public int getSize()
  • return list.size()
  • public BankAccount getElementAt(int index)
  • return list.get(index)
  • public void addListDataListener(ListDataListener
    l)
  • // TODO Auto-generated method stub
  • public void removeListDataListener(ListDataListe
    ner l)
  • // TODO Auto-generated method stub

14
Show collection in a GUI
  • import java.awt.
  • import javax.swing.
  • public class ShowListInAGui extends JFrame
  • public static void main(String args)
  • ShowListInAGui aWindow new
    ShowListInAGui()
  • aWindow.setVisible(true)
  • // The model for this application
  • private AccountCollection model
  • // Show all ListModel elements in a Graphical
    manner
  • private JList view
  • // A Button that will have a listener
    registered to it
  • private JButton theButton new JButton("Click
    me")

15
No Events Yet
  • public ShowListInAGui()
  • // Initialize the model
  • model new AccountCollection()
  • model.addBankAccount(new BankAccount("Rick",
    0.00))
  • model.addBankAccount(new BankAccount("Chris",
    567.89))
  • view new JList(model)
  • // Set up the GUI
  • setLocation(100, 100)
  • setSize(100, 300)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • Container contentPane getContentPane()
  • contentPane.add(view, BorderLayout.CENTER)
  • contentPane.add(theButton, BorderLayout.SOUTH)

16
In teams of 2 or 3
  • Complete code in handout
Write a Comment
User Comments (0)
About PowerShow.com