Skip to content

Latest commit

 

History

History
46 lines (44 loc) · 2.44 KB

javax.swing.md

File metadata and controls

46 lines (44 loc) · 2.44 KB

javax.swing

Overview

javax.swing is the library built into Java for creating graphic user interfaces (GUIs). It is more challenging to use than stdlib, but significantly more powerful. For instance, in buttons, sliders, and pop-up menus.

The best way to learn Swing is probably to read chapters 10 and 11 of Horstmann's Core Java. The second best is to work through the code for these chapters from his website; I do this in the first couple of weeks of Software Development.

One could make a reasonable argument that there are better libraries (such as JavaFX) or more modern ways of creating GUIs (such as creating web-based interfaces). Following Hortsmann's lead, I have chosen to stick with Swing so as not to overload students with too many installations, tools, and languages.

Components

Look up individual components as you need details. For quick reference, here are some of the most widely-used classes:

  • Layout
    • GridBagLayout
    • JPanel
    • JScrollPane
  • Text
    • JLabel
    • JTextField
    • JPasswordField
    • JTextArea
  • Choice
    • JButton
    • JCheckBox
    • ButtonGroup / JRadioButton
    • JComboBox
    • JSlider
  • Menus
    • JMenuBar / JMenu / JMenuItem
    • JPopupMenu
    • JToolBar
  • Dialogs
    • JOptionPane
    • JDialog
    • JFileChooser

Resources

Questions

  1. ⭐ Which class corresponds to a visible window on the screen?
  2. ⭐ Where is 0, 0 in the coordinate system used by Swing?
  3. ⭐⭐ If some back-end information in your program has changed and you need to re-draw something, what method should you call?
  4. ⭐⭐ Why can't you create an instance of Rectangle2D?

Answers

  1. javax.swing.JFrame.
  2. At the upper left. The first coordinate is the x coordinate from left to right (in pixels). The second is the y coordinate from top to bottom.
  3. java.awt.Component.repaint (usually on a JFrame, which descends from Component).
  4. It is an abstract class. You must instead create an instance of Rectangle2D.Double (or, if you're creating a huge number of objects, Rectangle2D.Float).