TAKE IT TO THE NTH An Introduction to Java Swing TAKE IT TO THE NTH Asif Habibullah Member of Technical Staff, Software Market Development Engineering In Introduction to Java Swing An Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller (MVC) Design Pattern • Rich clients for Web Services • Deploy via Java Web Start What is Java Swing? • Graphical User Interface (GUI) component toolkit. • Complete set of user-interface elements written entirely in Java. • Integrated into the Java 2 platform (1.2 or higher). • No need to download and install on Java 2 platform. • If running Java 1.1, you can download and install separately. • Part of Java Foundation Classes (JFC) Java Foundation Classes (JFC) The Java Foundation Classes (JFC) extend the original Abstract Window Toolkit (AWT) by adding a comprehensive set of graphical user interface class libraries • What makes up the JFC (features)? • Swing • Accessibility API • Java 2D API • Drag and Drop API • AWT • Internationalization What about AWT? Swing AWT • Lightweight components • Heavyweight components • Complex components • Primitive components • Pluggable Look and Feel • Single Look and Feel • Pure Java – Platform • Not Pure Java – Platform independent dependent Lightweight vs. Heavyweight Lightweight Heavyweight • Not dependent on native peers • Dependent on native peers • Rely on primitive Java drawing • Rely on native windowing techniques in rendering. system in rendering. • Components behavior is the • Component behavior may same across platforms. differ across platforms. • Look and Feel is independent • Look and feel is dependent of platform on platform Swing GUI Fundamentals • Swing components • Containers • Non-containers • Layout Mangers • Event Handling Swing Containers Used to hold other Swing containers and components. Two main types of containers: – 1) Top level containers: Root of the containment heirarchy e.g. JFrame, JDialog, JApplet – 2) Intermediate containers: Middle nodes of containment heirarchy e.g. JPanel, JScrollPane, JSplitPane, ... GUIs are usually made up of nested containers. Swing Non-containers All of the other GUI elements that are not used to hold or positions other GUI elements. 2 categories of non-containers: User input Data display/organization JButton JLabel JComboBox JToolTip JTextField JProgressBar JTable ... ... Example: Swing Components JComboBox JFrame JMenuBar JLabel JTabbedPane JScrollPane JTable JButton Layout Managers Used by containers to position and organize their components. Common Layout Managers: – FlowLayout – BorderLayout – BoxLayout – CardLayout – GridLayout – GridBagLayout Set container's layout by calling : Container. setLayout(LayoutManager manager) FlowLayout FlowLayout lays out components left to right creating new rows as necessary. Default for JPanel. Container.add(Component comp) Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html BorderLayout BorderLayout can hold components in the 5 areas: north, south, east, west, center. Default for JFrame. Container.add(Component comp, int index) // e.g. - index = BorderLayout.NORTH Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html BoxLayout BoxLayout simply positions components in a rows or columns. Example: Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html CardLayout A card layout is used when one wants to display different components at different times. A controller such as a JComboBox is normally used to flip through the different cards. Example: Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html GridLayout GridLayout will make all components the same size by placing them in a grid of specified row and column count. Example: Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html GridBagLayout Places components in a grid of rows and columns allowing specificed components to span mulitple rows and/or columns. Most flexible and robust of all layout managers. Example: Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html JPanel JPanel - JPanel - Center South JPanel - North Example: SwingLayout JFrame - BorderLayout JPanel JPanel JPanel - - FlowLayout FlowLayout Event Handling The way a program handles user interactions with the GUI Type of events: – Mouse clicks – Keyboard events Event Event Source Event Listener Object Whenever an event occurs, all listeners that subscribe to that event are notified. For an object to be able to subscribe to an event, it just needs to implement the appropriate listener interface. Event Handling (cont.) 2 key pieces of code: 1) Create the Listener class that implements the correct methods 2) Register the Listener with the component Easiest implementation: Use an Anonymous class – Example: JButton jb = new JButton(); jb.addActionListener(new ActionListener(){ public void actionPerformed(Event e){ System.err.println("Button clicked"); } }); Multiple Listeners Possible to register multiple listeners to a single component. MouseListener Detect mouse over JButton ActionListener Detect button click FocusListener Detect focus gain The Event Thread When an event is dispatched, the code that is executed runs in the Event Thread. Code that runs in the event thread should execute very quickly. Time-consuming tasks: – Loading of a file – Running a benchmark These tasks should be run in their own thread. SwingWorker Class SwingWorker is a class that should be used to perform time consuming tasks not to be run in the event thread. Without SwingWorker With SwingWorker Button Down Button Down Load Load Button Up Button Up SwingWorker - How to use it In the Event Handling code: – Create a class that extends SwingWorker. – Override the public Object SwingWorker.construct() method with the time-consuming code (Loading, etc.) – Override the public void SwingWorker.finished() method to execute code that MUST be executed after the completion of the SwingWorker Thread. – Call SwingWorker.start() SwingWorker.finished() is run in the Event Thread after the SwingWorker thread is complete. Visit for details: http://java.sun. com/docs/books/tutorial/uiswing/misc/threads.html Model-View-Controller(MVC) Design Pattern Model – An internal representation of the data displayed by the view. View – The interface that the user manipulates. Controller – Used to change state when the view is changed by the user. Model Invoke View Change Invoke State Change View User Interaction Controller MVC - A Simple Example SpreadSheet Application – Model = TableModel – View = JTable – Controller = CellEditorListener TableModel Invoke View Change Invoke State Change CellEditorListener JTable User Interaction Rich Clients for Web Services Talk given last year at Java One (2001) by Howard Rosen, Hans Muller, Scott Violet of the Java Client Group Details can be found at: http://java.sun.com/products/jfc/tsc/articles/javaOne2001/2734/index.html Summary: Rich Clients + Web Services = Best Web Experience Web Services – Not only for browser based applications. – Create rich clients using Java Technologies. – Deploy Java applications via Java Web Start Java [tm] Technology Web Services Model Why Build Rich Clients? Dynamic, Responsive, Easy to use Competitive edge, not just HTML Real-time visualization Decouple server performance from GUI Support offline usage Client - Server Communication Speak HTTP just like your browser-based applications. Server-side code does not need to change. Low bandwidth connection to the server 1 second round trips typical – If > 1 second; use a worker thread to communicate. Just like SwingWorker – Always keep GUI reponsive Deploying via Java Web Start Single click deployment: – User clicks on a link in a browser – Application starts Eliminates the install step Based on JSR-56: Java Network Launching Protocol and API Downloaded application runs outside of the browser, unlike an applet. Guaranteed to run the most current version of the application. Summary Java Swing provides developers to create complex 100% Java based GUIs. A whole lot more out there that we did not talk about: – Pluggable Look and Feel – Drag and Drop – Internationalization ... The Java Tutorial – http://java.sun.com/docs/books/tutorial/ TAKE IT TO THE NTH Asif Habibullah [email protected].
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages34 Page
-
File Size-