GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces (Part IV)
Graphic User Interfaces Layout Managers Event Handling.
Graphical User Interfaces, 2D Graphics & Game Programming.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
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 Programming using NetBeans. What is a GUI ? GUI – Graphical User Interface The (visual) interface between humans and computers Ranging from command.
Java Programming Chapter 10 Graphical User Interfaces.
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.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
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 --
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
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.
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.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
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.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Creating User Interfaces Event-Driven Programming.
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.
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.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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.
1 Lecture 8: User Interface Components with Swing.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
A Quick Java Swing Tutorial
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
GUI Programming using NetBeans
A First Look at GUI Applications Radio Buttons and Check Boxes
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Creating Graphical User Interfaces
Presentation transcript:

GUI Programming using NetBeans

RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text fields –Lists and Combo boxes –Check and Radio buttons We now look more closely at how to use GUI controls in an application

RHS – SOC 3 GUI construction In general, we have two options when constructing a GUI –Build it ”by hand” using Swing API –Use the NetBeans GUI Builder Using the GUI Builder is usually much easier than hand-coding the GUI Does not give you complete control, however…

RHS – SOC 4 GUI construction

RHS – SOC 5 GUI construction If you wish to construct a GUI manually, you usually begin by creating a JFrame A JFrame object is essentially an empty window, into which you can add containers for GUI controls Typically, you will add a JPanel to the frame – the JPanel object will contain the actual GUI controls

RHS – SOC 6 GUI construction public static void main(String[] args) { JFrame theFrame = new JFrame(); theFrame.setBounds(200, 200, 720, 450); theFrame.setVisible(true); JPanel thePanel = new JPanel(); theFrame.add(thePanel); }

RHS – SOC 7 GUI construction On the JPanel object, various layout strategies can be used –Flow layout – left to right –Border layout – groups into areas –Grid layout – groups into a grid Border layout is default, and also most commonly used

RHS – SOC 8 GUI construction Using border layout, the panel is divided into five areas –Center –North –South –East –West

RHS – SOC 9 GUI construction If a control is put into an area, it will expand to fill out the area Good when resizing, but may look weird… If you need a finer level of control, put a panel inside a panel… …or maybe consider a different layout

RHS – SOC 10 public static void main(String[] args) { JFrame theFrame = new JFrame(); theFrame.setBounds(200, 200, 240, 150); theFrame.setVisible(true); JPanel thePanel = new JPanel(); thePanel.setLayout(new BorderLayout()); thePanel.add(new Button("Center"), BorderLayout.CENTER); theFrame.add(thePanel); } GUI construction

RHS – SOC 11 Exercises Try out create an application which looks like the picture to the right, without using the GUI Builder. That is, create it only by using the Swing API methods as shown in the presentation. Remember that you can put panels within panels…

RHS – SOC 12 Text field Two common purposes: –Allow the user to enter data as text –Display text data to the user A text field can be ”enabled” or ”disabled” –Enabled: Data can be entered –Disabled: Data can only be displayed At some point we need to set or extract the text from the text field

RHS – SOC 13 Text field JFrame theFrame = new JFrame(); theFrame.setBounds(200, 200, 300, 300); JPanel thePanel = new JPanel(); thePanel.setLayout(new BorderLayout()); JTextField theTextField = new JTextField(); thePanel.add(theTextField, BorderLayout.NORTH); theFrame.add(thePanel); theFrame.setVisible(true);

RHS – SOC 14 Text field

RHS – SOC 15 Text field Enabling a text field theTextField.setEditable(true); Disabling a text field theTextField.setEditable(false); Setting the text in a text field theTextField.setText("Greeting earthlings!"); Getting the text from a text field String s = theTextField.getText();

RHS – SOC 16 List box / Combo box A list (or combo) box enables the user to choose an option between many alternatives List box: User can only choose between specified alternatives Combo box: User can choose between specified alternatives, or specify choice manually (type it in)

RHS – SOC 17 List box / Combo box Object[] choices = {"One", "Two", "Three", "Four"}; JComboBox theBox = new JComboBox(choices); theBox.setEditable(true); thePanel.add(theBox, BorderLayout.NORTH);

RHS – SOC 18 List box / Combo box Combo box (editable)

RHS – SOC 19 List box / Combo box Enabling a Combo box theBox.setEditable(true); Disabling a Combo box theBox.setEditable(false); Setting the selection in a Combo box theBox.setSelectedItem(”Three"); Getting the selection from a Combo box String s = (String)theBox.getSelectedItem();

RHS – SOC 20 Check boxes In some cases, the set of possible choices is limited to two options Often a case of either/or, or perhaps on/off A Check box can only be in two states; checked or unchecked Nice fit for binary choices

RHS – SOC 21 Check boxes JCheckBox theBBox = new JCheckBox("Bold"); JCheckBox theIBox = new JCheckBox("Italic"); JCheckBox theUBox = new JCheckBox("Underline"); thePanel.add(theBBox,BorderLayout.WEST); thePanel.add(theIBox,BorderLayout.NORTH); thePanel.add(theUBox,BorderLayout.EAST);

RHS – SOC 22 Check boxes

RHS – SOC 23 Check boxes Enabling a Check box theCBox.setEnabled(true); Disabling a Check box theCBox.setEnabled(false); Setting the selection in a Check box theCBox.setSelected(isSelected); Getting the selection from a Check box boolean isSelected = theCBox.isSelected();

RHS – SOC 24 Radio buttons If the number of choices is few, and they are mutually exclusive, use a group of Radio buttons Only one button in a group of Radio buttons can be selected

RHS – SOC 25 Radio buttons JRadioButton small = new JRadioButton("Small"); JRadioButton medium = new JRadioButton("Medium"); JRadioButton large = new JRadioButton("Large"); ButtonGroup theGroup = new ButtonGroup(); theGroup.add(small); theGroup.add(medium); theGroup.add(large);

RHS – SOC 26 Radio buttons

RHS – SOC 27 Radio buttons Enabling a Radio button theRB.setEnabled(true); Disabling a Radio button theRB.setEnabled(false); Setting the selection in a Radio button theRB.setSelected(isSelected); Getting the selection from a Radio button boolean isSelected = theRB.isSelected();

RHS – SOC 28 Radio buttons Note that even though only one Radio button in a group can be selected, we must call isSelected() until we find it… Putting Radio buttons in a group does not make them appear grouped visually

RHS – SOC 29 Exercises Try out create an applica- tion which looks like the picture to the right, without using the GUI Builder. Only one radio button should be selected at all times Font for a control can be set using the setFont method (see the docu- mentation)

RHS – SOC 30 The concept of events On the inside (code), GUI code has a very different structure than ”usual” code Usual code is driven by con- ditions and various control structures (if, while,…) GUI code is driven by events

RHS – SOC 31 The concept of events Execution of GUI code is much more unpredictable than for usual code We cannot predict – or dictate – what the user does, so we must always handle any possible action the user can do A user action related to the GUI is called an event

RHS – SOC 32 The concept of events

RHS – SOC 33 The concept of events Almost all actions the user performs will ”trigger” an event for us to handle –Moving the mouse –Clicking on a button –Writing text in a text box –….and so on There are hundreds of possible events!

RHS – SOC 34 The concept of events Fortunately, is it optional to respond to an event We only need to do so, if we want any special action to happen If that is the case, we must write an event handler for that particular event

RHS – SOC 35 Relevant events Unless we need more sophisticated behavior, we only need to handle two types of events –Choosing a menu item –Clicking on a push button In both cases, we must create an object which can listen for events from the control in question

RHS – SOC 36 Relevant events An event listener class must implement a …Listener interface (there are several) From pushbuttons and menu items, we get ”action events”, so a listener class must implement the ActionListener interface This interface has a single method: actionPerformed

RHS – SOC 37 Relevant events public class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("Button clicked"); } } private ActionListener theListener; theListener = new MyListener();... JButton theButton = new JButton("Click Me!"); theButton.addActionListener(theListener);

RHS – SOC 38 Relevant events A very common dialog construction: –Add an ”OK” button –In the event listener for the button Retrieve data from the relevant controls Process the data Close the dialog Pressing ”OK” means: ”Now my input is ready, do something with it!”

RHS – SOC 39 Wrapping up GUI programming is somewhat difficult and tedious Unless you need something special, use the NetBeans GUI Builder! Always nice to know what does on under the hood… You can – to some degree – mix manual coding and using the GUI Builder

RHS – SOC 40 Exercises Try out create an application which looks like the picture to the right, without using the GUI Builder. Create event listeners for the buttons, that print the texts: –”Brrr” for Arctic and Antarctic –”Puhh” for America and Asia –”Wet!” for Atlantic You should not write more than three event listeners!