PV168

Swing

Where did the last seminar pain come from?

  • You were solving high-level problems with too much low-level tools
  • You were missing the appropriate abstractions for your tasks!

Abstraction

The essence of abstractions is preserving information that is relevant in a given context, and forgetting information that is irrelevant in that context. [1]

[1]: Guttag, John V. (18 January 2013). Introduction to Computation and Programming Using Python (Spring 2013 ed.).
Cambridge, Massachusetts: The MIT Press. ISBN 9780262519632.

Real Life Abstractions

  • Cars
    • Steering wheel
    • Pedals (brake and throttle)
  • Electronic devices
    • Plug
    • Socket

Abstractions you were missing

  • Java Swing Action
    • Substitute for menu items and toolbar buttons
  • Our own Column (for Swing TableModel)
    • Shields us from Repeated Switches problem

Java Swing

Java Swing

  • Added to Java in 1.2
  • Built on top of AWT (Abstract Window Toolkit) which is Java 1.0
  • Desktop applications are not web applications!!!
  • They can be very complex and even look good
    • A prominent example built on top of Swing/AWT is IntelliJ IDEA
Java Swing

Windows and Containers

  • JFrame vs. JDialog
    • Use JFrame for the part of your program that is always visible
    • Use JDialog's pop up when you need them, and disappear when you don't
  • JPanel is also useful
    • Use JPanel as a container for your elements when you have more of them ...
    • ...and you need to have the full control over their layout
Java Swing

JFrame

  • Fully-functional top-level window
    • Resizeable
    • With pull-down menus
    • Typically with toolbars
  • Default layout manager
    • BorderLayout
Java Swing

JDialog

  • Pop up window
    • Positioned relatively to a parent window
    • Fixed size
    • No menus or toolbars
    • Default button
    • Closes on Esc
Java Swing

JDialog (cont...)

  • Typically modal
    • Underlying windows cannot get user input
  • Typically used via JOptionPane and JPanel
Java Swing

Menus

  • JMenuBar + JMenu for application menu bar and pull-down menus
    • Use JMenuBar for the application menu bar
    • Add JMenu for each pull-down menu
  • JPopupMenu for pup-up menus displayed on right mouse click

  • JMenuItem for standard menu item (text + icon)
  • JCheckBoxMenuItem for menu items with a checkbox
  • JRadioButtonMenuItem for menu items with a radio button
Java Swing

Toolbars

  • JToolbar as a container of JButton objects
Java Swing

Buttons

  • JButton as a standard button (text + icon)
  • JCheckBox for two-state buttons with a check box
  • JRadioButton for two-state buttons with a radio button
    • Note however that radio buttons only make sense in groups
Java Swing

Text Fields

  • JLabel for displaying a non-editable text
    • Very useful especially in dialogs
  • JTextField for entering a single line of text
  • JPasswordField for entering a password
  • JTextArea for entering multiple lines of text
    • Has to be wrapped inside JScrollPane to work correctly for large content
Java Swing

Numerical Fields

  • JSpinner for entering a number
    • from an ordered sequence
  • JSlider for selecting a value graphically
    • from an interval
Java Swing

Date Fields

  • JDatePicker for selecting a calendar date

  • Unfortunately this component comes from 3rd party
    • We are using io.github.lzh0379:jdatepicker:2.0.3
Java Swing

List, Tables, and Trees

  • JList for a visualizing a list of simple values (single column)
    • The collapsed version with only a single value shown is JComboBox
  • JTable for visualizing a list of compound values (matrix)
  • JTree for visualizing a hierarchical structure of simple values

  • All of them need to be wrapped inside JScrollPane to work correctly for large content
Java Swing

Tabs, Splits

  • JTabbedPane for splitting the content into tabs (label + icon)
  • JSplitPane for splitting exactly two components
  • JScrollPane for wrapping a component that can grow indefinitely
Java Swing

Rich Text

  • Many components support HTML for rich-text possibilities
  • The text needs to be prefixed with <html>
    • Without the need for closing pair tag </html>
  • Then a whole lot of HTML tags can be used
    • For adjusting font attributes
    • For hypertext links
    • Etc.