SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.

Slides:



Advertisements
Similar presentations
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Advertisements

Graphical User Interfaces (Part IV)
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
1 lecture 14 GUI PROGRAMMING IVLecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs.
1 lecture 12Lecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs with Tooltips.  Example.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
GUI Programming using NetBeans. What is a GUI ? GUI – Graphical User Interface The (visual) interface between humans and computers Ranging from command.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
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.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
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.
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.
OOP (Java): GUI I/ OOP Objectives – –describe some of the GUI controls and their listeners; more appear in part 13 Semester 2,
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.
Chapter 9: Creating User Interfaces What is JavaBean? What is JavaBean? JComponent JComponent JButton JButton JLabel JLabel JTextField JTextField JTextArea.
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.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
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.
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 the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
1 Lecture 8: User Interface Components with Swing.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
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.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
CSC 205 Programming II Lecture 5 AWT - I.
Advanced User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interface
Advanced Programming in Java
Advanced GUIs and Graphics
Presentation transcript:

SWING

AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton is the template class from which all buttons are defined and their behavior is controlled. This includes push buttons, toggle buttons, check boxes, radio buttons, menu items, and menus themselves. Its direct subclasses are JButton, JToggleButton, and JMenuItem. There are no subclasses of JButton in Swing. JToggleButton has two subclasses: JCheckBox, JRadioButton. JMenuItem has three subclasses: JCheckBoxMenuItem, JRadioButtonMenuItem, and JMenu.

JButton class javax.swing.JButton JButton is a basic push-button, one of the simplest Swing components. We can add images, specify text and image alignment, set foreground and background colors, set fonts, etc. Additionally, we can add ActionListeners, ChangeListeners, and ItemListeners to receive ActionEvents, ChangeEvents, and ItemEvents respectively when any properties in its model change value. Constructors:- JButton (Icon i) JButton (String s) JButton (String s, Icon i)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jbutton extends JApplet implements ActionListener { JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); JButton b1=new JButton("Do Nothing"); b1.setActionCommand("Do Nothing"); b1.addActionListener(this); cp.add(b1); ImageIcon ic = new ImageIcon(“poo.gif"); JButton b2=new JButton(ic); b2.setActionCommand(“Pooh"); b2.addActionListener(this); cp.add(b2); jtf = new JTextField(15); cp.add(jtf); } public void actionPerformed( ActionEvent e) { jtf.setText(e.getActionCommand()); }

JToggleButton class javax.swing.JToggleButton JToggleButton provides a selected state mechanism which extends to its children, JCheckBox and JRadioButton We can test whether a toggle button is selected using AbstractButton's isSelected() method. we can set this property with its setSelected() method. JToggleButtons are often used in ButtonGroups. A ButtonGroup manages a set of buttons by guaranteeing that only one button within that group can be selected at any given time. Thus, only JToggleButton and its subclasses are useful in a ButtonGroup (because a JButton does not maintain a selected state).

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class togglebuttondemo { public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { AbstractButton ab = (AbstractButton) ae.getSource(); boolean selected = ab.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); } }; toggleButton.addActionListener(al); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); } }

//The event demonstration program for JToggleButton. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class togglebuttondemo2 { public static void main(String[] args) { JToggleButton jtb = new JToggleButton("Press Me"); jtb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { System.out.println ("ActionEvent!"); } }); jtb.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { System.out.println ("ItemEvent!"); } });

jtb.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ev) { System.out.println ("ChangeEvent!"); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout (new FlowLayout()); c.add (jtb); f.pack(); f.setVisible(true); }

How to Use Check Boxes The JCheckBox class provides support for check box buttons. You can also put check boxes in menus, using the JCheckBoxMenuItem class. Because JCheckBox and JCheckBoxMenuItem inherit from AbstractButton, Swing check boxes have all the usual button characteristics. For example, you can specify images to be used in check boxes. Any number of check boxes in a group — none, some, or all — can be selected. Constructors JCheckBox (Icon i) JCheckBox (Icon i, boolean state) – if state is true, checkbox is initially selected JCheckBox (String s) JCheckBox (String s, Icon i) JCheckBox (String s, boolean state) JCheckBox (String s, Icon i, boolean state) When a checkbox is selected or deselected, an item event is generated, which is handled by itemStateChanged (). Inside itemStateChanged(), getItem () method gets the JCheckBox object that generated the event. getText() method gets the text for the checkbox.

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jcheckbox extends JApplet implements ItemListener { String msg =""; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JCheckBox cb = new JCheckBox("vanila"); cb.addItemListener(this); cp.add(cb); cb = new JCheckBox("Strawberry"); cb.addItemListener(this); cp.add(cb); cb = new JCheckBox("Butterscotch"); cb.addItemListener(this); cp.add(cb); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)ie.getItem(); msg += cb.getText(); repaint(); } public void paint(Graphics g) { g.drawString(msg,100,100);} }

How to Use Radio Buttons Radio buttons are groups of buttons in which, only one button at a time can be selected. The Swing supports radio buttons with the JRadioButton and ButtonGroup classes. To put a radio button in a menu, use the JRadioButtonMenuItem class. Because JRadioButton inherits from AbstractButton. Immediate super class is JToggleButton, Swing radio buttons have all the usual button characteristics. For example, you can specify the image displayed in a radio button. Each time the user clicks a radio button (even if it was already selected), the button fires an action event. One or two item events also occur — one from the button that was just selected, and another from the button that lost the selection (if any). Constructors: JRadioButton (Icon i) JRadioButton (Icon i, boolean state) JRadioButton (String s) JRadioButton (String s, Icon i) JRadioButton (String s, boolean state) JRadioButton (String s, Icon i, boolean state)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jradiobutton extends JApplet implements ActionListener { JTextField tf; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JRadioButton b1 = new JRadioButton("Small"); b1.addActionListener(this); cp.add(b1); JRadioButton b2 = new JRadioButton("Medium"); b2.addActionListener(this); cp.add(b2); JRadioButton b3 = new JRadioButton("Large"); b3.addActionListener(this); cp.add(b3); ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); tf = new JTextField(15); cp.add(tf); } public void actionPerformed(ActionEvent ae) { tf.setText(ae.getActionCommand()); } }

How to use Combo Boxes Swings provide a combo box( a combination of a text field and a drop-down list) through the JComboBox class, which extends JComponent. A combo box normally displays one entry. It can also displays a drop down list that allows a user to select a different entry. Selection can also be typed inside the text field. Constructors: JComboBox () JComboBox (Vector v) Items are added to the list of choices via the addItem () method. Syntax – void addItem (Object ob)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jcombobox extends JApplet implements ItemListener { JLabel jl; ImageIcon India,england,usa; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JComboBox jc = new JComboBox(); jc.addItem("India"); jc.addItem("USA"); jc.addItem("UK"); jc.addItemListener(this); cp.add(jc); jl=new JLabel(new ImageIcon("India.gif")); cp.add(jl); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); if (s.equals("India")) jl.setIcon(new ImageIcon("India.gif")); else if (s.equals("USA")) jl.setIcon(new ImageIcon("USA.gif")); else jl.setIcon(new ImageIcon("UK.gif")); }}

Menus Java provides the two different kinds of menus – regular and pop-up menus. Java provides the following classes for creating and managing menus: JMenuBar JMenu JMenuItem JCheckBoxMenuItem JRadioButtonMenuItem The menu bar in a window is the horizontal area below the title bar of the window and captions of each of the menus on it. Steps for creating Regular Menus 1. Create an object of JMenuBar class. - Attach a JMenuBar class to a frame. The JMenuBar object is created JMenuBar mb = new JMenuBar(); 2. Call the add method of the frame to attach the menu bar to the frame. frame.getContentPane().add(mb).

3. Create objects of the JMenu class for each menu you want in the menu bar. For example JMenu mfile = new JMenu (“File”); JMenu medit = new JMenu (“Option”); 4. Call the add method of the JMenuBar class to add each menu objects to the menu bar. For example mb.add (mfile); mb.add (moption); 5.Create objects of the JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem class for each sub-menu item. The check box menu item functions like a check box button. For example JMenuItem opt1 = new JMenuItem(“option1”); JMenuItem opt2 = new JMenuItem(“option2”); 6. Call add method of the menu class to add each item to its appropriate menu. For example: moption.add(opt1); moption.add(opt2);

import javax.swing.*; public class jmenu { public static void main(String args[]) { JFrame frame = new JFrame(" Frame with menu"); JMenuBar mb = new JMenuBar(); // frame.getContentPane().add(mb); frame.setJMenuBar(mb); JMenu mfile, medit; mfile = new JMenu("File"); medit = new JMenu("Edit"); mb.add(mfile); mb.add(medit); JMenuItem mnew, mclose,mcopy,mpaste; mnew = new JMenuItem("New"); mclose = new JMenuItem("Close"); mcopy = new JMenuItem("Copy"); mpaste = new JMenuItem("Paste");

mfile.add(mnew); mfile.add(mclose); medit.add(mcopy); medit.add(mpaste); // make close disabled mclose.setEnabled(false); // create print check box followed by a separtor JCheckBoxMenuItem mprint; mprint = new JCheckBoxMenuItem("Print"); mfile.add(mprint); mfile.addSeparator(); // create a font submenu JMenu mfont; mfont = new JMenu("Font"); mfile.add(mfont); mfont.add("Arial"); mfont.add("Times New Roman"); frame.setSize(400,400); frame.setVisible(true);} }

Pop-up Menus Pop-up Menus are menu that are displayed when a user clicks the right mouse button. They are also known as Short – cut menus Steps for creating a pop-up menu: 1. Create an object of the PopupMenu class. 2. Create objects of the Menu class for each menu you want on the menu bar. 3. Call the add() method of the PopupMenu class to add each menu object to the pop-up menu. 4. Create objects of the MenuItem or CheckBoxMenuItem classes for each item you want to display in the menu. 5. Call the add() methods of the menu in order each item to its appropriate menu.