Swing GUI. Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which.

Slides:



Advertisements
Similar presentations
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Advertisements

Corresponds with Chapter 12
1 GUI Programming (Part III). Graphical User Interfaces (Part III) Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now.  Overview.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI’s and eventhandlers in java Martin Jagersand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 8: Layout Management.
Review Session for EXCEPTIONS & GUIS -David Gries 1 Adapted from Previous Review Slides.
Graphical User Interfaces Java’s AWT and Swing APIs.
7-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : User Interface Components with.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
1 Java Swing - Components and Containment. 2 Components and Containers Components and Containers Components Components The building blocksThe building.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
(c) by Elizabeth Sugar Boese.1 Chapter 12 Events on Components - Lecture Slides.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
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.
CC1007NI: Further Programming Week 6-7 Dhruba Sen Module Leader (Islington College)
1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
1 12 Apr 2011 GUIS: Graphical User Interfaces Their mouse had a mean time between failure of … a week … it would jam up irreparably, or... jam up on the.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
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. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Object-Oriented Software Engineering JFrames with Swing.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
8 April 2008 GUIS —Graphical User Interfaces
Chapter 7-3 (Book Chapter 14)
13 April 2010 GUIS: Graphical User Interfaces
Presentation transcript:

Swing GUI

Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. It also includes containers such as windows and tool bars. SwingApplication creates four commonly used Swing components: a frame, or main window ( JFrame ) a panel, sometimes called a pane ( JPanel ) a button ( JButton ) a label ( JLabel )

A diagram of the containment hierarchy for the window shown by SwingApplication.

How to Make Frames (Main Windows)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FrameDemo { public static void main(String s[]) { JFrame frame = new JFrame("FrameDemo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }

JPanel JPanel aPanel = new JPanel(); aPanel.setLayout(new BorderLayout()); Or JPanel aPanel = new JPanel(new BorderLayout());

How to Use Buttons To create a button, you can instantiate one of the many classes that descend from the AbstractButton class. AbstractButton ClassDescription JButtonA common button JCheckBoxA check box button. JRadioButton One of a group of radio buttons JMenuItemAn item in a menu. JCheckBoxMenuItem A menu item that has a checkbox. JRadioButtonMenuItem A menu item that has a radio button

Button(JButton) Normal Button MyButton= new JButton (“Click Me!”); pane.add(MyButton); Image Button ImageIcon leftButtonIcon = new ImageIcon(“flower.gif"); b1 = new JButton(“Bunga", leftButtonIcon);

Label (JLabel) ImageIcon icon = new ImageIcon("images/middle.gif");... label1 = new JLabel("Image and Text", icon, JLabel.CENTER); //Set the position of the text, relative to the icon: label1.setVerticalTextPosition(JLabel.BOTTOM); label1.setHorizontalTextPosition(JLabel.CENTER); label2 = new JLabel("Text-Only Label"); label3 = new JLabel(icon); //Add labels to the JPanel. add(label1); add(label2); add(label3);

Check Boxes chinButton = new JCheckBox("Chin"); chinButton.setSelected(true); glassesButton = new JCheckBox("Glasses"); glassesButton.setSelected(true); hairButton = new JCheckBox("Hair"); hairButton.setSelected(true); teethButton = new JCheckBox("Teeth"); teethButton.setSelected(true);

JPanel checkPanel = new JPanel(); checkPanel.setLayout(new GridLayout(0, 1)); checkPanel.add(chinButton); checkPanel.add(glassesButton); checkPanel.add(hairButton); checkPanel.add(teethButton);

Example JCheckBox import java.awt.*; import javax.swing.*; public class KotakPilihan extends JApplet { private JCheckBox java, c cobol, vbscript, asp; public void init() { Container pane = getContentPane(); pane.setBackground(Color.white); pane.setLayout(new FlowLayout());

java = new JCheckBox (“Java”, true); pane.add(java); c = new JCheckBox (“C”); pane.add(c); cobol= new JCheckBox (“Cobol”); pane.add(cobol); vbscript = new JCheckBox (“Vbscript”); pane.add(vbscript); asp = new JCheckBox (“ASP”, true); pane.add(asp); } JavaC Cobol VBscript ASP

Radio Button JRadioButton birdButton = new JRadioButton(“Bird”); JRadioButton catButton = new JRadioButton(“Cat”); JRadioButton dogButton = new JRadioButton(“Dog”); dogButton.setSelected(true); JRadioButton rabbitButton = new JRadioButton(“Rabbit”); JRadioButton pigButton = new JRadioButton(“Pig”);

// Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton); group.add(dogButton); group.add(rabbitButton); group.add(pigButton);

List (JList) Define List JList ShortList String item[] = {“ one”, “two”,”three”,”four”,”five”,”six”} ShortList = new JList(item); ShortList.setVisibleRowCount(5); ShortList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroll = new JScrollPane(ShortList);

Example: JList import java.awt.*; import javax.swing.*; public class Senarai extends JApplet{ Container pane = getContentPane(); JList bentuk; String item [] = { “Garis”, “Segiempat”, “Bulatan”, ”Segitiga”, “Segienam”, “Segilapan”};

public void init() { pane.setBackground(Color.white); pane.setLayout (new FlowLayout()); bentuk = new JList (item); bentuk.setVisibleRowCount (4); bentuk.setSelectionModel(ListSelectionModel.SINGLE_SELECTION); JScrollPane skrol = new JScrollPane(bentuk); pane.add(skrol); }

Selecting Items in a List SINGLE_SELECTION Only one item can be selected at a time. When the user selects an item, any previously selected item is deselected first. SINGLE_INTERVAL_SELECTION Multiple, contiguous items can be selected. When the user begins a new selection range, any previously selected items are deselected first.

MULTIPLE_INTERVAL_SELECTION The default. Any combination of items can be selected. The user must explicitly deselect items.

JComboBox String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; // Create the combo box, select item at index 4. JComboBox petList = new JComboBox(petStrings); pane.add(petList) ;

JSlider static final int FPS_INIT = 10; JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 0, 30, FPS_INIT); framesPerSecond.setMajorTickSpacing(10); framesPerSecond.setMinorTickSpacing(1); framesPerSecond.setPaintTicks(true); framesPerSecond.setPaintLabels(true); //add the slider to the content pane contentPane.add(framesPerSecond);

JTextField textField = new JTextField(20); textField.addActionListener(this);... contentPane.add(textField);

JTextArea JTextArea ruangTeks; ruangTeks =new JTextArea(5,20); JScrollPane skrol =new JScrollPane(ruangTeks, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); pane.add(skrol);

JPassword JPasswordField passwordField = new JPasswordField(10); passwordField.setEchoChar('#'); passwordField.addActionListener(new ActionListener() {... }); …… private static boolean isPasswordCorrect(char[] input) { char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' }; if (input.length != correctPassword.length) return false; for (int i = 0; i < input.length; i ++) if (input[i] != correctPassword[i]) return false; return true; }

BorderLayout Container contentPane = getContentPane(); //Use the content pane's default BorderLayout. //contentPane.setLayout(new BorderLayout()); contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); contentPane.add(newJButton("Button 3 (WEST)"), BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST);

BoxLayout Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); addAButton("Button 1", contentPane); addAButton("2", contentPane); addAButton("Button 3", contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Button 5", contentPane);

CardLayout

CardLayout Program JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; public CardWindow() { Container contentPane = getContentPane(); //Put the JComboBox in a JPanel to get a nicer look. String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JPanel cbp = new JPanel(); JComboBox c = new JComboBox(comboBoxItems); c.setEditable(false); c.addItemListener(this); cbp.add(c);

//Use the default layout manager, BorderLayout contentPane.add(cbp, BorderLayout.NORTH); cards = new JPanel(); cards.setLayout(new CardLayout()); Jpanel p1 = new JPanel(); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 3")); JPanel p2 = new JPanel(); p2.add(new JTextField("TextField", 20)); cards.add(p1, BUTTONPANEL); cards.add(p2, TEXTPANEL); contentPane.add(cards, BorderLayout.CENTER);

FlowLayout Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5"));

GridLayout Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(0,2)); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5"));