CM10228 / Programming Ib:   Lecture 14


Components, Layouts, Panes, Listeners etc.


 -I. Review

  1. Remember the kinds of things we saw when building the window yesterday...
    1. Frame
    2. Widgets (small components)
    3. Layout
    4. Listener (in Java, basically the C in MVC)
    5. Remind students what these are for!  And make sure they know how important the lecture was on Tuesday.
  2. Now we're going to look at how to look up and find stuff that you can use to build into your system (except not about listeners if you got them in Tuesday's lecture).

I.  Listeners

  1. ActionListener -- actionPerformed (button press, slider moved, etc)
  2. FocusListener -- focusGained (FocusEvent e), focusLost.
    1. Focus is basically where you can type, as in "focus of attention".
    2. You can also make a widget request the focus (like Google does in its search window) JComponent.requestFocus()
  3. MouseListener (MouseEvent e)
  4.  void mouseClicked(MouseEvent e)
              Invoked when the mouse button has been clicked (pressed and released) on a component.
     void mouseEntered(MouseEvent e)
              Invoked when the mouse enters a component.
     void mouseExited(MouseEvent e)
              Invoked when the mouse exits a component.
     void mousePressed(MouseEvent e)
              Invoked when a mouse button has been pressed on a component.
     void mouseReleased(MouseEvent e)
              Invoked when a mouse button has been released on a component.
    1. MouseMotionListener
       void mouseDragged(MouseEvent e)
                Invoked when a mouse button is pressed on a component and then dragged.
       void mouseMoved(MouseEvent e)
                Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
  5. Also table, menu & hyperlink listeners, many others.


II. Containers &Panes

  1. This lecture is mostly out of the Java Tutorial, because that way I got all the running components for free.
    1. This may seem like the laziest lecture of all time, but honest, I spent a lot of time looking for better solutions & then deciding what to show you after I decided how to show it...
    2. The tutorial for swing starts on http://docs.oracle.com/javase/tutorial/uiswing/
  2. Start with components --> visual introduction
    1. http://docs.oracle.com/javase/tutorial/ui/features/components.html
  3. Top level containers
    1. skip applets, get them Tuesday
    2. Frame --
      1. already saw basics previous lecture,
      2. but show first & last set of methods
        1. diff exits, icon -- see notes too
    3. dialogs --
      1. remind them they've seen one of the default ones at the top if they ran the code from the last lecture.
      2. show them everything on the run dialog demo, then show them the code for a few things.
        1. code is a couple screens past the demo, just before screen shots, or through link.
      3. we'll be doing radio buttons etc. later, see how they work.
      4. Be sure to show modal stuff, leads to glass pane below...
  4. show & talk about the different panes.
    1. skip down to "special purpose containers"
    2. go into layered panes, show demo
    3. show pane drawing,
      1. explain glass pane, but don't run demo, it's lame
  5. go back up to GP containers
  6. skip panels until talk about layouts (below)
  7. show scroll pane -- show demo
    1. show basic call to scroll pane at top - arg is another pane
    2. go down to show how scroll panes are themselves made of other components -- you could really build your own.
      1. track, knob, buttons -- important key words
    3. might want to use scroll panes for CW2.
  8. show split pane demo
  9. offer tabbed pane demo, prob not
    1. cool thing is the dragging

III. Components

  1. button
    1. show basic demo,
    2. explain disabled / enabled concept
    3. show
      1. mnemonic
      2. tool tips
    4. can't always use glass pane! often some options make sense in some circumstances.
    5. e.g. can't edit or graph file before you've chosen it.
    6. show radio buttons / single option
    7. show check buttons / multiple options.
    8. emphasize these are universals
  2. show list
    1. maybe show demos (time?)
    2. show the selection model part!
  3. show combo box
    1. like radio buttons but takes less space
    2. editable one doesn't seem to work
  4. show slider
    1. not demo, just code is enough, right after first demo/picture
  5. spinner
    1. demo is kind of cool if time -- stops you from giving wrong answer, advances from right one
    2. for sure go down & show the stuff about models (remember model, view controller?)
  6. text field
    1. go in and show the different types of text fields
    2. when done, click on formatted & show the example formats quickly
  7. Wave at uneditable displays -- no time to really go in to it & it's obviousl
  8. wave at most of interactive displays, but go into text
    1. go down to the table of types of text components,
    2. point out text panes are darn useful for coursework
    3. show demo

IV. Layouts

  1. got to go way up to where you had clicked on "using swing components", then down two to layouts, visual guide
    1. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
  2. one they saw before was grid, probably the most boring
  3. most generally useful / quick is border
    1. need directions
    2. center expands to fit
  4. flow layout is really easy
  5. box layout is too,
    1. a lot like panels
    2. can make vertical boxes of horizontal panels, kind of like grid
  6. grid bag a bit more complicated, flexible
  7. spring layout for nice fancy things
    1. runnable demo is way down on 3rd one for some reason.

V. Summary

  1. talked about listeners quickly, then showed containers & panes, components, layouts
  2. You should play with them & look at the documentation!

page author: Joanna Bryson
22 March 2012