HCI: Advanced Swing, Handling Texts - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

HCI: Advanced Swing, Handling Texts

Description:

Dr. Ehud Reiter, Computing Science, University of Aberdeen. 1 ... optional radix. Double.parseDouble. also Float.parseFloat. Boolean.valueOf ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 37
Provided by: computin7
Category:

less

Transcript and Presenter's Notes

Title: HCI: Advanced Swing, Handling Texts


1
HCI Advanced Swing, Handling Texts
  • Advanced Swing
  • Handling texts
  • Reading Swing tutorial

2
Look and Feel
  • You can set the Look and Feel of a Swing
    program
  • windows (PC)
  • metal (original Java)
  • motif (common on Unix)
  • Define your own using GTK
  • Affects appearance, not function

3
Look and feel examples
Metal (Java) (NetBeans default)
Windows
Motif
4
Setting Look and Feel
  • Set in NetBeans
  • Start-up parameter
  • Set at run-time
  • in main method before GUI instance form
  • For platform (eg, Window on Windows PC)
  • try UIManager.setLookAndFeel(UIManager
    .getSystemLook AndFeelClassName())
  • catch (Exception e) System.out.println(e.toStri
    ng())

5
Look and Feel
  • Swing mechanism criticised for not being very
    effective
  • One reason Swing not widely used?
  • SWT alternative to Swing
  • IBM, Eclipse
  • VEP GUI builder
  • Claims to do a better job of adapting to the
    platforms look and feel

6
Text Components
  • Powerful!
  • Formatted text, colours, embedded graphics,
    clickable hyperlinks
  • Input/output from HTML and RTF (word)
  • some support for undo/redo
  • Complex to use
  • See Swing tutorial

7
Graphics
  • Java2D package
  • Paint tool for Java
  • Draw lines, points, shapes, text
  • arbitrary shape, size, colour
  • Include images (eg, GIF files)
  • Detect what user clicked mouse on
  • also Java3D package
  • Will discuss later in course

8
Sound
  • AudioClip plays sounds from WAV, etc
  • once, looping
  • simple to use
  • Java Media Framework (JMF)
  • more powerful, more complex
  • more control
  • plays video as well as audio

9
Timer
  • Ticks (triggers an action) at specified
    intervals
  • delay (millisec)
  • whether once-shot or repeats
  • action
  • Useful for animations
  • 3 versions Java, Swing, NetBeans
  • Under Beans palette in NB

10
Fancy widgets
  • JFileChooser
  • JColorChooser
  • JToolBar
  • JTable
  • JTree
  • JProgressBar

11
Swing in Java versions
  • Coming in Java 5 (1.5)
  • More look and feels (skin like?)
  • Set default location for window
  • Better integration with Windows for Unicode
  • Incremental changes, not huge
  • Swing is not focus of development efforts

12
Handling Text
  • User enters information in GUI
  • How can we get this information into our program?
  • Programs produces information for user
  • How can we display this on the GUI?

13
Typical Event Handler
  • Read information from GUI widgets
  • Convert to internal form (eg, int)
  • Do computations
  • Convert back to strings
  • Display in GUI widgets

14
Read from GUI
  • Each widget has method(s) for reading its value
  • text fields getText() method
  • list getSelectedValue(), getSelectedValues()
  • checkbox isSelected()
  • etc

15
Display in GUI
  • Also methods for setting values
  • text fields setText(String) method
  • list setSelectedIndex(int), setSelectedValue(Obje
    ct)
  • checkbox setSelected(boolean)
  • etc

16
String lt-gt int, etc
  • Converting text to/from integers, doubles, dates,
    etc.
  • How can we read formatted data
  • 21 March 2001, 2.35E-2
  • How can we write formatted data?
  • 2.35E-2
  • 0.0235
  • 0.02

17
Basic String-gtint, etc
  • Integer.parseInt
  • optional radix
  • Double.parseDouble
  • also Float.parseFloat
  • Boolean.valueOf
  • throw Exception if text isnt a legal int,
    double, etc

18
Basic int/etc -gt text
  • String.valueOf
  • int, double, float, boolean, etc
  • no control over appearance
  • also Integer.toString, etc

19
Example double
  • Read in number from text box, double it, write it
    back to the text box
  • String input textBox.getText()
  • int value Integer.parseInt(input)
  • value value2
  • String output String.valueOf(value)
  • textBox.setText(output)

20
Catch errors
  • String input textBox.getText()
  • try
  • int value Integer.parseInt(input)
  • value value2
  • String output String.valueOf(value)
  • textBox.setText(output)
  • catch (Exception ex)
  • JOptionPane.showMessageDialog(this,
    ex.toString())

21
Real numbers
  • String input textBox.getText()
  • try
  • double value Double.parseDouble(input)
  • value value2
  • String output String.valueOf(value)
  • textBox.setText(output)
  • catch (Exception ex)
  • JOptionPane.showMessageDialog(this,
    ex.toString())

22
Richer formatting java.text
  • NumberFormat - formatting numbers
  • DateFormat - formatting times/dates
  • MessageFormat - formatting strings
  • not discussed here, just concatenate
  • Depend on Locale (location)
  • 29 March 2002 (UK)
  • March 29, 2002 (US)

23
NumberFormat
  • For more complex formatting
  • Create a formatter, use for input and output
  • many types (currency, percent, etc)
  • locale-specific
  • parse method for input
  • format method for output

24
Example (doubleGui)
  • Import java.text.
  • String text numberField.getText()
  • NumberFormat nf NumberFormat.getInstance()
  • // default formatter for UK locale
  • double value nf.parse(text).doubleValue()
  • value value2
  • numberField.setText(nf.format(value))

25
Inputs and outputs
  • Allowable inputs
  • 1000
  • 1,000
  • 1000.0
  • Output of double
  • 2,000

26
Other formats
  • NumberFormat.getCurrencyInstance()
  • input 2000
  • output 4000.00
  • NumberFormat.getPercentInstance()
  • make value a double
  • input 20 becomes 0.2
  • output 0.4 becomes 40

27
DecimalFormat
  • Provides exact control of number format
  • eg, can avoid scientific format (1.23E-02)
  • Based on patterns
  • nf new DecimalFormat(0.)
  • one integer digit, up to 2 fractional digits
  • 3
  • 0.04
  • 1.23
  • see documentation for details

28
Example
  • Import java.text.
  • String text numberField.getText()
  • DecimalFormat nf new DecimalFormat(0.)
  • // default formatter for UK locale
  • double value nf.parse(text).doubleValue()
  • value value2
  • numberField1.setText(nf.format(value))

29
JFormattedTextField
  • Text field with associated formatter
  • Constructor specified formatter
  • getValue() gets value using formatter
  • Combines getText() and parse()
  • setValue shows value using formatter
  • Combines format() and setText()
  • Value shown updates when focus lost
  • Hard to use from NB, wont discuss

30
Date, Time format
  • DateFormatter - like NumberFormatter
  • types date, time, datetime
  • lengths
  • Short 08/02/02
  • Medium 08-Feb-02
  • Long, Full 08 February 2002
  • SimpleDateFormatter - specify format
  • using pattern, like DecimalFormat

31
Example
  • Import java.text., java.util.Date
  • String text box.getText()
  • DateFormat df DateFormat.getDateInstance(DateFor
    mat.SHORT)
  • Date value df.parse(text)
  • box.setText(df.format(value))

32
Parsing Strings
  • Breaking a string up into values
  • known as tokenisation
  • Fred went home -gt Fred, went, home
  • Java.util.StringTokenizer

33
Example
  • StringTokenizer st new
  • StringTokenizer("this is a test")
  • while (st.hasMoreTokens())
  • println(st.nextToken())
  • this
  • is
  • a
  • test

34
Parameters
  • Delimeters characters that sep tokens
  • typically space, comma, full stop,
  • Flag are delimeters tokens

35
Other parsers
  • Java.io.StreamTokeniser
  • many more parameters, intended for input from
    file or terminal
  • Java.util.regex
  • regular expressions
  • Lemmatisers
  • children -gt childplural
  • research systems

36
Summary
  • Read string from GUI using getText, etc
  • Convert string into int, double if required,
    using a formatter
  • Do computations
  • Convert int, double, etc into string using a
    formatter
  • Display string on GUI using setText, etc
Write a Comment
User Comments (0)
About PowerShow.com