Graphical User Interface Components Chapter 14
2 What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look and feel options Look and feel options
3 JTextArea Area for manipulating multiple lines of text Area for manipulating multiple lines of text Extends JTextComponent Extends JTextComponent –Inherits from that class View Figure 14.1 View Figure 14.1Figure 14.1Figure 14.1 Note Note –Creation of box –Creation of two JTextArea objects –Copy of marked text to other text area
4 Subclass of JPanel A JPanel can A JPanel can –Aggregate a set of GUI components –Create dedicated drawing areas –Create areas that receive mouse events Subclass of a JPanel should Subclass of a JPanel should –Override method paintComponent –Call the superclass version as first statement Note the customized JPanel subclass, Figure 14.2 Note the customized JPanel subclass, Figure 14.2 Figure 14.2 Figure 14.2
5 Demonstration of Customized JPanel Now view Figure 14.3 Now view Figure 14.3Figure 14.3Figure 14.3 Note Note –Declaration, instantiation of the CustomPanel –Method draw used in actionPerformed methods
6 Another Customized JPanel Class Possible for the (sub)class to handle its own events Possible for the (sub)class to handle its own events Note SelfContainedPanel class, Figure 14.4 Note SelfContainedPanel class, Figure 14.4SelfContainedPanel This class has This class has –Mouse activity handlers –Preferred dimension specified –Call to paintComponent method Figure 14.5 uses this customized subclass Figure 14.5 uses this customized subclass Figure 14.5 Figure 14.5
7 JSlider s Enable users to select from range of integer values Enable users to select from range of integer values Features Features –Tick marks (major and minor) –Snap-to ticks –Orientation (horizontal and vertical) thumb tick mark
8 JSlider s JPanel subclass for drawing circles, Figure 14.7 JPanel subclass for drawing circles, Figure 14.7 Figure 14.7 Figure 14.7 JSlider used to control size of a circle graphic, Figure 14.8 JSlider used to control size of a circle graphic, Figure 14.8Figure 14.8Figure 14.8 Note Note –Slider declaration, implementation –Registration of listener for the slider
9 Using Menus with Frames Allows for performing actions with cluttering GUI Allows for performing actions with cluttering GUI Contained by menu bar – JMenuBar Contained by menu bar – JMenuBar Comprised of menu items – MenuItem Comprised of menu items – MenuItem Note Figure 14.9 Note Figure 14.9Figure 14.9Figure 14.9 –Declares menu items –Populates the menus –Specifies actions for menu choices
10 JPopupMenu Menus that are context-sensitive Menus that are context-sensitive –Right click and popup menu appears In Swing, use JPopupMenu In Swing, use JPopupMenu –Provide options specific to component –Popup trigger event occurs when object right mouse clicked Demonstration of JPopupMenu shown in Figure Demonstration of JPopupMenu shown in Figure Figure Figure 14.10
11 Pluggable Look-and-Feel Recall that Java is designed to run on variety of platforms Recall that Java is designed to run on variety of platforms –GUI components on a Mac, look different from those on a PC or on a UNIX platform Look-and-Feel capability Look-and-Feel capability –Java program on a particular platform looks like other programs on the same platform –Also possible to specify the look-and-feel of a Swing GUI
12 Pluggable Look-and-Feel Figure demonstrates how to change the appearance Figure demonstrates how to change the appearance Figure Figure Note use of UIManager methods Note use of UIManager methods
13 JDesktopPane and JInternalFrame Multiple document interface Multiple document interface –Main (parent) window –Child windows View Figure which demonstrates this View Figure which demonstrates thisFigure Figure Note Note –Declaration, instantiation of JDesktopPane object –Choosing and painting of the icons
14 JTabbedPane Arranges GUI components into layers Arranges GUI components into layers –One layer visible at a time –Access each layer via a tab View tab demo program, Figure View tab demo program, Figure Figure Figure Note Note –declaration of tabbed panes
15 Layout Managers BoxLayout a rranges GUI components BoxLayout a rranges GUI components –Horizontally along x-axis –Vertically along y-axis
16 BoxLayout Demonstrated in Figure Demonstrated in Figure 14.15Figure 14.15Figure –Also uses the tabs –Offers several different options for laying out three JButton s Note Note –Creation of boxes –Adding of buttons
17 GridBagLayout Layout Manager Flexible GridBagLayout Flexible GridBagLayout –Components can vary in size –Components can occupy multiple rows and columns –Components can be added in any order Uses GridBagConstraints Uses GridBagConstraints –Specifies how component is placed in GridBagLayout
18 GridBagLayout Layout Manager GridBagConstraints fields GridBagConstraints fields
19 GridBagLayout Layout Manager Designing a GUI to use the GridBagLayout Designing a GUI to use the GridBagLayout Program for this layout demonstrated in Figure Program for this layout demonstrated in Figure Figure Figure Row Column
20 GridBagLayout Layout Manager Variation of GridBagLayout does not use gridx and gridy Variation of GridBagLayout does not use gridx and gridy –Use constants RELATIVE and REMAINDER RELATIVE RELATIVE –Specifies next-to-last component placement in row or column –Component placed next to one previously added REMAINDER REMAINDER –Specifies component as last component in row or column
21 GridBagLayout Layout Manager Use of constraints demonstrated in Figure Use of constraints demonstrated in Figure Figure Figure Note Note –Setting constraint weights –Setting grid width –Adding buttons –The addComponent method