Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Advertisements

Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Graphical User Interfaces (Part IV)
1 Chapter 13 Creating User Interfaces. 2 Objectives F To create graphical user interfaces with various user-interface components: JButton, JCheckBox,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
1 Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 13 Creating User Interfaces …Starry starry night flaming flowers that brightly blaze.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Chapter 9: Creating User Interfaces What is JavaBean? What is JavaBean? JComponent JComponent JButton JButton JLabel JLabel JTextField JTextField JTextArea.
Chapter 13 Creating User Interfaces F What is JavaBeans?  Component and JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Chapter 9: Creating User Interfaces What is JavaBean? What is JavaBean? JComponent JComponent Jbutton Jbutton ImageIcon ImageIcon JLabel JLabel JTextField.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 34 Menus, Toolbars,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Getting Input. Text Fields A text field is a box that the user can type in Use the JTextField class JTextField tf1 = new JTextField(15); 15 is the field.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Creating User Interfaces Event-Driven Programming.
Basics of GUI Programming Chapter 11 and Chapter 22.
Chapter 11 Creating User Interfaces
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.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
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.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Lecture 08 Abstract Windows Toolkit (AWT) and Swing Jaeki Song.
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.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Jaeki Song JAVA Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
제 14 장 고급 스윙 컴포넌트.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Building a Swing Interface
·  Chapter 29 Menus.
CSE 114 – Computer Science I Event Programming
Chapter 7 Creating User Interfaces
Chapter 13 Creating User Interfaces
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Presentation transcript:

Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs

Component Properties The Component class is root for all UI components and containers. List of frequently used properties: F font F background F foreground F preferredSize F minimumSize F maximumSize

JComponent Properties All but a few Swing components ( such JFrame, JApplet and JDialog ) are subclasses of JComponent. F toolTipText  doubleBuffered (to reduce flickering) F border

Buttons

JButton A button is a component that triggers an Action Event when clicked. The following are JButton non-default constructors: JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)

JButton Properties  mnemonic (to specify a shortcut key: Alt+S)  icon ( image on the button ) Position of text and icon on the button: F horizontalAlignment F verticalAlignment F horizontalTextPosition F verticalTextPosition

JLabel  A label is a display area for a short text, an image, or both. F The non-default constructors for labels are as follows: JLabel(String text, int horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment)

JLabel Methods F void setText(String str) F String getText() F void setAlignment(int how) SwingConstants.LEFT SwingConstants.CENTER SwingConstants.RIGHT F int getAlignment()

JLabel Methods F Example: JLabel latLabel = new JLabel(”Latitude”, SwingConstants.RIGHT);

JTextField F A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description).

JTextField Constructors F JTextField(int columns) Creates an empty text field with the specified number of columns. F JTextField(String text) Creates a text field initialized with the specified text.  JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size.

JTextField Properties F text F horizontalAlignment F editable F columns

JTextField Methods F String getText() Returns the string from the text field. F setText(String text) Puts the given string in the text field. F void setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. F void setColumns(int) Sets the number of columns in this text field. The length of the text field is changeable.

JTextField Example F Create JTextField JTextField latTextField = new JTextField("", 30); panel.add(latTextField);... F Use the JTextField String latString = latTextField.getText(); // double lat = Double.parseDouble(latString);

JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.

JTextArea Constructors  JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns.  JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified.

JTextArea Properties  text F editable F columns F lineWrap F wrapStyleWord F rows F lineCount F tabSize

Example: Using Text Area String text = “… A text …/n” + “… more text …”; JTextArea jta = new JTextArea(); jta.setText(text);

JComboBox F A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. Also known as choice or drop-down menu F To create a choice, use constructors: JComboBox() JComboBox(Object[] stringItems)

JComboBox Methods  To add an item to a JComboBox, use void addItem(Object item)  To get an item from JComboBox, use Object getItem()

Using the itemStateChanged Handler  JComboBox can generate ActionEvent and ItemEvent.  When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.

Example: JComboBox String itemString[]={”Item 1”, ”Item 2”, ”Item 3”}; // Create JComboBox JComboBox jcbo = new JComboBox(itemsString); // Register Listener jcbo.addItemListener(this); public void itemStateChanged(ItemEvent e) { int i = jcbo.getSelectedIndex(); //do something System.out.println(“Index is ” + i); System.out.println(“Item is ” + itemString[i]); }

JList A list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values.

JList Constructors  JList() Creates an empty list. F JList(Object[] stringItems) Creates a new list initialized with items.

JList Properties & Methods F selectedIndex F selectedIndices F selectedValue F selectedValues F selectionMode: SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION. F int getSelectedIndex() F int[] getSelectedIndices()

JCheckBox A check box is a component that enables the user to toggle a choice on or off, like a light switch.

JCheckBox Constructors F JCheckBox() F JCheckBox(String text) F JCheckBox(String text, boolean selected) F JCheckBox(Icon icon) F JCheckBox(String text, Icon icon) F JCheckBox(String text, Icon icon, boolean selected)

JCheckBox Properties  JCheckBox has all the properties in JButton.  Additionally, JCheckBox has the following property: selected

JCheckBox jchk1 = new JCheckBox(“Check 1”, FALSE); jchk2 = new JCheckBox(“Check 2”, TRUE); jchk1.addItemListener(); jchk2.addItemListener();... public void itemStateChanged(ItemEvent e) { int i; if(e.getSource() instanceof JCheckBox) { if(jchk1.isSelected()) { // do something } if(jchk2.isSelected()) { // do something else }

JRadioButton F Radio buttons are variations of check boxes. F They are often used in the group, where only one button is checked at a time.

JRadioButton Constructors F JRadioButton() F JRadioButton(String text) F JRadioButton(String text, boolean selected) F JRadioButton(Icon icon) F JRadioButton(String text, Icon icon) F JRadioButton(String text, Icon icon, boolean selected)

JRadioButton Properties  JRadioButton has all the properties in JButton.  Additionally, JRadioButton has the following property: selected Example: JRadioButton jrb1 = JRadioButton(“Radio 1”); JRadioButton jrb2 = JRadioButton(“Radio 2”, selected);

Grouping Radio Buttons // Create JRadioButtons JRadioButton jrb1 = JRadioButton(“Radio 1”); JRadioButton jrb2 = JRadioButton(“Radio 2”, selected); // Create group of JRadioButtons ButtonGroup jrbg = new ButtonGroup(); jrbg.add(jrb1); jrbg.add(jrb2);

Message Dialogs F A dialog is normally used as a temporary window to receive additional information from the user, or to provide notification that some event has occurred.

Creating Message Dialogs Use static method in JOptionPane class. F showMessageDialog( Component parentComponent, Object message, String title, int messageType) F showMessageDialog( Component parentComponent, Object message, String title, int messageType, Icon icon)

Dialogs F /uiswing/components/dialog.html Example: Message dialog with default title and icon: String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str);

Examples String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str, “Message”); JOptionPane.showMessageDialog(frame, str, “Inane warning”, JOptionPane.WARNING_MESSAGE);

Examples String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str, “Inane error”, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(frame, str, “A plain message”, JOptionPane.A_PLAIN_MESSAGE);

Confirm Dialog showConfirmDialog int n = JOptionPane.showConfirmDialog(frame, "Would you like green eggs and ham?", "An Inane Question", JOptionPane.YES_NO_OPTION);

Input Dialog Object[] possibilities = {"ham", "spam", "yam"}; showInputDialog String s = (String)JOptionPane.showInputDialog( frame, "Complete the sentence:\n” + ”\"Green eggs and...\"", "Customized Dialog", JOptionPane.PLAIN_MESSAGE, icon, possibilities,"ham"); //If a string was returned, say so. if ((s != null) && (s.length() > 0)) { setLabel("Green eggs and... " + s + "!"); return; } //If you're here, the return value was null/empty. setLabel("Come on, finish the sentence!"); int n = JOptionPane.

Dialogs  As the previous code snippets showed, the showMessageDialog, showConfirmDialog, and showOptionDialog methods return an integer indicating the user's choice.  The values for this integer are YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, and CLOSED_OPTION. Each option corresponds to the button the user pressed.  Exception: when CLOSED_OPTION is returned, it indicates that the user closed the dialog window explicitly, rather than by choosing a button inside the option pane. components/dialog.html components/dialog.html

JScrollPane F A scroll pane is a component that supports automatically scrolling without coding.

Menus  Java provides several classes— JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, and JRadioButtonMenuItem —to implement menus in a frame.  A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. F Menus consist of menu items that the user can select (or toggle on or off). Menu bars can be viewed as a structure to support menus.

Menu Demo Menu Bar Menu Items

The JMenuBar Class JFrame frame = new JFrame(); frame.setSize(300, 200); frame.setVisible(true); JMenuBar mb = new JMenuBar(); frame.setJMenuBar(mb); A menu bar holds menus; the menu bar can only be added to a frame. Example: Create and add a JMenuBar to a frame:

The Menu Class JMenu fileMenu = new JMenu("File", false); JMenu helpMenu = new JMenu("Help", true); mb.add(fileMenu); mb.add(helpMenu); Attach menus onto a JMenuBar. Example: create two menus, File and Help, and add them to the JMenuBar mb :

The MenuItem Class  Individual items of Menu. MenuItem() MenuItem(String ItemName) MenuItem(String ItemName, MenuShortcut keyAccel)

1. Create Menu Items JMenuItem jmiNew = new JMenuItem("new"); JMenuItem jmiOpen = new JMenuItem("open"); JMenuItem jmiPrint= new JMenuItem("print"); JMenuItem jmiExit = new JMenuItem("exit");

2. Add Menu Items fileMenu.add(jmiNew); fileMenu.add(jmiOpen); fileMenu.add(new JMenuItem("-")); // separator fileMenu.add(jmiPrint); fileMenu.add(jmiExit); fileMenu.add(new JMenuItem("-")); // separator

3. Register Listener jmiNew.addActionListener(this); jmiOpen.addActionListener(this); jmiPrint.addActionListener(this); jmiExit.addActionListener(this);

4. Implement Handler public void actionPerformed(ActionEvent e) { String actionCommand = e.getActinCommand(); if(e.getSource() isntanceof JMenuItem) { if(“New”.equals(actionCommand)) { // DO IT! } else if(“Open”.equals(actionCommand)) { // DO IT! } else if(“Print”.equals(actionCommand)) { // DO IT! } else if(“Exit”.equals(actionCommand)) { System.out(0); } }//e.getSource() }//actionPerformed()