Building a Swing Interface

Slides:



Advertisements
Similar presentations
Gui Programming - II Teguh Sutanto, M.Kom.
Advertisements

GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
F27SB2 Software Development 2 Lecture 9: Java GUIs 6.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
Java Programming, Second Edition Chapter Five Input and Selection.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Java's Graphical User Interface Toolkit
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
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.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
Basics of GUI Programming Chapter 11 and Chapter 22.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
제 14 장 고급 스윙 컴포넌트.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Graphical User Interfaces
Swing JComponents.
Java GUI.
CSE 114 – Computer Science I Event Programming
Message, Input, Confirm, and Specialized Dialogs
Chapter 13: Advanced GUIs and Graphics
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
Object-Oriented Software Engineering
Graphical User Interface
Message, Input, and Confirm Dialogs
CS431 ws99 Half Text Half Graphics
Ch. No Name Marks 01 AWT Networking JDBC Swing 05
Object-Oriented Software Engineering
Ch. No Name Marks 01 AWT Networking JDBC Swing 05
Advanced GUIs and Graphics
Presentation transcript:

Building a Swing Interface Dept. of CSIE, National University of Tainan 10/21/2012

Standard Dialog Boxes The four standard dialog boxes offered by JOptionPane are as follows: ConfirmDialog—Asks a question, with buttons for Yes, No, and Cancel responses InputDialog—Prompts for text input MessageDialog—Displays a message OptionDialog—Comprises all three of the other dialog box types

Confirm Dialog Boxes showConfirmDialog(Component, Object) method. The Component argument specifies the container that should be considered to be the parent of the dialog box. The second argument, Object, can be a string, a component, or an Icon object. int response = JOptionPane.showConfirmDialog(null, “Should I delete all of your irreplaceable personal files?”); response can be YES_OPTION or NO_OPTION or CANCEL_OPTION.

Confirm Dialog Boxes showConfirmDialog(Component, Object, String, int, int). The first two arguments are the same as those in other showConfirmDialog() methods. The last three arguments are the following: A string that will be displayed in the dialog box’s title bar. An integer that indicates which option buttons will be shown; it should be equal to one of the class constants: YES_NO_CANCEL_OPTION or YES_NO_OPTION. An integer that describes the kind of dialog box it is, using the class constants ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE.

Confirm Dialog Boxes For example: int response = JOptionPane.showConfirmDialog(null, “Error reading file. Want to try again?”, “File Input Error”, JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);

Input Dialog Boxes showInputDialog(Component, Object) method String response = JOptionPane.showInputDialog(null, “Enter your name:”);

Message Dialog Boxes JOptionPane.showMessageDialog(null, “The program has been uninstalled.”);

Using Dialog Boxes import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*; public class FeedInfo extends JFrame { private JLabel nameLabel = new JLabel(“Name: “, SwingConstants.RIGHT); private JTextField name; private JLabel urlLabel = new JLabel(“URL: “, SwingConstants.RIGHT); private JTextField url; private JLabel typeLabel = new JLabel(“Type: “, private JTextField type;

Using Dialog Boxes public FeedInfo() { super(“Feed Information”); setSize(400, 105); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLookAndFeel(); // Site name String response1 = JOptionPane.showInputDialog(null, “Enter the site name:”); name = new JTextField(response1, 20); // Site address String response2 = JOptionPane.showInputDialog(null, “Enter the site address:”); url = new JTextField(response2, 20);

Using Dialog Boxes // Site type String[] choices = { “Personal”, “Commercial”, “Unknown” }; int response3 = JOptionPane.showOptionDialog(null, “What type of site is it?”, “Site Type”, 0, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]); type = new JTextField(choices[response3], 20); setLayout(new GridLayout(3, 2)); add(nameLabel); add(name); add(urlLabel);

Using Dialog Boxes add(url); add(typeLabel); add(type); setLookAndFeel(); setVisible(true); } private void setLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(this);

Using Dialog Boxes } catch (Exception e) { System.err.println(“Couldn’t use the system “ + “look and feel: “ + e); } } public static void main(String[] arguments) { FeedInfo frame = new FeedInfo(); }

Scroll Panes The following example creates a text area in a scroll pane and adds the scroll pane, scroller, to a container called mainPane: textBox = new JTextArea(7, 30); JScrollPane scroller = new JScrollPane(textBox); mainPane.add(scroller); JScrollPane(Object, int, int) constructor HORIZONTAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_NEVER VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_AS_NEEDED VERTICAL_SCROLLBAR_NEVER

Toolbars A toolbar, created in Swing with the JToolBar class, is a container that groups several components into a row or column. These components are most often buttons. Constructor methods include the following: JToolBar()—Creates a new toolbar JToolBar(int)—Creates a new toolbar with the specified orientation the orientation can be explicitly set with the HORIZONTAL or VERTICAL class variables of the SwingConstants interface

FeedBar.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FeedBar extends JFrame { public FeedBar() { super(“FeedBar”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create icons ImageIcon loadIcon = new ImageIcon(“load.gif”); ImageIcon saveIcon = new ImageIcon(“save.gif”); ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”); ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”);

FeedBar.java // create buttons JButton load = new JButton(“Load”, loadIcon); JButton save = new JButton(“Save”, saveIcon); JButton subscribe = new JButton(“Subscribe”, subscribeIcon); JButton unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon); // add buttons to toolbar JToolBar bar = new JToolBar(); bar.add(load); bar.add(save); bar.add(subscribe); bar.add(unsubscribe);

FeedBar.java // prepare user interface JTextArea edit = new JTextArea(8, 40); JScrollPane scroll = new JScrollPane(edit); BorderLayout bord = new BorderLayout(); setLayout(bord); add(“North”, bar); add(“Center”, scroll); pack(); setVisible(true); } public static void main(String[] arguments) { FeedBar frame = new FeedBar(); }

Menus JMenuItem—An item on a menu JMenu—A drop-down menu that contains one or more JMenuItem components, other interface components, and separators, lines displayed between items JMenuBar—A container that holds one or more JMenu components and displays their names A JMenuItem component is like a button and can be set up using the same constructor methods as a JButton component. Call it with JMenuItem(String) for a text item, JMenuItem(Icon) for an item that displays a graphics file, or JMenuItem(String, Icon) for both.

Menus The following statements create seven menu items: JMenuItem j1 = new JMenuItem(“Open”); JMenuItem j2 = new JMenuItem(“Save”); JMenuItem j3 = new JMenuItem(“Save as Template”); JMenuItem j4 = new JMenuItem(“Page Setup”); JMenuItem j5 = new JMenuItem(“Print”); JMenuItem j6 = new JMenuItem(“Use as Default Message Style”); JMenuItem j7 = new JMenuItem(“Close”); A JMenu container holds all the menu items for a drop-down menu. After you have created a JMenu container, call its add(JMenuItem) to add a menu item to it.

Menus To add a line separator to the end of the menu, call the addSeparator() method. JMenu m1 = new JMenu(“File”); m1.add(j1); m1.add(j2); m1.add(j3); m1.addSeparator(); m1.add(j4); m1.add(j5); m1.add(j6); m1.add(j7);

Menus JMenuBar bar = new JMenuBar(); bar.add(m1); gui.setJMenuBar(bar); Example

FeedBar2.java public class FeedBar2 extends JFrame { public FeedBar2() { super(“FeedBar 2”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create icons ImageIcon loadIcon = new ImageIcon(“load.gif”); ImageIcon saveIcon = new ImageIcon(“save.gif”); ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”); ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”); // create buttons JButton load = new JButton(“Load”, loadIcon);

FeedBar2.java JButton save = new JButton(“Save”, saveIcon); JButton subscribe = new JButton(“Subscribe”, subscribeIcon); JButton unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon); // add buttons to toolbar JToolBar bar = new JToolBar(); bar.add(load); bar.add(save); bar.add(subscribe); bar.add(unsubscribe);

FeedBar2.java // create menu JMenuItem j1 = new JMenuItem(“Load”); JMenuItem j2 = new JMenuItem(“Save”); JMenuItem j3 = new JMenuItem(“Subscribe”); JMenuItem j4 = new JMenuItem(“Unsubscribe”); JMenuBar menubar = new JMenuBar(); JMenu menu = new JMenu(“Feeds”); menu.add(j1); menu.add(j2); menu.addSeparator(); menu.add(j3); menu.add(j4); menubar.add(menu);

FeedBar2.java // prepare user interface JTextArea edit = new JTextArea(8, 40); JScrollPane scroll = new JScrollPane(edit); BorderLayout bord = new BorderLayout(); setLayout(bord); add(“North”, bar); add(“Center”, scroll); setJMenuBar(menubar); pack(); setVisible(true); } public static void main(String[] arguments) { FeedBar2 frame = new FeedBar2(); }