More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Eric Stokes, Matt Behling. GridbagLayout CardLayout SpringLayout.
Final Review (lectures 8-16) TA: Truman
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Chapter 4 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
7-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : User Interface Components with.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Chapter 141 More Swing Chapter Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Chapter 14- More Swing, Better looking applications.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
Building a Swing Interface
A First Look at GUI Applications
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
Constructors, GUI’s(Using Swing) and ActionListner
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University

Announcements There will be no classes or labs next week There will be only an overview recitation, so make sure that you attend it since it will be a very good practice for the final exam. Also we will make recitation evaluation on that overview recitation. Due date for project 8: 04/23/2008

Menus Allows the user to navigate the GUI easily 3 key words to remember: Menu Bar, Menu, Menu Item One can place a menu bar almost anywhere. But if setMenuBar method of JFrame is used it is placed at the top by default. Menus can be added to a menu bar which can then have menu items or other menus.

Menus JMenu is derived from JMenuItem (i.e. JMenu is also a JMenuItem)‏ So we can add a JMenu to another JMenu as if we are adding a JmenuItem Since JButton is also derived from JAbstractButton, JMenuItem have many similar methods with JButton.

Menus //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. menu = new JMenu("A Menu"); menuBar.add(menu); //a submenu menu.addSeparator(); submenu = new JMenu("A submenu"); menuItem = new JMenuItem("An item in the submenu"); submenu.add(menuItem); menuItem = new JMenuItem("Another item"); submenu.add(menuItem); menu.add(submenu);... frame.setJMenuBar(theJMenuBar); Example taken from herehere

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); This is useful if you are implementing actionPerformed in the same actionListener for all the components in your program but it is not recommended. (i.e. implement a separate actionListener just for the menu bar.)‏

Placing an Icon and a String on a Label (or Button)‏ Example JButton helloButton = new JButton(“Hello”); ImageIcon dukeWavingIcon = newImageIcon(“dukeWaving.gif”); helloButton.setIcon(dukeWavingIcon); As you can see text “Hello” is by default centered vertically and to the right of the icon.

JScrollPane JScrollPane is used to limit the visible part of a component, text or an image in GUI A JViewport instance is used to handle the visible portion of JScrollPane

Making a JPanel Scrollable JPanel tmp = new JPanel(); //checkBoxes is an ArrayList that holds all //the checkboxes that we will display for (JCheckBox ch : checkBoxes){ tmp.add(ch); } JScrollPane listScroller = new JScrollPane(tmp); listScroller.setPreferredSize(new Dimension(250, 80)); mainPanel.add(listScroller);

JScrollPane If the frame is enlarged enough, scroll bars will disappear in the default behavior. You can change that behavior by using scroll pane's scroll bar policy. Two of the constructors let you set the policy or you can use mutator methods of the class. JScrollPane(Component, int, int)‏ JScrollPane(int, int)‏ setHorizontalScrollBarPolicy (int)‏ setVerticalScrollBarPolicy (int)‏

JScrollPane Policies VERTICAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_AS_NEEDED The default. The scroll bar appears when the viewport is smaller than the client and disappears when the viewport is larger than the client. VERTICAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_NEVER HORIZONTAL_SCROLLBAR_NEVER Always display the scroll bar. The knob disappears if the viewport is large enough to show the whole client. Never display the scroll bar. Use this option if you don't want the user to directly control what part of the client is shown, or if you want them to use only non-scroll-bar techniques (such as dragging).

CardLayout Card Layout is very useful if two or more of your components (usually JPanels) will use the same display place. Component that shares the same display space is similar to playing cards in a deck. That is why this layout is called CardLayout.

CardLayout public void addComponentToPane(Container pane) { JPanel comboBoxPane = new Jpanel()‏ //BUTTONPANEL = "Card with JButtons" //TEXTPANEL = "Card with JTextField" String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb); //Create the "cards". JPanel card1 = new JPanel(); card1.add(new JButton("Button 1")); card1.add(new JButton("Button 2")); card1.add(new JButton("Button 3")); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)); //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER); } public void itemStateChanged(ItemEvent evt) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)evt.getItem()); } Example taken from herehere

CardLayout Method Purpose first (Container parent) Flips to the first card of the container. next (Container parent) Flips to the next card of the container. If the currently visible card is the last one, this method flips to the first card in the layout. previous (Container parent) Flips to the previous card of the container. If the currently visible card is the first one, this method flips to the last card in the layout. last (Container parent) Flips to the last card of the container. show (Container parent, String name) Flips to the component that was added to this layout with the specified name, using the addLayoutComponent method.

WindowListener The WindowListener interface makes the window itself the listener just as the ActionListener interface makes a window a button listener. There are 7 methods in WindowListener interface. Remember that we need to implement all of them if we want to use WindowListener interface. But you will be able to use the methods of your program inside the methods of WindowListener interface since they are in the same class. For instance you may want to close a PrintWriter instance when the user clicks the close button.

Methods in the WindowListener Interface

WindowListener vs. WindowAdapter WindowAdapter implements interface WindowListener by giving every method an empty body. So you won't have to implement all the 7 methods of WindowListener. If you want to handle the window inside of the class that is drived from JFrame, you need to implement WindowListener. You cannot use WindowAdapter. (i.e. A class cannot extend two classes)‏

Window Closing 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. So, first add setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSING); to the constructor. You may want to use setDefaultCloseOperation (Jframe.EXIT_ON_CLOSE); But in this case you won't be able to take extra actions for closing (i.e. Popping up a confirmation window on window closing.)‏

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.

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. Method pack causes the window to be resized to an approximation of the “preferred size.” Method repaint repaints the window. If your program uses the same display space for different set of components each time, CardLayout should be used instead of using setVisible(), validate(), and repaint() each time.

Quiz In what situation do you prefer using WindowListener over WindowAdapter?