More Swing. Chapter 14. Chapter 14 1

Size: px
Start display at page:

Download "More Swing. Chapter 14. Chapter 14 1"

Transcription

1 More Swing Chapter 14 Chapter 14 1

2 Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the Box class learn about inner classes learn about the WindowListener interface learn how to change GUI components to visible or invisible Chapter 14 2

3 Outline Menus Making GUIs Pretty and More Functional More Layout Managers Inner Classes More on Events and Listeners The Swing Class Hierarchy Reconsidered Chapter 14 3

4 Menus: Outline Programming Example: A GUI with a Menu Menu Bars, Menus, and Menu Items Nested Menus Chapter 14 4

5 Programming Example: A GUI with a Menu class MemoGUI Chapter 14 5

6 Programming Example: A GUI with a Menu, cont. class MemoGUI, cont. Chapter 14 6

7 Programming Example: A GUI with a Menu, cont. Chapter 14 7

8 Menu Bars, Menus, and Menu Items You add menus using three Swing classes: JMenuBar JMenu JMenuItem JMenuItems are placed in a JMenu, and a JMenu typically is placed in a JMenuBar. By default, an object of class JMenuItem is identified by the string that labels it. Chapter 14 8

9 Menu Bars, Menus, and Menu Items, cont. Using method add, you can add as many JMenuItems as you wish to a menu. example JMenu_Name.add(JMenu_Item); The menu lists them in the order in which they are added. Listeners are added using JMenu_Item_Name.addActionListener (Action_Listener); Chapter 14 9

10 Menu Bars, Menus, and Menu Items, cont. Method actionperformed is defined for menu items the same way it is defined for buttons. The menu in our example includes an additional entry labeled Exit. Chapter 14 10

11 Menu Bars A menu bar is a container for a menu. Typically it is placed near the top of a windowing interface. Menus are added to the menu bar using JMenu_Bar_Name.add(JMenu_Name); A menu bar can be added to a JFrame using setjmenubar(jmenu_bar_name); Chapter 14 11

12 Menu Bars, cont. Alternatively, a menu bar can be added to the content pane of a JFrame or other container. Chapter 14 12

13 Setting the Action Command for a Menu Item If you do not wish to use the text for a JMenuItem as the default action command, you can set the action command using Menu_Item_Object.setActionCommand (Action_Command_String); Chapter 14 13

14 Nested Menus Class JMenu descends from class JMenuItem, so every JMenu object is also a JMenuItem object. Thus, a JMenu can be a menu item in another menu, permitting menus to be nested (cascading menus). Chapter 14 14

15 Making GUIs Pretty and More Functional: Outline Adding Icons The JScrollPane Class for Scroll Bars Adding Borders Chapter 14 15

16 Adding Icons Typically, an icon is simply a small picture. Labels, buttons, menu items, and other components can have icons. A label or button can have just a string, just an icon, both, or neither. A picture in almost any standard format can be used as the basis for an icon. Chapter 14 16

17 Converting a Picture to a Swing Icon You use class ImageIcon to convert a picture file to a Swing Icon. example ImageIcon dukewavingicon = new ImageIcon( duke_waving.gif ); You can use a relative or complete path name to specify the picture file. Chapter 14 17

18 Adding an Icon to a Label and a Button To produce a button with just an icon on it, you use JButton dukebutton = new JButton(dukeWavingIcon); setactioncommand should be used explicitly to give the button an action command. Chapter 14 18

19 Placing an Icon and a String on a Label (or Button) example JButton hellobutton = new JButton( Hello ); ImageIcon dukewavingicon = new ImageIcon( dukewaving.gif ); hellobutton.seticon(dukewavingicon); Chapter 14 19

20 Placing an Icon and a String on a Label (or Button), cont. class IconDemo Chapter 14 20

21 Placing an Icon and a String on a Label (or Button), cont. Chapter 14 21

22 Some Methods in the Classes JButton and JLabel to create a button or label with no text and no icon public JButton() public JLabel() to create a button or label with text public JButton(String text) public JLabel(String text) Chapter 14 22

23 Some Methods in the Classes JButton and JLabel, cont. to create a button or label with an icon public JButton(ImageIcon Picture) public JLabel(ImageIcon Picture) to create a button or label with both text and an icon public JButton(String text, ImageIcon Picture) public JLabel(String text, ImageIcon Picture) Chapter 14 23

24 Some Methods in the Classes JButton and JLabel, cont. to make text the text on the already created button or label public void settext(string text) to make picture the icon on the already created button or label public void seticon(imageicon picture) Chapter 14 24

25 Some Methods in the Classes JButton and JLabel, cont. to set the size of the margin (in pixels) around the text and icon in the button (but not the label) public void setmargin(insets margin) or public void setmargin (new Insets(int top, int left, int bottom, int right)) Chapter 14 25

26 Some Methods in the Classes JButton and JLabel, cont. to set the preferred size (in pixels) of the button or label public void setpreferredsize( Dimension(preferredSize)) or public void setpreferredsize( new Dimension(int width, int height)) Chapter 14 26

27 Some Methods in the Classes JButton and JLabel, cont. to set the maximum size (in pixels) of the button or label public void setmaximumsize( Dimension(maximumSize)) or public void setmaximumsize( new Dimension(int width, int height)) Chapter 14 27

28 Some Methods in the Classes JButton and JLabel, cont. to set the minimum size (in pixels) of the button or label public void setminimumsize( Dimension(minimumSize)) or public void setminimumsize( new Dimension(int width, int height)) Chapter 14 28

29 Some Methods in the Classes JButton and JLabel, cont. to set the vertical position of the text relative to the icon public void setverticaltextposition (int textposition) where textposition is one of the constants SwingConstants.TOP SwingConstants.CENTER (default) SwingContants.BOTTOM Chapter 14 29

30 Some Methods in the Classes JButton and JLabel, cont. to set the horizontal position of the text relative to the icon public void sethorizontaltextposition (int textposition) where textposition is one of the constants SwingConstants.RIGHT (default) SwingConstants.LEFT Chapter 14 30

31 Resizing Buttons The methods for setting the preferred, maximum, and minimum sizes are only recommendations to the layout manager. An image may be clipped (NOT resized) if the icon is too big. Chapter 14 31

32 Classes Dimension and Inset Objects of classes Dimension and Inset are used with buttons, labels, and other objects. The numbers are in pixels. constructors Insets (int top, int left, int bottom, int right) Dimension(int width, int height) Chapter 14 32

33 Classes Dimension and Inset, cont. examples abutton.setmargin(new Insets (10, 20, 10, 20)); alabel.setpreferredsize (new Dimension (20, 50)); Chapter 14 33

34 The JScrollPane Class for Scroll Bars When you create a text area, you specify the number of lines that are visible and the number of characters per line. example JTextArea the Text = new JTextArea(10,40;) It might be better not to limit the number of lines and the number of characters per line. Chapter 14 34

35 The JScrollPane Class for Scroll Bars, cont. This can be accommodated using scroll bars along the sides of the window or view port that shows only a selected portion of the text. The view port functions as a cut out over an unbounded document. Chapter 14 35

36 The JScrollPane Class for Scroll Bars, cont. Chapter 14 36

37 The JScrollPane Class for Scroll Bars, cont. Scroll bars can be provided using class JScrollPane. An object of class JScrollPane is essentially a view port with scroll bars. Chapter 14 37

38 The JScrollPane Class for Scroll Bars, cont. The text area is provided as an argument to the JScrollPane constructor. example JScrollPane scrolledtext = new JScrollPane(theText); A JScrollPane can be added to a container such as a JPanel or a JFrame. example textpanel.add(scrolledtext); Chapter 14 38

39 The JScrollPane Class for Scroll Bars, cont. class ScrollBarDemo Chapter 14 39

40 The JScrollPane Class for Scroll Bars, cont. Chapter 14 40

41 Scroll Bar Policy If you omit the invocation of the methods sethorizontalscrollbarpolicy and setverticalscrollbarpolicy, the scroll bars will be visible only when you need them. Chapter 14 41

42 Some Methods and Constants in Class JScrollBar to create a new JScrollPane for the objecttobescrolled public JScrollPane(Component objecttobescrolled) Chapter 14 42

43 Some Methods and Constants in Class JScrollBar, cont. To set the policy for showing the horizontal scroll bar public void sethorizontalscrollbarpolicy(int policy) where policy is one of JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS JScrollPane.HORIZONTAL_SCROLLBAR_NEVER JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED (default) Chapter 14 43

44 Some Methods and Constants in Class JScrollBar, cont. To set the policy for showing the vertical scroll bar public void setverticalscrollbarpolicy(int policy) where policy is one of JScrollPane.VERTICAL_SCROLLBAR_AL WAYS JScrollPane VERTICAL SCROLLBAR NE Chapter 14 44

45 Adding Borders A border is an area around a component that frames the component. You can add a border to any JComponent. A border can serve two purposes: to make a component more attractive to separate the component from other components Chapter 14 45

46 Adding Borders, cont. to use the border classes import javax.swing.border.* to provide a border JComponent.setBorder(Border_Object); Chapter 14 46

47 Adding Borders, cont. class BorderDemo Chapter 14 47

48 Adding Borders, cont. Chapter 14 48

49 Adding Borders, cont. You can place a border around any JComponent such as a JButton, a JLabel, a JPanel, or a JTextField. It is common to use an anonymous border object. example testbutton.setborder(new BevelBorder(BevelBorder.LOWERED)); Chapter 14 49

50 Some Border Classes to create a BevelBorder object public BevelBorder(int beveltype) where beveltype is one of BevelBorder.RAISED BevelBorder.LOWERED Chapter 14 50

51 Some Border Classes, cont. to create an EtchedBorder object public EtchedBorder(int etchtype, Color highlight, Color shadow) where etchtype is one of EtchedBorder.RAISED EtchedBorder.LOWERED to create an EtchedBorder object public EtchedBorder(Color highlight, Color shadow) Chapter 14 51

52 Some Border Classes, cont. to create an EmptyBorder object public EmptyBorder(int top, int left, int bottom, int right) to create a LineBorder object public LineBorder(Color thecolor, int thickness) to create LineBorder object with rounded corners public LineBorder(Color thecolor, int thickness, boolean roundedcorners) Chapter 14 52

53 Some Border Classes, cont. to create a MatteBorder object public MatteBorder(int top, int left, int bottom, int right, Color thecolor) to create a MatteBorder object with an icon public MatteBorder(int top, int left, Chapter 14 53

54 More Layout Managers: Outline The BoxLayout Manager Class Struts and Glue Setting the Spacing Between Components The Box Container Class The CardLayout Manager Class Chapter 14 54

55 The BoxLayout Manager Class The FlowLayout manager can produce a horizontal array, and the GridLayout manager with a single column can produce a vertical array. However, the BoxLayout manager and the Box container class are more powerful. A Box container is a panel-like class that uses the BoxLayout manager. Chapter 14 55

56 The BoxLayout Manager Class, cont. Chapter 14 56

57 The BoxLayout Manager Class, cont. We will consider two different programs that produce this GUI. Chapter 14 57

58 The BoxLayout Manager class BoxLayoutDemo Class, cont. Chapter 14 58

59 The BoxLayout Manager Class, cont. The constructor for the BoxLayout manager expects two arguments. The first argument is the container for which it is the layout manager. The second argument is one of two constants BoxLayout.X_AXIS BoxLayout.Y_AXIS Chapter 14 59

60 Struts and Glue Static methods in class Box produce invisible components that can be added to a container. These invisible components add space between visible components. Method createhorizontalstrut creates a strut which is an invisible component with a fixed horizontal size. Chapter 14 60

61 Struts and Glue, cont. A layout manager cannot change the vertical size of a horizontal strut. Method VerticalStrut creates a strut which is an invisible component with a fixed vertical size. A layout manager cannot change the horizontal size of a vertical strut. Chapter 14 61

62 Struts and Glue, cont. Like struts, glue components are invisible components. Unlike struts, glue components are not rigid. They are like wet glue and can be made larger or smaller by the layout manager. Struts and glue are best used with a BoxLayout manager. Chapter 14 62

63 Struts and Glue, cont. Glue components can be horizontal or vertical. to create glue components Component horizontalglue = Box.createHorizontalGlue(); Component verticalglue = Box.createVerticalGlue(); to add a glue component, use horizontalbox.add(horizontalglue); Chapter 14 63

64 Setting the Spacing Between Components Except for the BoxLayout manager, the layout managers we have discussed use the following methods: public void sethgap(int hgap) public void setvgap(int vgap) where hgap and vgap are expressed in pixels. Alternatively, you can separate components using an EmptyBorder with any layout manager. Chapter 14 64

65 The Box Container Class An object of class Box behaves like a panel that has a BoxLayout manager. Instead of the JPanels used in class BoxLayoutDemo, class BoxClassDemo uses Box containers. Chapter 14 65

66 The Box Container Class Instead of the JPanels used in class BoxLayoutDemo Chapter 14 66

67 The Box Container Class class BoxClassDemo uses Box containers. Chapter 14 67

68 The Box Container Class, cont. Objects of class Box are created using a static method. to create the horizontalbox object Box horizontalbox = Box.createHorizontalBox(); to create the verticalbox object Box verticalbox = Box.createVerticalBox(); Chapter 14 68

69 The Box Container Class, cont. Method setlayout is not used to determine the layout of a Box object. An object of class Box automatically uses the BoxLayout manager. Chapter 14 69

70 The Box Container Class, cont. equivalent statements Box horizontalbox = new Box (BoxLayout.X_AXIS); and Box horizontalbox = Box.createHorizontalBox(); Chapter 14 70

71 The Box Container Class, cont. more equivalent statements Box verticalbox = new Box (BoxLayout.Y_AXIS); and Box verticalbox = Box.createVerticalBox(); Chapter 14 71

72 The CardLayout Manager Class The CardLayout manager class can add a dynamic element to a Swing GUI. The CardLayout manager class provides a set of views you can change - somewhat like flipping through or selecting from a deck of cards. Any number of views can be added to a container, but only one at a time is viewable. Chapter 14 72

73 The CardLayout Manager Class, cont. Views can be selected in order or randomly. Chapter 14 73

74 The CardLayout Manager Class, cont. class CardLayoutDemo Chapter 14 74

75 The CardLayout Manager Class, cont. Chapter 14 75

76 The CardLayout Manager Class, cont. We did not use an anonymous variable deckpanel.setlayout(new CardLayout()); Instead we used dealer = new CardLayout(); deckpanel.setlayout(dealer); to permit us to change the displayed card and to permit us to refer to the CardLayout manager in more than one method. Chapter 14 76

77 The CardLayout Manager Class, cont. The first argument of method add names the component provided as the second argument. example deckpanel.add( start, startcardpanel); deckpanel.add( green, greencardpanel); deckpanel.add( red, redcardpanel); Chapter 14 77

78 The CardLayout Manager Class, cont. Two other methods, first and next, permit you to select a view. examples dealer.first(deckpanel); dealer.next(deckpanel); The container always starts with the first component on view. After the last component, next goes back to the first component. Chapter 14 78

79 Some Methods in the CardLayout Manager Class to display the first card in the container public void first (Container thecontainer); to display the last card in the container public void last (Container thecontainer); to display the next card public void next (Container thecontainer); Chapter 14 79

80 Some Methods in the CardLayout Manager Class, cont. to display the previous card public void previous(container thecontainer); to display the card that was added with cardname as its name public void show (Container thecontainer, String cardname); Chapter 14 80

81 Inner Classes An inner class is a class defined within another class. Often, inner classes are used as helping classes when programming with Swing. Typically, helping classes are declared private. Chapter 14 81

82 Helping Classes Swing windows typically use class WindowDestroyer to close a window. Class WindowDestroyer can be an inner class. Chapter 14 82

83 Helping Classes, cont. class InnerClassDemo Chapter 14 83

84 Helping Classes, cont. Chapter 14 84

85 Advantages of Inner Classes Because inner classes are defined within an outer class, they can make the outer class self-contained (or more self-contained). The methods in the inner class have access to all the instance variables and methods of the outer class, including the private methods and variables. This increases efficiency. Chapter 14 85

86 Advantages of Inner Classes, cont. Inner classes are used frequently as listeners to handle events fired by the outer class or by a component of the outer class. The name of the inner class is local to the class in which it is defined, making it possible to have another class with the same name defined outside the class in which it is defined. Chapter 14 86

87 Invoking Methods of the Outer Class When there is a method invocation in an inner class, but a method with that name exists only in the outer class, the method of the outer class is invoked (i.e. the calling object is the this of the outer class, not the this of the inner class). Chapter 14 87

88 More on Events and Listeners: Outline The WindowListener Interface More Details on Updating a GUI Chapter 14 88

89 The WindowListener Interface When we placed buttons on a window, we made the window itself the button-listener class. But, when we wanted a window listener to respond to window-closing events, we made a separate window-listener class named WindowDestroyer (or an inner class named InnerDestroyer). Chapter 14 89

90 The WindowListener Interface, cont. We can make the window itself the window listener. The WindowListener interface makes the window itself the listener just as the ActionListener interface makes a window a button listener. Chapter 14 90

91 The WindowListener Interface, cont. To make class ButtonDemo (pages 37-38) an action listener and a window listener, it would begin public class ButtonDemo extends JFrame implements ActionListener, Window Listener Unfortunately, interface WindowListener requires 7 methods to be implemented. Chapter 14 91

92 The WindowListener Interface, cont. These 7 methods are the first of 10 methods in class WindowAdapter. Nevertheless, window class which is derived from class JFrame and which implements the WindowListener interface makes it easy to call a method in the window class within the window listener class, since they are in the same class. Chapter 14 92

93 Methods in the WindowListener Interface Chapter 14 93

94 A Window Listener class WindowListenerDemo Chapter 14 94

95 A Window Listener, cont. Chapter 14 95

96 Method dispose Because the GUI class is its own window listener this.dispose(); is allowed in method windowclosing. Method dispose is a method in class JFame, and class WindowListenerDemo is derived from class JFrame (so it inherits method dispose). Chapter 14 96

97 Method dispose, cont. Method dispose releases any resources used by the window. The program does not end when method dispose is invoked. Chapter 14 97

98 WindowListener vs. WindowAdapter WindowAdapter is a convenient variant of WindowListener. WindowAdapter implements interface WindowListener by giving every method an empty body. Any class derived from WindowAdapter does not need to provide those empty definitions. Chapter 14 98

99 WindowListener vs. WindowAdapter, cont. But sometimes, you want a listener class to be derived from class JFrame, which prevents it from being derived from class WindowAdapter. Instead it can implement interface WindowListener. Chapter 14 99

100 Java Tip: Programming the Close-Window Button To program the close-window button of a JFrame to do something other than cause the window to go away or end the program, add setdefaultcloseoperation (WindowConstants.DO_NOTHING_ON_CLOSING); to the constructor. Chapter

101 Java Tip: Programming the Close-Window Button, cont. class CloseWindowDemo Chapter

102 Java Tip: Programming the Close-Window Button, cont. Chapter

103 Java Tip: Programming the Close-Window Button, cont. If you invoke setdefaultcloseoperation, by default the window disappears but the program does not end. Simply reprogramming method windowclosing does not cancel the default action. Chapter

104 Java Tip: Programming the Close-Window Button, cont. To prevent the window from going away, you must first reset the default action with method setdefaultcloseoperation. Further, the close-window button is reprogrammed by registering the inner class InnerDestroyer as the window listener. Chapter

105 Java Tip: Programming the Close-Window Button, cont. Class ConfirmWindow is an inner class of class CloseWindowDemo, making class CloseWindowDemo self-contained. Chapter

106 Java Tip: More About setdefaultcloseoperation constants you can use with method setdefaultcloseoperation to permit any desired action to be programmed in method windowclosing WindowConstants.DO_NOTHING_ON_CLOSE Chapter

107 Java Tip: More About setdefaultcloseoperation cont. to hide the frame after invoking any registered WindowListener objects WindowConstants.HIDE_ON_CLOSE (the default action of setdefaultcloseoperation is not invoked) Chapter

108 Java Tip: More About setdefaultcloseoperation, cont. to hide and dispose of the frame after invoking any registered WindowListener objects WindowConstants.DISPOSE_ON_CLOSE to exit the application using method System.exit JFrame.EXIT_ON_CLOSE Chapter

109 Java Tip: More About setdefaultcloseoperation, cont. to program most Swing GUIs to have no window listener and still get the correct action for the close-window button setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); Chapter

110 Programmng Example: Components with Changing Visibility class VisibilityDemo Chapter

111 Programmng Example: Components with Changing Visibility Chapter

112 Method validate Every container class has a method validate for updating the container. Method validate causes the container to lay out its components again and redisplay them. Some changes, such as adding components or changing visibility require an invocation of method validate. Chapter

113 More Details on Updating GUIs Most changes to a GUI windowing system are done automatically by the repaint manager. However, a change in the visibility of a component requires an invocation of method validate, as we have discussed. Methods pack and repaint are two other updating methods common in Swing code. Chapter

114 More Details on Updating GUIs, cont. Method pack causes the window to be resized to an approximation of the preferred size. Method repaint repaints the window. Chapter

115 The Swing Class Hierarchy Reconsidered: Outline Buttons, Menus, and Abstract Buttons Other Swing Classes and Methods Chapter

116 Buttons, Menus, and Abstract Buttons Class JButton and class JMenuItem both are derived from class AbstractButton, from which all of the basic properties and methods (other than constructors) of class JButton and class JMenuItem are inherited. Class AbstractButton inherits some of these methods from class JComponent. Chapter

117 Buttons, Menus, and Abstract Buttons Chapter

118 Buttons, Menus, and Abstract Buttons Because class JMenu is derived from class JMenuItem, a menu is also a menu item, permitting a JMenu to be added to another JMenu to make nest menus possible. Because class JMenuBar is derived from class JComponent, a JMenuBar can be added to a container. With a suitable layout manager, you can have several JMenuBars, placed almost anywhere. Chapter

119 Java Tip: More Constructors for Class JMenuItem to create a menu item with no text or icon (assuming settext or seticon will be used later) public JMenuItem() to create a menu item with the text on it public JMenuItem(String text) to create a menu item with the icon picture on it public JMenuItem(ImageIcon picture) Chapter

120 Java Tip: More Constructors for Class JMenuItem, cont. to create a menu item with both the text and the icon picture on it public JMenuItem (String text, ImageIcon picture) Chapter

121 Other Swing Classes and Methods In Chapter 12 and in this chapter, you have learned a lot about Swing. However, a typically book on Swing is longer than the book for this course, and still doesn t cover all of Swing. Most of the classes, methods, and facilities that should be in Swing probably are in Swing already. Chapter

122 Summary You have learned to add menus, icons, borders, and scroll bars to GUIs. You have learned to use the BoxLayout manager and the Box class. You have learned about inner classes. You have learned about the WindowListener interface. You have learned how to change GUI components to visible or invisible. Chapter

Chapter 14. More Swing

Chapter 14. More Swing Chapter 14 More Swing Menus Making GUIs Pretty (and More Functional) Box Containers and Box Layout Managers More on Events and Listeners Another Look at the Swing Class Hierarchy Chapter 14 Java: an Introduction

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

More Swing. CS180 Recitation 12/(04,05)/08

More Swing. CS180 Recitation 12/(04,05)/08 More Swing CS180 Recitation 12/(04,05)/08 Announcements No lecture/labs next week Recitations and evening consulting hours will be held as usual. Debbie's study group on tuesday and office hours on thursday

More information

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7 PROGRAMMING DESIGN USING JAVA (ITT 303) Graphical User Interface Unit 7 Learning Objectives At the end of this unit students should be able to: Build graphical user interfaces Create and manipulate buttons,

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Swing II Window Listeners Icons and Scroll Bars The Graphics Class Colors 1044

Swing II Window Listeners Icons and Scroll Bars The Graphics Class Colors 1044 18.1 Window Listeners 1002 Example: A Window Listener Inner Class 1004 The dispose Method 1008 The WindowAdapter Class 1009 18.2 Icons and Scroll Bars 1010 Icons 1010 Scroll Bars 1017 Example: Components

More information

Swing II CHAPTER WINDOW LISTENERS 1034 Example: A Window Listener Inner Class 1036 The dispose Method 1040 The WindowAdapter Class 1041

Swing II CHAPTER WINDOW LISTENERS 1034 Example: A Window Listener Inner Class 1036 The dispose Method 1040 The WindowAdapter Class 1041 CHAPTER 19 Swing II 19.1 WINDOW LISTENERS 1034 Example: A Window Listener Inner Class 1036 The dispose Method 1040 The WindowAdapter Class 1041 19.2 ICONS AND SCROLL BARS 1042 Icons 1042 Scroll Bars 1049

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs What is Widget Layout? Laying Out Components Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and position Main problem: what if

More information

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

More information

Building Graphical User Interfaces. GUI Principles

Building Graphical User Interfaces. GUI Principles Building Graphical User Interfaces 4.1 GUI Principles Components: GUI building blocks Buttons, menus, sliders, etc. Layout: arranging components to form a usable GUI Using layout managers. Events: reacting

More information

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling Overview Building Graphical User Interfaces Constructing GUIs Interface components GUI layout Event handling 4.1 GUI Principles AWT and Swing Components: GUI building blocks. Buttons, menus, sliders, etc.

More information

Chapter 13 Lab Advanced GUI Applications

Chapter 13 Lab Advanced GUI Applications Gaddis_516907_Java 4/10/07 2:10 PM Page 113 Chapter 13 Lab Advanced GUI Applications Objectives Be able to add a menu to the menu bar Be able to use nested menus Be able to add scroll bars, giving the

More information

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

CS 251 Intermediate Programming GUIs: Components and Layout

CS 251 Intermediate Programming GUIs: Components and Layout CS 251 Intermediate Programming GUIs: Components and Layout Brooke Chenoweth University of New Mexico Fall 2017 import javax. swing.*; Hello GUI public class HelloGUI extends JFrame { public HelloGUI ()

More information

Laying Out Components. What is Widget Layout?

Laying Out Components. What is Widget Layout? Laying Out Components Interior Design for GUIs What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and position

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) Layout Managment 1 Hello World Often have a static method: createandshowgui() Invoked by main calling invokelater private static void createandshowgui() { } JFrame frame

More information

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

Building Graphical User Interfaces. Overview

Building Graphical User Interfaces. Overview Building Graphical User Interfaces 4.1 Overview Constructing GUIs Interface components GUI layout Event handling 2 1 GUI Principles Components: GUI building blocks. Buttons, menus, sliders, etc. Layout:

More information

1005ICT Object Oriented Programming Lecture Notes

1005ICT Object Oriented Programming Lecture Notes 1005ICT Object Oriented Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 1 20 GUI Components and Events This section develops a program

More information

Layout. Dynamic layout, Swing and general layout strategies

Layout. Dynamic layout, Swing and general layout strategies Layout Dynamic layout, Swing and general layout strategies Two Interface Layout Tasks Designing a spatial layout of widgets in a container Adjusting that spatial layout when container is resized Both

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus Chapter 13 Lab Advanced GUI Applications Lab Objectives Be able to add a menu to the menu bar Be able to use nested menus Be able to add scroll bars, giving the user the option of when they will be seen.

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Chapter 17 Creating User Interfaces

Chapter 17 Creating User Interfaces Chapter 17 Creating User Interfaces 1 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work.

More information

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright GUI Basics Object Orientated Programming in Java Benjamin Kenwright Outline Essential Graphical User Interface (GUI) Concepts Libraries, Implementation, Mechanics,.. Abstract Windowing Toolkit (AWT) Java

More information

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068 Welcome to! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) Overview JAVA and GUIs: SWING Container, Components, Layouts Using SWING Streams and Files Text

More information

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Prototyping a Swing Interface with the Netbeans IDE GUI Editor Prototyping a Swing Interface with the Netbeans IDE GUI Editor Netbeans provides an environment for creating Java applications including a module for GUI design. Here we assume that we have some existing

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

Window Interfaces Using Swing. Chapter 12

Window Interfaces Using Swing. Chapter 12 Window Interfaces Using Swing 1 Reminders Project 7 due Nov 17 @ 10:30 pm Project 6 grades released: regrades due by next Friday (11-18-2005) at midnight 2 GUIs - Graphical User Interfaces Windowing systems

More information

Layout. Dynamic layout Layout design pattern Layout strategies

Layout. Dynamic layout Layout design pattern Layout strategies Layout Dynamic layout Layout design pattern Layout strategies 2.6 Layout 2 https://www.bostonglobe.com/ 2.6 Layout 3 Responsive vs. Adaptive Responsive: universal design reflows spatial layout to fit width

More information

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

Layout. Dynamic layout Layout design pattern Layout strategies. 2.6 Layout 2

Layout. Dynamic layout Layout design pattern Layout strategies. 2.6 Layout 2 Layout Dynamic layout Layout design pattern Layout strategies 2.6 Layout 2 Two Interface Layout Tasks 1. Designing a spatial layout of widgets in a container 2. Adjusting that spatial layout when container

More information

Event Driven Programming

Event Driven Programming Event Driven Programming Part 1 Introduction Chapter 12 CS 2334 University of Oklahoma Brian F. Veale 1 Graphical User Interfaces So far, we have only dealt with console-based programs Run from the console

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Chapter 5 Chapter 5 1 Auto-Boxing and Unboxing and Wrapper Classes Many Java library methods work with class objects only Do not accept primitives Use wrapper classes instead!

More information

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

Graphical User Interface (Part-3) Supplementary Material for CPSC 233 Graphical User Interface (Part-3) Supplementary Material for CPSC 233 Menu Bars, Menus, and Menu Items A menu is an object of the class JMenu A choice on a menu is called a menu item, and is an object

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components COSC User Interfaces Module 5 Laying Out Components What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame s content pane) Basic idea: each widget has a size and

More information

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout. COSC 3461: Module 5B Resizing a Window Widgets II What has changed? scrollbar added menu has wrapped toolbar modified (buttons lost) 2 What is Widget Layout? Size State Transitions Recall: each widget

More information

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place. Chapter 8 Java continued CS3283 - Hugh Anderson s notes. Page number: 263 ALERT MCQ test next week This time This place Closed book CS3283 - Hugh Anderson s notes. Page number: 264 ALERT Assignment #2

More information

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3 Chapter 8 Java continued MCQ test next week This time This place Closed book ALERT CS3283 - Hugh Anderson s notes. Page number: 263 CS3283 - Hugh Anderson s notes. Page number: 264 ALERT Last week Assignment

More information

Basicsof. JavaGUI and SWING

Basicsof. JavaGUI and SWING Basicsof programming3 JavaGUI and SWING GUI basics Basics of programming 3 BME IIT, Goldschmidt Balázs 2 GUI basics Mostly window-based applications Typically based on widgets small parts (buttons, scrollbars,

More information

Programming graphics

Programming graphics Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it

More information

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build

More information

Implementing Graphical User Interfaces

Implementing Graphical User Interfaces Chapter 6 Implementing Graphical User Interfaces 6.1 Introduction To see aggregation and inheritance in action, we implement a graphical user interface (GUI for short). This chapter is not about GUIs,

More information

Part 3: Graphical User Interface (GUI) & Java Applets

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

2IS45 Programming

2IS45 Programming Course Website Assignment Goals 2IS45 Programming http://www.win.tue.nl/~wsinswan/programmeren_2is45/ Rectangles Learn to use existing Abstract Data Types based on their contract (class Rectangle in Rectangle.

More information

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University Java Swing Recitation 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 8 is out Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm No classes,

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) Layout Managment 1 Hello World Often have a run method to create and show a GUI Invoked by main calling invokelater private void run() { } JFrame frame = new JFrame("HelloWorldSwing");

More information

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014 Introduction to GUIs Principles of Software Construction: Objects, Design, and Concurrency Jonathan Aldrich and Charlie Garrod Fall 2014 Slides copyright 2014 by Jonathan Aldrich, Charlie Garrod, Christian

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Summary Chapter 25 GUI Components: Part 2

Summary Chapter 25 GUI Components: Part 2 1040 Chapter 25 GUI Components: Part 2 ponent on the line. TheJTextField is added to the content pane with a call to our utility method addcomponent (declared at lines 79 83). MethodaddComponent takes

More information

Chapter 12 GUI Basics

Chapter 12 GUI Basics Chapter 12 GUI Basics 1 Creating GUI Objects // Create a button with text OK JButton jbtok = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblname = new JLabel("Enter your

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Java Graphics and GUIs 1 The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised 1/15/10 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField, JTextArea,

More information

Jonathan Aldrich Charlie Garrod

Jonathan Aldrich Charlie Garrod Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING PROFESSOR GODFREY MUGANDA 1. Learn to Generate Random Numbers The class Random in Java can be used to create objects of the class

More information

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the

More information

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Eclipsing Your IDE. Figure 1 The first Eclipse screen. Eclipsing Your IDE James W. Cooper I have been hearing about the Eclipse project for some months, and decided I had to take some time to play around with it. Eclipse is a development project (www.eclipse.org)

More information

Lecture 18 Java Graphics and GUIs

Lecture 18 Java Graphics and GUIs CSE 331 Software Design and Implementation The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction Lecture 18 Java Graphics and GUIs None

More information

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer,

More information

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1. GUI Applications The C implementation of Python comes with Tkinter for writing Graphical User Interfaces (GUIs). The GUI toolkit that you get automatically with Jython is Swing, which is included with

More information

BASICS OF GRAPHICAL APPS

BASICS OF GRAPHICAL APPS CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

8. Polymorphism and Inheritance

8. Polymorphism and Inheritance 8. Polymorphism and Inheritance Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe polymorphism and inheritance in general Define interfaces

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised March 2, 2017 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField,

More information

CSE 8B Intro to CS: Java

CSE 8B Intro to CS: Java CSE 8B Intro to CS: Java Winter, 2006 February 23 (Day 14) Menus Swing Event Handling Inner classes Instructor: Neil Rhodes JMenuBar container for menus Menus associated with a JFrame via JFrame.setMenuBar

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame.

More information

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics 1 Collections (from the Java tutorial)* A collection (sometimes called a container) is simply an object that

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline Sequential vs. Event-driven Programming Reacting to the user GUI Program Organization Let s digress briefly to examine the organization of our GUI programs We ll do this in stages, by examining three example

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau List of Slides 1 Title 2 Chapter 13: Graphical user interfaces 3 Chapter aims 4 Section 2: Example:Hello world with a GUI 5 Aim 6 Hello world with a GUI 7 Hello world with a GUI 8 Package: java.awt and

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing. COSC User Interfaces Module 3 Sequential vs. Event-driven Programming Example Programs DemoLargestConsole.java DemoLargestGUI.java Demo programs will be available on the course web page. GUI Program Organization

More information

Graphical User Interfaces in Java

Graphical User Interfaces in Java COMP16121 Graphical User Interfaces in Java COMP16121 Sean Bechhofer sean.bechhofer@manchester.ac.uk Why? With the dominance of windows-based systems, Graphical User Interfaces (GUIs) are everywhere. They

More information

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI CBOP3203 Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI components like button, text input, scroll bar and others.

More information

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn Java & Graphical User Interface II Wang Yang wyang AT njnet.edu.cn Outline Review of GUI (first part) What is Event Basic Elements of Event Programming Secret Weapon - Inner Class Full version of Event

More information

JAVA NOTES GRAPHICAL USER INTERFACES

JAVA NOTES GRAPHICAL USER INTERFACES 1 JAVA NOTES GRAPHICAL USER INTERFACES Terry Marris 24 June 2001 5 TEXT AREAS 5.1 LEARNING OUTCOMES By the end of this lesson the student should be able to understand how to get multi-line input from the

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming 1. Graphical User Interfaces OOP10 - M. Joldoş - T.U. Cluj 1 GUI A Graphical User Interface (GUI pronounced "goo-ee") presents a userfriendly mechanism for interacting with

More information

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class. Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space

More information

CS180 Recitation. More about Objects and Methods

CS180 Recitation. More about Objects and Methods CS180 Recitation More about Objects and Methods Announcements Project3 issues Output did not match sample output. Make sure your code compiles. Otherwise it cannot be graded. Pay close attention to file

More information

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

SELF-STUDY. Glossary

SELF-STUDY. Glossary SELF-STUDY 231 Glossary HTML (Hyper Text Markup Language - the language used to code web pages) tags used to embed an applet. abstract A class or method that is incompletely defined,

More information

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming GUI Design Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu /~spring Overview of Part 1 of the Course Demystifying

More information

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components

More information

Inheritance. Chapter 7. Chapter 7 1

Inheritance. Chapter 7. Chapter 7 1 Inheritance Chapter 7 Chapter 7 1 Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class

More information

Inheritance (cont) Abstract Classes

Inheritance (cont) Abstract Classes Inheritance (cont) Abstract Classes 1 Polymorphism inheritance allows you to define a base class that has fields and methods classes derived from the base class can use the public and protected base class

More information

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information