Adding HTML to JButtons and JLabels. - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Adding HTML to JButtons and JLabels.

Description:

Clipboard: Cutting. When cutting, we are promising the ... Swing text components already support cut-and-paste to system clipboard. Often this is enough. ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 39
Provided by: Techs
Category:
Tags: html | adding | cut | jbuttons | jlabels

less

Transcript and Presenter's Notes

Title: Adding HTML to JButtons and JLabels.


1
Lecture 6
  • Adding HTML to JButtons and JLabels.
  • Getting the right cursor
  • Data Transfer
  • Cut and Paste, System Clipboard
  • Gestures
  • Drag and Drop
  • Drag n Drop with Unix
  • A desktop icon class

2
HTML-aware components
  • Can use HTML text mark-up with many Swing
    components now. Examples multi-line labels,
    more fonts, formatting and styles.
  • Warning bad HTML will cause exceptions.
    Feature still almost undocumented (Suns web
    page).
  • Believed to work with JLabel, JButton,
    JMenuItem, JMenu, JCheckBoxMenuItem,
    JRadioButtonMenuItem, JTabbedPane, JToolTip.
    JOptionPane too.

3
Uses
  • Can split large labels into several lines.
  • Documentation/Help options. Can make HTML docs,
    and also view them from within the program.
    Hyperlinks are displayed but not active.
  • Unfinished feature. You still have to insert
    your own line-breaks, and implement your own
    hypertext support.

4
Pointing devices
  • User normally thinks of pointer as hand-held
    tool, or even an extension of his/her body.
    Usually, but not always, controlled by mouse,
    which moves wherever user moves hand.
  • Used to allow user to specify a Location and a
    desired Action.
  • gestures such as drag, double-click.

5
The right cursor
  • Cursor refers to the on-screen image used to
    help user position pointer.
  • Cursor appearance should reflect pointer usage.
    This may change frequently.
  • Java provides 14 system-independent cursors. OS
    may provide more. Can also build your own.

6
Cursors in AWT
  • Each component can choose its cursor
    (Component.setCursor), or inherit from its
    parent. Cursor appearance changes when pointer
    enters component.
  • Cursor consists of an Image and a hotspot
    (relative pointer location).

7
Built-in cursorsCustom cursors
8
Setting component cursor
  • // Create a new JButton with a custom cursor
  • // myCursor.
  • JButton bnew JButton(text)
  • Image imnew ImageIcon(myFile.gif).getImage()
  • // Note the above is the shortest way to load an
  • // image from file. Many other methods exist.
  • Point hotspotnew Point(0,15)
  • // This sets hotspot at center of left edge
  • // (assuming a 32x32 image)
  • String namemyCursor
  • b.setCursor(b.getToolkit().createCustomCursor(im,h
    otspot,name))

9
Transparency
  • With most image formats, it is possible to mark
    at least one color as transparent. One use
    although images are stored as rectangles, can
    make them appear to be other shapes.
  • For cursors, this is the norm.
  • Can also make transparent buttons.

10
Translucency
  • There is an even more advanced technique called
    alpha-compositing, in which every pixel of an
    image is assigned a number representing its
    translucency. This is likely to become more
    common in the future. Ill come back to this
    when I discuss Java2D, another day.

11
Data Transfer
  • Internal between Objects
  • Between JVMs (rather easy with Serialization
    or RMI)
  • via the OS to/from Clipboard
  • via the OS Drag and Drop

12
Data Flavors
  • Data comes in many formats.
  • Format unknown ? data unusable
  • Makes sense to keep a label on data describing
    the format.
  • MIME types are the de-facto internet standard.
    Javas DataFlavor class is a slight extension to
    MIME.

13
MIME
  • Multipurpose Internet Mail Extensions format.
    e-mail, http Content-type
  • type/subtype
  • A proposed standard RFC 2912, 2913.
  • Examples text/plain, image/jpeg, image/gif,
    audio/basic, video/mpeg, application/x-java-serial
    ized-object, model/vrml, text/rtf,
    multipart/signed

14
Whats in a flavor?
A drama in 6 slides
Meet the cast
Hi, I am Mysterio, an instance of a class
implementing the Transferable interface.
Hi, Im Jo Java. I want to get cool stuff from
Mysterio. And I want it now, now, now!
15
Whats in a flavor?
Mysterio, what cool stuff do you have?
Haha, not so fast my dear! Ill only tell you if
you invoke the right methods (wink, wink)!
Scum! Give me what I want now! mysterio.getTransf
erDataFlavors()
16
Whats in a flavor?
Ouch, you win! I have cool stuff in the
following flavors text/html,
application/powerpoint, text/rtf, image/jpeg
Quick, give me the image/jpeg. Um, I
mean mysterio.getTransferData(jpegFlavor)
17
Whats in a flavor?
OK! Heres an Object representing the data you
requested. Bye!
???
18
Whats in a flavor?
Oh, no! Not a black box! This thing is
supposed to represent a JPEG?
Object
How am I supposed to put this on screen?
19
Whats in a flavor?
Object
Wait, the DataFlavor for image/jpeg tells me
the class of this Object. jpegFlavor.getRepresent
ationClass() Its an InputStream. How quaint.
Now I can read the JPEG file from this stream,
and try to display it.
20
Epilogue
But Transferables from other applications are
usually represented by a java.io.InputStream
These take more work.
Object
Usually, when Transferables come from within Java
programs, they include a DataFlavor obtained
by new DataFlavor(Class.forName(String),String) T
hese are represented by a ready-to-use Java object
21
Flavors ready for scooping
  • DataFlavor.stringFlavor
  • DataFlavor.getTextPlainUnicodeFlavor()
  • DataFlavor.javaSerializedObjectMimeType
  • DataFlavor.javaFileListFlavor
  • new DataFlavor(Class.forName(mypackage.MyClass),
  • My Neat-O Class)
  • new DataFlavor(MIMETypeString, HumanReadableString
    )

22
Using the Clipboard
  • Pass Transferables to and from system Clipboard.
    The clipboard must be obtained with
    Toolkit.getSystemClipboard()
  • Pasting use theClipboard.getContents() to get a
    Transferable object. Then extract the DataFlavor
    you want as previously described.

23
Clipboard Cutting
  • When cutting, we are promising the system that
    we will make the clipboard material available
    until it is replaced by the user. The
    ClipboardOwner interface allows the OS to notify
    us when we can forget the clipboard contents.
  • Clipboard.setContents() method cuts. Must
    implement Transferable for this.

24
Clipboard notes
  • Swing text components already support
    cut-and-paste to system clipboard. Often this is
    enough.
  • Can also have local clipboards. Useful if you
    want more persistence and dont wish to allow
    data exchange with other programs.

25
Gestures
  • A gesture is a high-level input event
    comprising a series or combination of several
    low-level mouse events. Can convey complicated
    information more easily than low-level mouse
    events.

26
Gestures are
  • information-rich
  • natural to the user
  • subject to misinterpretation/misinput
  • common to many tasks (even a language in
    themselves)
  • not necessarily natural to the UI
  • culture-specific

27
Gestures in Java
  • Java does not have a Gesture class.
  • Does have an extremely powerful mechanism for
    handling drag gestures
  • Supports drag-and-drop between components,
    objects, even non-Java applications.
  • Useful for moving, copying, linking, etc.

28
Package java.awt.dnd
  • On drag end must set up DragSource,
    DragGestureListener, DragSourceListener
  • On drop end must set up DropTarget,
    DropTargetListener
  • A successful drop gives the DropTargetListener a
    reference to a Transferable Object. See
    java.awt.datatransfer

29
Initialization
Source Component
Target Component
DragSource
DragSourceListener
DropTargetListener
DropTarget
DragGestureListener
DragSourceContext
The AWT Implementation (System specific)
DragSourceContextPeer
30
Flow of Events
Source Component
Target Component
DragSource
DragSourceListener
DropTargetListener
DropTarget
DragGestureListener
DragSourceContext
The AWT Implementation (System specific)
DragSourceContextPeer
31
DND within Java
32
DND recipe
  • Obtain a reference to a drag source with
    DragSource.getDefaultDragSource() or by
    instantiating a new one -- for example, new
    DragSource() .
  • Create a drag gesture recognizer with the drag
    source from step 1 by invoking DragSource.createDe
    faultDragGestureRecognizer(). The method is
    passed the component where the drag originates.
  • Wrap the data to be dragged in a transferable.

33
DND Recipe part II
  • 4. Initiate a drag when the drag gesture
    recognizer from step 2 is notified by invoking
    DragSource.startDrag() for the drag source from
    step 1. The transferable from step 4 is passed to
    the startDrag() method.
  • 5. Handle the drop by implementing the
    DropTargetListener interface.
  • 6. Implement the DragSource interface (often with
    empty methods).

34
Click gestures
  • Instead of associating a single command to a
    button, can associate two or more. For instance,
    left-click, right-click, double-click,
    shift-click, alt-click, left-and-right-click,
    etc. However, Java doesnt support this well.
  • Can recognize double-clicks using
    MouseEvent.getClickCount() method.

35
Windows 2000 clicks
36
Windows 2000 drags
37
DND Reading
  • DND Fundamentals (Geary, Sun)
  • Intro, part 1 (Javaworld)
  • Intro, part 2 (Javaworld)
  • Suns DND trail (minimal)
  • DND Links (esus)
  • JFC in a Nutshell, Chapter 6.
  • Data Transfer API (Javaworld)

38
Creating a DesktopIcon class
  • Desktop icons represent files. Their
    transferable content is most naturally
    represented internally by the File class.
    Externally by DataFlavor.javaFileListFlavor.
  • Set up DND. Ordinary files have dragsources.
    Directories have dragsources and dragtargets for
    MOVE and COPY.
Write a Comment
User Comments (0)
About PowerShow.com