Download presentation
Presentation is loading. Please wait.
1
Advanced GUI Applications
Chapter 13 Advanced GUI Applications
2
Contents The Swing and AWT Class Hierarchy Read-Only Text Fields Lists
Combo Boxes Display Images in Labels and Buttons Mnemonics and Tool Tips File Choosers and Color Choosers Menus
3
Contents More about Text Components Text Areas Fonts Sliders
Look and Feel
4
I. The Swing and AWT Class Hierarchy
The classes that are in the unshaded top part are AWT classes and are in the java.awt package. The classes that are in the shaded bottom part are Swing classes and are in the javax.swing package. Notice that all of the components we have dealt with ultimately inherit from the Component class.
5
I. The Swing and AWT Class Hierarchy
java.awt package javax.swing package
6
II. Read-Only Text Fields
A read-only text field displays text that Can be changed by code in the application. Cannot be edited by the user. A read-only text field is not a new component, but a different way to use the JTextField component. The JTextField component has a method named setEditable: setEditable(boolean editable) If editable is false, then the text field becomes read-only.
7
II. Read-Only Text Fields
The following code could be used to created the read-only text field: JTextField subtotalField = new JTextField(); subtotalField.setEditable(false); We can use the setText method to display data inside it: subtotalField.setText(“100.00”);
8
III. Lists Lists Selection Modes Responding to List Events
Retrieving the Selected Items Placing a Border around a List Adding a Scroll Bar to a List Adding Items to an Existing JList Component Multiple Selection Lists
9
III.1 Lists A list is a component that displays a list of items
allows the user to select one or more items from the list. Java provides the JList component for creating lists. At runtime, the user may select an item in the list, which causes the item to appear highlighted.
10
JList(Object[] array)
III.1 Lists When we create an instance of the JList class, we pass an array of objects to the constructor: JList(Object[] array) The JList component uses the array to create the list of items. String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JList nameList = new JList(names);
11
III.2 Selection Modes The JList component can operate in any of the following selection modes: Single Selection Mode: Only one item can be selected at a time. When an item is selected, any other item that is currently selected is deselected. Single Interval Selection Mode: Multiple items can be selected, but they must be in a single interval. An interval is a set of contiguous items. Multiple Interval Selection Mode: Multiple items may be selected with no restrictions. This is the default selection mode.
12
Single selection mode allows only one item to be selected at a time.
III.2 Selection Modes Single interval selection mode allows a single interval of contiguous of item to be selected. Multiple interval selection mode allows multiple item to be selected with no restriction. Single selection mode allows only one item to be selected at a time. Using the method setSelectionMode of JList to change the selection mode: nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); nameList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION ); nameList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTI ON);
13
III.3 Responding to List Events
When an item in a JList object is selected, it generates a list selection event. We handle list selection events with a list selection listener class, which must meet the following requirements: It must implement the ListSelectionListener interface. It must have a method named valueChanged. This method must take an argument of the ListSelectionEvent type.
14
III.3 Responding to List Events
Using the addListSelectionListener method of the JList component to add an object of the list selection listener class. When the JList component generates an event, it automatically executes the valueChanged method of the list selection listener object, passing the event object as an argument.
15
III.4 Retrieving the Selected Items
To determine which item in a list is currently selected, we may use either getSelectedValue Returns a reference to the item that is currently selected. If no item is selected, the method returns null. The return value is an Object reference. We had to cast the return value. getSelectedIndex Returns the index of the selected item, or -1 if no item is selected. The first item (the item at the top of the list) has the index 0. We can use the index to retrieve the item from an array.
16
III.4 Retrieving the Selected Items
String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JList nameList = new Jlist(names); Using the getSelectedValue method String selectedName; selectedName = (String) nameList.getSelectedValue(); Using the getSelectedIndex method int index; String selectedName; index = nameList.getSelectedIndex(); if(index != -1) selectedName = names[index];
17
III.4 Retrieving the Selected Items
Write a GUI program that allows the user selects an item in the list of months. The selected month is displayed in a read-only text field.
18
III.4 Retrieving the Selected Items
Create a class that extends the JFrame class BorderLayout manager A panel to hold a list of months Center region of the content pane A JList component A list of months Single selection mode Add an object of the list selection listener class
19
A panel to hold a selected month
South region of the content pane A JTextField component Width: 10 Uneditable text field A JLabel component “You Selected:”
20
Create a list selection listener class
A private inner class Implements the ListSelectionListener interface valueChanged method Get the selected month of the list Set the selected month to the text field
24
III.5 Placing a Border around a List
We can use the setBorder method to draw a border around a list. monthList.setBorder( BorderFactory.createLineBorder(Color.BLACK,1)); Draws a black 1-pixel thick line border around the monthList component.
25
III.6 Adding a Scroll Bar to a List
By default, a list component is large enough to display all of the items it contains. Most GUI applications display a scroll bar on list components that contain a large number of items. The user simply uses the scroll bar to scroll through the list of items. To display a scroll bar on a list: Set the number of visible rows for the list. Create a scroll pane object and add the list to it. Add the scroll pane to any other containers, such as panels.
26
III.6 Adding a Scroll Bar to a List
Let's take a closer look at how to apply a scroll bar to the list component: String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JList nameList = new Jlist(names); Set the number of visible rows of the list: nameList.setVisibleRowCount(3); Create a scroll bar pane and add the list to it: JScrollPane scrollPane = new JScrollPane(nameList);
27
III.6 Adding a Scroll Bar to a List
Add the scroll pane object to any containers that are necessary for our GUI: //Create a panel and add the scroll pane to it JPanel panel = new JPanel(); panel.add(scrollPane); //Add the panel to this JFrame object's content //Pane add(panel);
31
III.6 Adding a Scroll Bar to a List
By default, when a JList component is added to a JScrollPane object, the scroll bar is only displayed when there are more items in the list than there are visible rows. When a JList component is added to a JScrollPane object, a border will automatically appear around the list.
32
III.7 Adding Items to an Existing JList Component
The method setListData of JList class allows you to store items in an existing JList component. void setListData(Object[] data) data: is an array of objects that will become the items displayed in the JList component. Any items that are currently displayed in the component will be replaced by the new items.
33
III.7 Adding Items to an Existing JList Component
Here is an example: JList nameList = new JList(); String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; nameList.setListData(names);
34
III.8 Multiple Selection Lists
Single Interval Selection Mode The getSelectedValue method Returns the first item in the selected interval. The getSelectedIndex method Returns the index of the first item in the selected interval. The getSelectedValues method Returns an array of objects. The array will hold the items in the selected interval. The getSelectedIndices method Returns an array of int values. The values in the array will be the indices of all the selected items in the list.
35
III.8 Multiple Selection Lists
Multiple Interval Selection Mode The getSelectedValue method Returns the first item in the selected interval. The getSelectedIndex method Returns the index of the first item in the selected interval. The getSelectedValues method Returns an array of objects containing the items that are selected. The getSelectedIndices method Returns an array of int values containing the indices of all the selected items in the list.
36
III.8 Multiple Selection Lists
In this example, the user can select some items from the top list, click the Get Selection button, and the selected items will be displayed in the bottom list.
37
Create a class that extends the JFrame class
BorderLayout manager A panel to hold a list of months In the North region of the content pane A JList component A list of months Multiple interval selection mode Visible row count: 6 Add a scroll bar to the list
38
A panel to hold a list selected month
In the Center region of the content pane A JList component A list of selected months Visible row count: 6 Add a scroll bar to the list
39
A panel to hold a button In the South region of the content pane A JButton component “Get Selection” Add an action listener to the button
40
Create a list selection listener class
A private inner class Implements the ActionListener interface actionPerformed method Get the selected months of the list of months Set the selected months to the list of selected months
45
IV. Combo Boxes Combo Boxes Responding to Combo Box Events
Retrieving the Selected Item Editable Combo Boxes
46
IV.1 Combo Boxes A combo box presents a list of items that the user may select from. A combo box presents its items in a drop-down list. A combo box is a combination of a button and a list. The combo box initially appears as a button that displays the selected item. When the user clicks on the button, the list of items drops down. The user may select another item from the list.
47
IV.1 Combo Boxes We use JComboBox to create a combo box.
We pass an array of objects that are to be displayed as the items in the drop-down list to the constructor. String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JComboBox nameBox = new JComboBox(names);
48
IV.2 Responding to Combo Box Events
When an item in a JCompoBox is selected, it generates an action event. We handle action events with an action event listener class, which implements the ActionListener interface and must have an actionPerformed method. When the user selects an item in a combo box, the combo box executes its action event listener's actionPerformed method, passing an ActionEvent object as an argument.
49
IV.3 Retrieving the Selected Item
There are two methods in the JComboBox to determine which item in a combo box is currently selected. getSelectedItem returns a reference to the item that is currently selected. String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JComboBox nameBox = new JcomboBox(names); String selectedItem; selectedItem = (String)nameBox.getSelectedItem();
50
IV.3 Retrieving the Selected Item
getSelectedIndex returns the index of the selected item. The items that are stored in a combo box are numbered with indices that start at 0. String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JComboBox nameBox = new JcomboBox(names); int index; String selectedItem; index = nameBox.getSelectedIndex(); selectedItem = names[index];
51
IV.3 Retrieving the Selected Item
An example: When the user selects an item in the combo box of coffee types, the selected coffee will be displayed in a read-only text field.
52
Create a class that extends the JFrame class
BorderLayout manager A panel to hold a combo box of coffee types In the center region of the content pane A JComboBox component A list of coffee types Add an object of an action listener class
53
A panel to hold a selected coffee type
In the south region of the content pane A JTextField component Width: 10 Uneditable text field A JLabel component “You Selected:”
54
Create an action listener listener class
A private inner class Implements the ActionListener interface actionPerformed method Get the selected coffee type of the combo box Set the selected coffee type to the text field
58
IV.4 Editable Combo Boxes
There are two types of combo boxes: Uneditable combo boxes: An uneditable combo box combines a button with a list. Allow the user to select items from the list only. The default type of combo boxes Editable combo boxes: An editable combo box combines a text field and a list. Allow the user to select items from the list. The user may type input into the text field.
59
IV.4 Editable Combo Boxes
We make a combo box editable by calling the setEditable method, passing true as the argument. String[] names = {“Bill”, “Geri”, “Greg”, “Jean”, “Kirk”, “Phillip”, “Susan” }; JComboBox nameBox = new JComboBox(names); nameBox.setEditable(true); The text field displays the item that is currently selected. The user may type a value into the text field. The user is not restricted to the values that appear in the list, and may type any input into the text field.
60
IV.4 Editable Combo Boxes
The editable combo box initially appears as a text field that displays the selected item. A small button with an arrow appears next to the text field. Alternatively, the user may type input into the text field. The user may type a value that does not appear in the list.
61
IV.4 Editable Combo Boxes
To retrieve the selected item: getSelectedValue: returns a reference to the item that is currently selected. This method returns the item that appears in the combo box's text field, so it may or may not be an item that appears in the combo box's list. getSelectedIndex: returns the index of the selected item. If the user has entered a value in the text field that does not appear in the list, this method will return -1.
62
V. Displaying Images in Labels and Buttons
Images may be displayed in labels and buttons. In this window The label displays a smiley face image and text. The button displays a smiley face image and text.
63
V. Displaying Images in Labels and Buttons
To display an image, first you create an instance of the ImageIcon class, which can read the contents of an image file. The constructor accepts a String argument that is the name of an image. The support file types are JPEG, GIF and PNG. ImageIcon image = new ImageIcon(“Smiley.gif”); ImageIcan image = new ImageIcon(“c:\\Images\\Smiley.gif”);
64
V. Displaying Images in Labels and Buttons
Next, you can display the image in a label by passing the ImageIcon object as an argument to the JLabel constructor. JLabel(Icon image) The argument image can be an ImageIcon object or any object that implements the Icon interface. ImageIcon image = new ImageIcon(“Smiley.gif”); JLabel label = new JLabel(image); This creates a label with an image, but no text.
65
V. Displaying Images in Labels and Buttons
To create a label with both an image and text ImageIcon image = new ImageIcon(“Smiley.gif”); JLabel label = new JLabel(“Have a nice day”); label.setIcon(image); This text will be displayed to the right of the image. The JLabel class also has the following constructor: JLable(String text, Icon image, int horizontalAligment) ImageIcon image = new ImageIcon(“Smiley.gif”); JLabel label = new JLabel(“Have a nice day”, image, SwingConstants.RIGHT);
66
V. Displaying Images in Labels and Buttons
The process of creating a button with an image is similar to that of creating a label with an image. Use an ImageIcon object to read the image file. Pass the ImageIcon object as an argument to the JButton constructor. To create a button with an image and no text: ImageIcon image = new ImageIcon(“Smiley.gif”); JButton button = new Jbutton(image);
67
V. Displaying Images in Labels and Buttons
To create a button with an image and text: ImageIcon image = new ImageIcon(“Smiley.gif”); JButton button = new Jbutton(“Have a nice day!”, image); To add an image to an existing button: ImageIcon image = new ImageIcon(“Smiley.gif”); JButton button = new Jbutton(“Have a nice day!”); button.addIcon(image);
68
V. Displaying Images in Labels and Buttons
An example: Display a digital photograph in a label when the user clicks a button.
72
VI. Mnemonics and Tool Tips
A mnemonic is a key on the keyboard that you press in combination with the Alt key to access a component quickly. Shortcut key Hot key When we assign a mnemonic to a button, the user can click the button by holding Alt key and press the mnemonic key.
73
VI. Mnemonics and Tool Tips
To assign a mnemonic to a component: void setMnemonic(int key) key: is an integer code that represents the key you wish to assign as a mnemonic. KeyEvent class has predefined constants KeyEvent.VK_x x is a key on the keyboard. KeyEvent.VK_A : The key A KeyEvent.VK_X : The key X KeyEvent.VK_EXCLAMATION_MARK : The key ! KeyEvent.VK_AMPERSAND : The key &
74
VI. Mnemonics and Tool Tips
A button with the text “Exit” JButton exitButton = new Jbutton(“Exit”); exitButton.setMnemonic(KeyEvent.VK_X); If the letter is chosen as the mnemonic, the first occurrence of that letter will appear underlined.
75
VI. Mnemonics and Tool Tips
Create three radio buttons and assign mnemonics JRadioButton rb1 = new JRadioButton(“Breakfast”); rb1.setMnemonic(KeyEvent.VK_B); JRadioButton rb1 = new JRadioButton(“Lunch”); rb1.setMnemonic(KeyEvent.VK_L); JRadioButton rb1 = new JRadioButton(“Dinner”); rb1.setMnemonic(KeyEvent.VK_D);
76
VI. Mnemonics and Tool Tips
Create three check boxes and assign mnemonics JCheckBox cb1 = new JcheckBox(“Monday”); cb1.setMnemonic(KeyEvent.VK_M); JCheckBox cb2 = new JcheckBox(“Wednesday”); cb2.setMnemonic(KeyEvent.VK_W); JCheckBox cb3 = new JcheckBox(“Friday”); cb3.setMnemonic(KeyEvent.VK_F);
77
VI. Mnemonics and Tool Tips
A tool tip is a text that is displayed in a small box when the user holds the mouse cursor over a component. The box usually gives a short description of what the component does. Most GUI applications use tool tips as a way of providing immediate and concise help to the user.
78
VI. Mnemonics and Tool Tips
To assign a tool tip to a component: void setToolTipText(String text) JButton exitButton = new Jbutton(“Exit”); exitButton.setToolTipText(“Click here to exit.”);
79
VII. File Choosers and Color Choosers
Java provides components that equip your applications with standard dialog boxes for opening files, saving files, and selecting colors File Choosers A file chooser is a specialized dialog box that allows the user to browse for a file and to select it In order to open a file chooser dialog box: JFileChooser
80
VII. File Choosers and Color Choosers
To create an instance of the JFileChooser class: JFileChooser() Using the default directory as the starting point for all of its dialog boxes JFileChooser(String path) path : is a valid path. This path will be the starting point for the object's dialog boxes.
81
VII. File Choosers and Color Choosers
A JFileChooser object can display two types of predefined dialog boxes: An open file dialog box A save file dialog box To display a file chooser dialog box, use the showOpenDialog method. int showOpenDialog(Component parent) parent: null: the dialog box is normally centered in the screen A reference to a component (such as JFrame): the dialog box is displayed over the component.
82
VII. File Choosers and Color Choosers
To display a save file dialog box, use the showSaveDialog method. int showSaveDialog(Component parent)
83
VII. File Choosers and Color Choosers
The return value of the showOpenDialog and showSaveDialog: The return value indicates the action taken by the user to close the dialog box: JFileChooser.CANCEL_OPTION: This return value indicates that the user clicked the Cancel button. JFileChooser.APPROVE_OPTION: This return value indicates that the user clicked the Open or Save button. JFileChooser.ERROR_OPTION: This return value indicates that an error occurred, or the user clicked the standard close button.
84
VII. File Choosers and Color Choosers
If the user selected a file, we can use the getSelectedFile method to determine the selected file. File getSelectedFile() This method returns a File object which contains data about the selected file. Call the getPath method of the File object to get the path and file name as a String.
85
VII. File Choosers and Color Choosers
JFileChooser fileChooser = new JfileChooser(); int status = fileChooser.showOpenDialog(null); if(status == JFileChooser.APPROVE_OPTION){ File selectedFile = fileChooser.getSelectedFile(); String filename = selectedFile.getPath(); }
86
VII. File Choosers and Color Choosers
A color chooser is a specialized dialog that allows the user to select a color from a predefined palette of colors. In order to open a color chooser, use the JColorChooser class. Do not create an instance of this class. Use the static method named showDialog
87
VII. File Choosers and Color Choosers
Color showDialog(Component parent, String title, Color initial) parent: can be either null or a reference to a component title: is the text that is displayed in the dialog box's title bar. initial: is the color that appears initially selected in the dialog box. This method returns the color selected by the user.
88
VII. File Choosers and Color Choosers
JPanel panel = new Panel(); Color selectedColor; selectedColor = JcolorChooser.showDialog(null, “Select a Background Color”, Color.BLUE); panel.setBackgroundColor(selectedColor);
89
VIII. Menus Java provides classes for creating systems of drop-down menus. Menus can contain: Menu items Check Box menu items Radio button menu items Submenus
90
VIII. Menus Menu Bar Menu Menu Item Check Box Menu Item Submenu
Radio Button Menu Item Separator Bar
91
VIII. Menus JMenuBar: To create a menu bar. JMenu: To create a menu.
JMenuItem: To create a regular menu item. JCheckBoxMenuItem: To create a check box menu item JRadioButtonMenuItem: To create a radio button menu item
92
An Example
99
IX. More about Text Components
Text Areas Text field A text field is a component that allows the user to enter a single line of text. Using the JTextField class A text area is like a text field that can accept multiple lines of input. Using the JTextArea class
100
IX. More about Text Components
Constructors JTextArea(int rows, int columns) JTextArea(String text, int rows, int columns) rows: is the number of rows or lines that the text area is to display. columns: is the number of columns or characters that are to be displayed. text: is a string that the text area will initially display.
101
IX. More about Text Components
To get the text of a JTextArea component JTextArea text = new JTextArea(20, 40); … String userText = text.getText(); To set the text to a JTextArea component JTextArea text = new JTextArea(20, 40); String info = “Data ...”; … text.setText(info);
102
IX. More about Text Components
JTextArea components do not automatically display scroll bars. To display a scroll bar, add it to the scroll pane. JTextArea text = new JTextArea(20, 40); JScrollPane scrollPane = new ScrollPane(text); The JScrollPane object displays both vertical and horizontal scroll bars on a text area. By default, the scroll bars are not displayed until they are needed.
103
IX. More about Text Components
setHorizontalScrollBarPolicy(int pol): JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED A horizontal scroll bar is displayed only when there is not enough horizontal space to display the text contained in the text area. JScrollPane.HORIZONTAL_SCROLLBAR_NEVE R This prevents a horizontal scroll bar from being displayed in the text area. JScrollPane.HORIZONTAL_SCROLLBAR_ALWA YS A horizontal scroll bar is always displayed.
104
IX. More about Text Components
setVerticalScrollBarPolicy(int pol): JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED A vertical scroll bar is displayed only when there is not enough vertical space to display the text contained in the text area. JScrollPane.VERTICAL_SCROLLBAR_NEVER This prevents a vertical scroll bar from being displayed in the text area. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS A vertical scroll bar is always displayed.
105
IX. More about Text Components
By default, JTextArea components do not perform line wrapping. When the text is entered into the component and the end of a line is reached, the text does not wrap around to the next line. To perform line wrapping: setLineWrap(true): To turn on line wrapping setLineWrap(false): To turn off line wrapping
106
IX. More about Text Components
There are two styles of line wrapping: Word wrapping: The line breaks always occur between words, never in the middle of a word. setWrappingStyleWord(true) Character wrapping: Lines are broken between characters. This means that lines can be broken in the middle of a word. This is the default style. setWrappingStyleWord(false)
107
IX. More about Text Components
Fonts The appearance of a component's text is determined by Font Name of the typeface Style Plain, Bold, Italic Size The size of the text in points.
108
IX. More about Text Components
To change the appearance of the text setFont(Font appearance) The Font class constructor Font(String fontName, int style, int size) fontName: Java guarantess that you will have Dialog, DialogInput, Monospaced, SansSerif, Serif. style: Font.PLAIN, Font.BOLD, Font.ITALIC size: The size of the text in points. 72 points per inch
109
IX. More about Text Components
Examples: JLabel label1 = new JLabel(“Hello”); label1.setFont( new Font(“Serif”), Font.PLAIN, 24); JLabel label2 = new JLabel(“Hello”); label2.setFont( new Font(“Serif”), Font.BOLD, 12); JLabel label3 = new JLabel(“Hello”); label3.setFont(new Font(“Serif”), Font.BOLD + Font.ITALIC, 14);
110
X. Sliders A slider is a component that allows the user to adjust a number graphically within a range of values. Sliders, which are created from the JSlider class, display an image of a “slider knob” that can be dragged along a track. Sliders can be horizontally or vertically oriented. A slider is designed to represent a range of numeric values. At one end of the slider is the range's minimum value, and at the other end is the range's maximum value.
111
X. Sliders
112
X. Sliders The JSlider constructor: orientation: JSlider.HORIZONTAL
JSlider(int orientation, int minValue, int maxValue, int initialValue) orientation: JSlider.HORIZONTAL JSlider.VERTICAL minValue: The minimum value of the slider's range maxValue: The maximum value of the slider's range initialValue: The initial value of the slider, which determines the initial position of the slider's knob.
113
X. Sliders We can set the major and minor tick mark spacing.
setMajorTickSpacing(int interval) interval: The interval of the major tick marks setMinorTickSpacing(int interval) interval: The interval of the minor tick marks By default, tick marks are not displayed. setPaintTicks(true): To display tick marks setPaintTicks(false): Not to display tick marks
114
X. Sliders By default, numeric labels are not displayed on the slider component. setPaintLabels(true): to display numeric labels setPaintLabels(false): not to display labels When the knob's position is moved, the slider component generates a change event. To handle the change event, we must write a change listener class. It must implement the ChangeListener interface. It must have a method named stateChanged. This method takes an argument of the ChangedEvent.
115
X. Sliders To retrieve the current value stored in a JSlider component, use the getValue method. This method returns the slider's value as an int.
116
X. Sliders An example: Write a temperature converter program that displays a window with a slider component. The user can convert the centigrade temperatures from 0 through 100 to Fahrenheit by moving the slider.
120
XI. Look and Feel Most operating systems' GUIs have their own unique appearance and style conventions. If a Windows user switches to a Macintosh, UNIX, or Linux system: The difference in the way the GUIs on each system appear.
121
XI. Look and Feel The appearance of a particular system's GUI is known as its look and feel. A GUI application's appearance is determined by its look and feel. Java allows to select the look and feel of a GUI application.
122
XI. Look and Feel Look and feel choices
The default look and feel for Java is called Ocean. Metal Ocean is actually a special theme of the Metal look and feel Motif Windows Currently the Windows look and feel is available only on computers running Windows system.
123
XI. Look and Feel Metal Look and Feel Motif Look and Feel
Windows Look and Feel
124
XI. Look and Feel To change an application's look and feel:
Set the look and feel Update all created GUI components
125
UIManager.setLookAndFeel(String className)
XI. Look and Feel To set the look and feel Call this static method UIManager.setLookAndFeel(String className) className: Look and feel class name “javax.swing.plaf.metal.MetalLookAndFeel” : Metal “com.sun.java.swing.plaf.motif.MotifLookAndFeel” : Motif “com.sun.java.swing.plaf.windows.WindowsLookAndFeel” : Windows This method throws a number of exceptions ClassNotFoundException InstantiationException IllegalAccessException UnsupportedLookAndFeelException
126
SwingUtilities.updateComponentTreeUI
XI. Look and Feel To update created GUI components Call this static method SwingUtilities.updateComponentTreeUI Pass a reference to the component to be updated. Example: Using the Motif look and feel in the temperature converter program.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.