First Window Builder Program

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 Object-Oriented Software Engineering CS GUI building with NetBeans Contents User Interaction JButtons JTextFields JLabels JForms in NetBeans Practical.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
Copyright © Curt Hill Turtles The beginning of media computation.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Windows Builder An Eclipse Plugin for Creating Windows Graphical User Interfaces Copyright © 2014 Curt Hill.
Copyright © Curt Hill First Windows Program GUIs in Eclipse.
Applications Development
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Topics Introduction Scene Graphs
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Creating User Interfaces Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © – Curt Hill Building Windows Applications in wxDev-C++
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Copyright © 2015 – Curt Hill Window Builder Again Some more features.
Integrating Components and Dynamic Text Boxes with the Animated Map– Lesson 101 Integrating Components and Dynamic Text Boxes with the Animated Map Lesson.
Using Microsoft Office Word Assignment Layout. Target Create a Cover Page (Front Page) Create a Table of Contents Page Create a Table of Figures Page.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
CompSci 230 S Programming Techniques
Java FX: Scene Builder.
Chapter 2: The Visual Studio .NET Development Environment
An Eclipse Plugin for Creating Windows Graphical User Interfaces
Chapter 1: An Introduction to Visual Basic 2015
“Form Ever Follows Function” Louis Henri Sullivan
Java GUI.
Putting Objects in Motion
2.5 Another Java Application: Adding Integers
Console and GUI Programs
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
Java Programming: From Problem Analysis to Program Design,
An Automated Testing Framework
Using Procedures and Exception Handling
Ellen Walker Hiram College
Windows Desktop Applications
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Organizing common actions
Variables and Arithmetic Operations
Predefined Dialog Boxes
Graphical user interface-based programming
Number and String Operations
Chapter 5 Processing Input with Applets
Introducing JavaScript
Java Looking at our first console application in Eclipse
Running a Java Program using Blue Jay.
An Eclipse Plugin for Creating Windows Graphical User Interfaces
The IF Revisited A few more things Copyright © Curt Hill.
The IF Revisited A few more things Copyright © Curt Hill.
The beginning of media computation Followed by a demo
Presentation transcript:

First Window Builder Program Easy GUIs in Eclipse Copyright © 2008-2018 Curt Hill

Preliminaries Goal: Create a Windows program using Eclipse with the already installed Window Builder plugin The problem to solve is to take the number of years and convert it into the number of seconds of a persons life Simple GUI with: Two buttons One input One output Copyright © 2008-2018 Curt Hill

Disclaimer There is a lot to know So we have much to cover Fortunately, it is easier than it might seem We will follow this with a demonstration A code along with Curt exercise We will have more presentations to further explain things Copyright © 2008-2018 Curt Hill

GUIs and Objects Most object oriented languages that support GUIs do so with objects This encapsulates the properties and methods nicely Java uses this approach Thus there are some objects we need to know something about Copyright © 2008-2018 Curt Hill

Needed Objects JFrame JButton JTextField JLabel LayoutManager A window and container JButton JTextField A field to type in data JLabel LayoutManager Organizes GUI objects in a container Handles the resizing Listeners Copyright © 2008-2018 Curt Hill

Process First we do the normal things: Start Eclipse The Window Builder plugin should have been previously installed Close all open projects Create a new project We do not create the new Java class This is where it starts to change We add a Windows application We do this with dropdown menu Copyright © 2008-2018 Curt Hill

Add an Application Window Copyright © 2008-2018 Curt Hill

Fill in Package and Name Copyright © 2008-2018 Curt Hill

After Finish Clicked Copyright © 2008-2018 Curt Hill

What do we have? A program with 44 lines 3 methods Main initialize NewWindow We are not yet ready to modify the Java code Notice at the bottom of the code there are two tabs Source and design Copyright © 2008-2018 Curt Hill

Looking at Source Copyright © 2008-2018 Curt Hill

Next We will next click on the design tab We will either get a design screen or an error We will consider the error first Copyright © 2008-2018 Curt Hill

This is bad – but fixable Copyright © 2008-2018 Curt Hill

Explanation Both Eclipse and WindowBuilder may not have been updated to the most current version of the JDK If this error appears we revert to a previous JDK See the next screens Copyright © 2008-2018 Curt Hill

Get Properties Open the project so that the JRE shows Right click on JRE Choose properties Copyright © 2008-2018 Curt Hill

Choose a Previous JRE Copyright © 2008-2018 Curt Hill

One more time Once this is fixed we can click on Design tab again This time we should get the following screen Copyright © 2008-2018 Curt Hill

At Last Copyright © 2008-2018 Curt Hill

Commentary On the right is a stylized preview of what the window will look like The middle pane describes components we can drag onto the window On lower left is property inspector First we will change the window characteristics We click on the window in the right pane Scroll down to title and type Copyright © 2008-2018 Curt Hill

Add a title Copyright © 2008-2018 Curt Hill

If we run we get Copyright © 2008-2018 Curt Hill

Next Each window needs a Layout manager A layout manager determines how the various components are arranged on the window This is usually chosen early We will choose a simple one Flow Layout Copyright © 2008-2018 Curt Hill

Click Flow and Drag Copyright © 2008-2018 Curt Hill

Adding Components We click on items and then drag over to window First start with a Jlabel Click it Click in the Window Copyright © 2008-2018 Curt Hill

Jlabel Added Copyright © 2008-2018 Curt Hill

Change Contents After we drop it, it should be selected in the property inspector Change the Text property to Years Copyright © 2008-2018 Curt Hill

Text Property Changed Copyright © 2008-2018 Curt Hill

Next We are now up to 51 lines and have coded nothing The next thing to do is add the input box This is the Java JTextField Similar to the JLabel as far as adding Also similar to JLabel for setting the original value It may precede or follow the label Copyright © 2008-2018 Curt Hill

JTextField Dragging Copyright © 2008-2018 Curt Hill

Setting Text Copyright © 2008-2018 Curt Hill

Names When a component is dragged onto the form a new property is created The Windows Builder Plug-In does not know what you are intending It gives a uninformative name: textField, textField_1 etc. There is a Variable field in the object inspector that allows this to be changed Those components referenced in code should usually be named Copyright © 2008-2018 Curt Hill

Buttons The slightly harder one is buttons They have the same drag, set value as JLabels and JTextAreas They also need an event handler Even that is not hard We must show the events Copyright © 2008-2018 Curt Hill

Button Placed Copyright © 2008-2018 Curt Hill

Show Events Clicked Copyright © 2008-2018 Curt Hill

Scroll Down and Opened Copyright © 2008-2018 Curt Hill

Action When you double click on the Mouse Clicked slot it generates an event handler The event handler is method that is called when the event occurs It then takes you to the code page The event handler is called mouseClicked It is preceded by an annotation @override Copyright © 2008-2018 Curt Hill

Code Copyright © 2008-2018 Curt Hill

Event Handler The event handler is now an empty method in the initialized method Now we have to make that method do something Many programs and event handlers have this basic form: // Get data // Do computations // Display results Copyright © 2008-2018 Curt Hill

Getting Data The main problem is that a textfield always has type String text in it If we are interested in numerics then we must convert the String to a number This is done with static methods of a wrapper class Usually Integer.parseInteger or Double.parseDouble We cannot use Scanner That is console/file based Copyright © 2008-2018 Curt Hill

TextField to Numerics To convert an edit box value to a usable numeric value, we use the wrapper classes Integer Double A wrapper class makes a class out of a primitive Then it gives static methods that are useful for that primitive Today we want parseInt from Integer or parseDouble from Double Copyright © 2008-2018 Curt Hill

Conversion We can use this: String val = textField.getText(); int v = Integer.parseInt(val); Or combine into one: int i = Integer.parseInt(edit1.getText()); double d = Double.parseDouble(edit1.getText()); This causes the value to be obtained from the edit box named edit1 and converted into an int or double Copyright © 2008-2018 Curt Hill

Back to a String To display a result in either a JTextField or JLabel requires the setText method This may only take a String There is no String constructor that takes a numeric There is however the valueOf method Thus: lab1.setText( String.valueOf(d)); Copyright © 2008-2018 Curt Hill

Another Conversion You may also concatenate a number to a string painlessly: int a = b*c/2; String s = “Answer: “+a; However, you may not assign a number to a string directly without valueOf s = a; // Illegal s = String.valueOf(a); // OK Copyright © 2008-2018 Curt Hill

Static Methods parseInt, parseDouble, and valueOf are all static methods A static method does not need an instance of the type We usually use the class name as if it were an instance: Integer.parseInt(x); Double.parseDouble(y); String.valueOf(u); Copyright © 2008-2018 Curt Hill

Casts Why do we not cast these conversions? Casts only work on primitive types A String is not a primitive so it must use a static method to convert values into it Similarly the wrapper methods also use static methods to make a double out of a string Copyright © 2008-2018 Curt Hill

Get Data The data is in the text field named text The method call: text.getText() will get it Then we convert into an integer with: Integer.parseInt(…) We use: int years = Integer.parseInt( text.getText()); Copyright © 2008-2018 Curt Hill

Do Computations The computation is merely a bunch of multiplications to convert years into seconds You have seen this before Multiply by Days in year Hours in day Minutes in an hour Seconds in a minute double sec = years*365.25*24*60*60; Copyright © 2008-2018 Curt Hill

Display Results System.out.println is used for console programs In a GUI we put the result in a Label or TextField In this example the label is named label We use the setText method: label.setText(“”+sec); Copyright © 2008-2018 Curt Hill

Event Handler Copyright © 2008-2018 Curt Hill

Are we done? This program is now 80 lines It still needs some work: Three of which we have coded It still needs some work: An exit button An about button However, most of that can wait until we get this part under control Copyright © 2008-2018 Curt Hill

New Button Exit button is simple, add a new: Event handler The event handler method should execute: System.exit(0); This enlarges the program to 82 lines The About needs a dialog which will wait for a future presentation Copyright © 2008-2018 Curt Hill

With Exit Button Copyright © 2008-2018 Curt Hill

Errors In generating this presentation I received an error in Windows Builder It is not too hard to get WB confused The program would compile and run So I exited Eclipse and started it and it was fine Copyright © 2008-2018 Curt Hill

Problems Each event handler is local to the listener that is being created The order of adding is important An early event handler cannot reference things that are declared later Thus sometimes the Java code needs to be fixed to move the declaration away from the initialization and/or registering the event handler Copyright © 2008-2018 Curt Hill

Lastly We are up to about 89 lines Seven of these we coded directly Four Java lines Three comment lines The rest Windows Builder generated Our last two programs will be using Windows Builder Still picture manipulators We will next do the demo Copyright © 2008-2018 Curt Hill