More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

More on GUI We wrap up our examination of Java GUI programming by looking at several other components and their listeners – The components all come from.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
 Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Lecture 16: GUI programming JFrame windows (p.247) Containers and layout managers (p.290) Inheriting GUI components (p.330) Chapter 12: GUI programming.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
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.
1 L46 Advanced GUI Component (1). 2 OBJECTIVES  To create and manipulate sliders, and menus,
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
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.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
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.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 219 Patterns in Programming More Design Patterns.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Alice in Action with Java Chapter 14 Events and GUIs.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concurrent Programming and Threads Threads Blocking a User Interface.
EVENTS Wiring together objects, code, and actions.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
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.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Clicker quiz 11/5/13 CSE 1102 Fall A. a FlowLayout B. a BorderLayout C. a GridGrouping D. a JSplitPane E. a GridLayout Suppose we want to to lay.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUIs & Event-Driven Programming Chapter 11 Review.
Modular Event Handling
GUIs and Events Rick Mercer.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Advanced Programming in Java
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the JSlider is a bar with a mouse-movable controller you slide the controller from left to right or right to left –we will use the JSlider to control things like speed (as you move it to the right, the Timer’s delay can be shortened, thus speeding up your game colors, we will see how to do this in a couple of slides where 3 JSliders control the background color We create a JSlider by initializing it with 3 values: the minimum value (left-hand side or lower limit), maximum value (right-hand side or upper limit), and initial value –JSlider redSlider = new JSlider(0, 255, 0); –this could be used to allow the user to set the amount of Red between 0 and 255, initialized to 0

More on JSliders You can also create vertical sliders by adding JSlider.VERTICAL at the beginning of the parameters in the instantiation –JSlider slide = new JSlider(JSlider.VERTICAL, 0, 100, 50); // initial value is 50 To use a JSlider, you need to assign the JSlider a listener (just like you did with a JButton and other GUI components) –the listener needs to implement ChangeListener, so our class’ definition that contains a JSlider will look like this: –public static class JSliderExample extends JPanel implements ChangeListener To implement a ChangeListener, you need to implement the method stateChanged, which will receive a ChangeEvent ChangeEvent has a method getValue( ) which will return the current position of the JSlider

JSlider Example public static class SliderPanel extends JPanel implements ChangeListener { private JSlider redSlider, greenSlider, blueSlider; private int red, green, blue; public SliderPanel() { red = green = blue = 0; redSlider = new JSlider(0, 255, 0); greenSlider = new JSlider(0, 255, 0); blueSlider = new JSlider(0, 255, 0); redSlider.addChangeListener(this); greenSlider.addChangeListener(this); blueSlider.addChangeListener(this); JPanel sliderPanel = new JPanel(new GridLayout(3, 1)); sliderPanel.add(redSlider); sliderPanel.add(greenSlider); sliderPanel.add(blueSlider); add(sliderPanel); // Add the panels to the main panel. }

Continued public void stateChanged(ChangeEvent e) { if(e.getSource()==redSlider) red = redSlider.getValue(); else if(e.getSource()==greenSlider) green = greenSlider.getValue(); else if(e.getSource()==blueSlider) blue = blueSlider.getValue(); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(red, green, blue)); g.fillRect(0, 0, getWidth(), getHeight()); } Notice that we use e.getSource( ) to udentify the source of the Change Event (which JSlider was used?)

JSpinners A JSpinner is used to control a numeric selection –it contains a textfield that has a number, and two arrows to increase or decrease the number –the user can select a value by clicking the arrows To create a JSpinner, you have to do two things –SpinnerNumberModel mod = new SpinnerNumberModel(init, min, max, step); –JSpinner spin = new JSpinner(mod); –the Model is needed to first define the range of values for the spinner –init is the initial value, min is the smallest value in the range, max is the largest value in the range, and step is the number of values that the value goes up or down by with every arrow press For instance, we might use 3 JSpinners to allow the user to change the color instead of (or addition to) the JSliders –in which case, we might define 3 JSpinners as: SpinnerNumberModel mod = new SpinnerNumberModel(0, 0, 255, 1); JSpinner redSpinner = new JSpinner(mod); JSpinner greenSpinner = new JSpinner(mod); JSpinner blueSpinner = new JSpinner(mod);

More on JSpinners The JSpinner will also use a ChangeListener like the JSlider which means that you must add implements ChangeListener to the class, add the listener to each JSpinner, and implement a stateChanged event –the ChangeEvent passed to the stateChanged method will include both a value and the source, just like the JSlider You can also create spinners that show lists of strings: String[] classes = {“Freshman”, “Sophmore”, “Junior”, “Senior”, “Graduate”}; –SpinnerModel model = new SpinnerListModel(classes); –JSpinner spinner = new JSpinner(model); Notice in this case, the model is a List model, not a number model –notice that you do not provide an initial value with the SpinnerListModel, instead To get the value from a Spinner, you have to be careful because spinner.getValue( ) returns an Object, so if the values are numbers, use: x = Integer.parseInt(“” + spinner.getValue( ));

Lists There are three GUI components to create lists –JList – a list of items that the user can select in any combination (0 or more) –JComboBox – a list of items available through a drop box, the user selects exactly one item –JMenu – same as the JComboBox except that the menu is attached to the title bar We will only look at JComboBoxes as they are the easiest –first, you must set up the list of values that will appear in the box this is done by making an array of Strings –now create the JComboBox by using the array to instantiate it –now you use various JComboBox methods to determine what item(s) has(have) been selected (what String) determine what position(s) was(were) selected change the selection(s)

JComboBox Example Here we have code that creates a JFrame that consists of a JComboBox and a JLabel –when an item is selected in the JComboBox, the item selected is output in the JLabel private JComboBox jcb; private JLabel lab; public ListExample() { String[ ] list = {“Select an Item”, “ ”, …, “”}; jcb = new JComboBox(list); lab = new JLabel("Selection: "); jcb.addItemListener(this); JPanel panel = new JPanel(new GridLayout(1, 2)); panel.add(jcb); panel.add(lab); add(panel); } public void itemStateChanged(ItemEvent e) { if(jcb.getSelectedIndex( )!=0) lab.setText("Selection: " + (String)jcb.getSelectedItem( )); else lab.setText("Selection:"); } Notice the extra space here, why do you suppose it is needed? getSelectedItem( ) does not return a String, but instead returns an Object, since the items are Strings, we have to cast it as a String before using it in the setText operation