Swing 1 Session 12 Objectives 1 n Discuss

Swing - 1 Session 12

Objectives (1) n Discuss JFC n Describe Swing classes such as n n n JComponent JFrame JPanel JApplet JRoot. Pane. JScroll. Pane. JView. Port Swing – 1 / 2 of 34

Objectives (2) n Describe GUI Components such as n n Label Text. Components Describe Buttons, Check. Boxes and Radio. Buttons Describe Lists and Combo. Boxes Swing – 1 / 3 of 34

Introduction n n Individuals are constantly on the lookout for simpler means of carrying out a particular activity Software platforms provide us with “Foundation Classes” (FC) FC simplifies the designing process and reduces time taken to code For example, creating a menu is simplified by FC Microsoft Foundation Classes (MFC) and Java Foundation Classes (JFC) are two popularly used classes Swing – 1 / 4 of 34

Java Foundation Classes (JFC) Programmer n n AWT Applications or Applets AWT had restrictions in rendering and event handling This led to the development of JFC extends the original AWT by adding a set of GUI class libraries Provides additional visual component classes Swing – 1 / 5 of 34

Swing Components (1) n n n Swing is a set of classes under JFC Provides lightweight visual components Contains replacement components for AWT visual components and also complex components - trees and tables While designing a GUI, there is a main window on which visual components are placed Swing components are in the javax. swing package The names of all Swing components start with J Swing – 1 / 6 of 34

Swing Components (2) Top-level container Menu Bar Visible Components Content pane Swing – 1 / 7 of 34

Frames n n A top-level container or window Provides place for other Swing components JFrame components is used to create windows in Swing program Some of its constructors are: n n n JFrame() JFrame(String Title) Components have to be added to the content pane and not directly to the JFrame object n Example : frame. get. Content. Pane(). add(b); Swing – 1 / 8 of 34

Frames - Example Output Swing – 1 / 9 of 34

Panels n n JPanel component is an intermediate container Used to group small lightweight components together JPanel objects have Flow. Layout as their default layout JPanel has the following constructors: n n JPanel() JPanel(Layout. Manager lm) Swing – 1 / 10 of 34

Applets n n javax. swing. JApplet is slightly different from java. applet. Applet While adding a component to a JApplet component, it becomes mandatory to add it to the JApplet’s content pane n Example : get. Content. Pane(). add(component); Swing – 1 / 11 of 34

Applet - Example Output Swing – 1 / 12 of 34

Content Pane and Applets n Content pane makes Swing applets different from regular applets in the following ways: n n Components are added to the content pane of Swing applets, not directly to the applet The layout manager is set on a Swing applet’s content pane, not directly on the applet Default layout manager for Swing applet’s content pane is Border. Layout whereas for regular applet’s, it is Flow. Layout Painting code is not directly put in a JApplet object Swing – 1 / 13 of 34

Basic GUI Components n n n A form can be used for collecting information While creating GUI’s, the text can be placed on the content pane using labels To create elements on a GUI: n n Create the element Set its attributes (size, color, font) Position it Add it to the screen Swing – 1 / 14 of 34

Various components Label Text field Checkbox Radio button Text Area Button Swing – 1 / 15 of 34

Label Output Swing – 1 / 16 of 34

Text. Components (1) n JText. Component is the root class of all Swing text components JText. Field JText. Area JText. Component JEditor. Pane JText. Pane JPassword. Field Swing – 1 / 17 of 34

Text. Components (2) n n JText. Field component allows us to enter/edit a single line of text The constructors of this class are: n n n JText. Field() JText. Field(Document doc, String text, int columns) JText. Field(String text) JText. Field(String text, int columns) Swing – 1 / 18 of 34

Text. Components (3) Output Swing – 1 / 19 of 34

Text Area (1) n n n JText. Area component is used to accept several lines of text from the user It Implements the scrollable interface to activate scrollbars JText. Area component can be created using: n n n JText. Area() JText. Area(Document doc, String text, int rows, int cols) JText. Area(int rows, cols) JText. Area(String text, int rows, int cols) Swing – 1 / 20 of 34

Text Area (2) Output Swing – 1 / 21 of 34

Buttons n n Buttons trap user action JButton class descends from javax. swing. Abstract. Button class JButton object consists of a text label and/or image icon, empty area around the text/icon and border A JButton can be created using: n n n JButton() JButton(Icon icon) JButton(String text, Icon icon) JButton(Action a) Swing – 1 / 22 of 34

Buttons – Example (1) Continued on next slide Swing – 1 / 23 of 34

Buttons – Example (2) Output Swing – 1 / 24 of 34

Checkbox n n Checkbox is used to provide the user with a set of options JCheck. Box class has the following constructors: n JCheck. Box() n JCheck. Box(Icon icon, boolean selected) n JCheck. Box(String text, Icon icon, boolean selected) n JCheck. Box(Action a) Swing – 1 / 25 of 34

Checkbox – Example (1) Continued on next slide Swing – 1 / 26 of 34

Checkbox – Example (2) Output Swing – 1 / 27 of 34

Radio button n A set of radio buttons displays a number of options out of which only one may be selected A Button. Group is used to create a group in Swing JRadio. Button object can be created by using: n n n n JRadio. Button() JRadio. Button(Icon icon) JRadio. Button(Icon, boolean selected) JRadio. Button(String text, Icon icon, boolean selected) JRadio. Button(Action a) Swing – 1 / 28 of 34

Lists n n n When the options to choose from is large, the user can be presented with a list to choose from JList component arranges elements one after the other, which can be selected individually or in a group JList class can display strings as well as icons JList does not provide support for double clicks Mouse. Listener can be used to overcome the double click problem Swing – 1 / 29 of 34

JList constructors n n public JList() – constructs a JList with an empty model public JList (List. Model data. Model) – displays the elements in the specified, non-null list model public JList(Object [] list. Data) – displays the elements of the specified array “list. Data” JList does not support scrolling. To enable scrolling, the following piece of code can be used: JScroll. Pane my. Scroll. Pane=new JScroll. Pane(); my. Scroll. Pane. get. Viewport(). set. View(data. List); Swing – 1 / 30 of 34

Lists – Example (1) Continued on next slide Swing – 1 / 31 of 34

Lists – Example (2) Output Swing – 1 / 32 of 34

Combo Box n n n Combination of text field and drop-down list In Swing, combo box is represented by JCombo. Box class public JCombo. Box() – this constructor creates a JCombo. Box with a default data model public JCombo. Box(Combo. Box. Model as. Model) – a combo box that takes items from an existing Combo. Box. Model public JCombo. Box(Object [] items) – a combo box that contains the elements of the specified array Swing – 1 / 33 of 34

Combo box - Example Output Swing – 1 / 34 of 34
- Slides: 34