JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Similar documents
CSIS 10A Assignment 7 SOLUTIONS

Example 3-1. Password Validation

H212 Introduction to Software Systems Honors

Swing from A to Z Some Simple Components. Preface

Graphical User Interfaces. Comp 152

Window Interfaces Using Swing Objects

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Dr. Hikmat A. M. AbdelJaber

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

MIT AITI Swing Event Model Lecture 17

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Window Interfaces Using Swing Objects

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java Programming Lecture 6

We are on the GUI fast track path

Graphic User Interfaces. - GUI concepts - Swing - AWT

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Swing Programming Example Number 2

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

RAIK 183H Examination 2 Solution. November 11, 2013

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Introduction. Introduction

AP CS Unit 11: Graphics and Events

JAVA NOTES GRAPHICAL USER INTERFACES

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

GUI Forms and Events, Part II

CS Exam 1 Review Suggestions

Part 3: Graphical User Interface (GUI) & Java Applets

Lecture 5: Java Graphics

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Midterm assessment - MAKEUP Fall 2010


Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Agenda. Container and Component

Chapter 12 Advanced GUIs and Graphics

Attempt FOUR questions Marking Scheme Time: 120 mins

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

INTRODUCTION TO (GUIS)

Systems Programming Graphical User Interfaces

RAIK 183H Examination 2 Solution. November 10, 2014

Client-side GUI. A simple Swing-gui for searching for proudcts

Dr. Hikmat A. M. AbdelJaber

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Swing - JLabel. Adding a text (and HTML) labels to a GUI

navlakhi.com / navlakhi.education / navlakhi.mobi / navlakhi.org 1

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CS 251 Intermediate Programming GUIs: Components and Layout

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Graphical User Interface

CS111: PROGRAMMING LANGUAGE II

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Final Examination Semester 2 / Year 2011

Java. GUI building with the AWT

AWT TEXTFIELD CLASS. Constructs a new empty text field with the specified number of columns.

CSCI 201L Midterm Written Summer % of course grade

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

CS108, Stanford Handout #22. Thread 3 GUI

DM503 Programming B. Peter Schneider-Kamp.

Graphical User Interfaces 2

Java Project P6 Event Handling

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Chapter 13 Lab Advanced GUI Applications

Building Java Programs Bonus Slides

2110: GUIS: Graphical User Interfaces

Graphical User Interfaces

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

Java, Swing, and Eclipse: The Calculator Lab.

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Contents Chapter 1 Introduction to Programming and the Java Language

Swing. By Iqtidar Ali

SINGLE EVENT HANDLING

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

PART1: Choose the correct answer and write it on the answer sheet:

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

encompass a group of features for building Graphical User Interfaces (GUI).

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

JFrame & JLabel. By Iqtidar Ali

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

Transcription:

Q)Write a program to design an interface containing fields User ID, Password and Account type, and buttons login, cancel, edit by mixing border layout and flow layout. Add events handling to the button login and cancel such that clicking in login checks for matching user id and password in the database and opens another window if login is successful and displays appropriate message if login is not successful. Clicking in cancel terminates our program Let us suppose we have a database named user and a table named user_table with fields user_id and password. Now the complete code for the above task is given below: //UserInterfaceDesign.java import java.awt.*; //needed to create GUI components import javax.swing.*; //needed to create GUI components import java.awt.event.*; //needed to take a action upon any event e.g. clicking on a button import java.sql.*; //needed for database connection /* In order to create a frame, we need to extend Jframe. Extending Jframe means that our class is inherited from Jframe class. Extends is a keyword user for inheritance. We can inherit only one class. */ public class UserInterfaceDesign extends JFrame { //Firstly all the necessary variables are created //Jlabel is used to create label.. This code creates a label named User ID JLabel user_id_label = new JLabel("User ID:"); //this code creates a tetfield of size 10 to enter the user ID JTextField user_id_field = new JTextField(10); //this line creates another label called Password JLabel password_label = new Jlabel("Password:"); //this code creates a tetfield of size 10 to enter the password JPasswordField password_field = new JPasswordField(10); //this line creates another label called Account Type JLabel account_type_label = new JLabel("Account Type:"); //Radio Button is used for Account Type //This code is used to create a radio button with name Savings and Current JRadioButton account_type_radio_button1 = new JRadioButton("Savings");

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); //A new object of ButtonGroup is created. These are created such that only one radio button //could be choosed at a time ButtonGroup account_type_button_group = new ButtonGroup(); //Jbutton is used to create a button. This creates a butto with name Login JButton login_button = new Jbutton("LogIn"); //Creates button with text Cancel JButton cancel_button = new Jbutton("Cancel"); //Creates button with text edit JButton edit_button = new Jbutton("Edit"); /* Required variable declaration is completed. Till now, these GUI components will not be displayed to users as we have only created a object of all the required GUI components. In order to make them visible, we must create a Jpanel and then add all these components to Jpanel. This is done inside the default constructor. */ public UserInterfaceDesign() { //Firstly, the radio buttons are added to a button group such that user can only select one option at a time account_type_button_group.add(account_type_radio_button1); account_type_button_group.add(account_type_radio_button2); //Creates a main panel and set its layout to be FlowLayout with all its child to be left aligned JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); // Now all the components are added to the panel p1.add(user_id_label); p1.add(user_id_field); p1.add(password_label);p1.add(password_field); p1.add(account_type_label); p1.add(account_type_radio_button1); p1.add(account_type_radio_button2); //Now, finally, the panel is added to the Jframe with BorderLayout.NORTH such that the panel is places in north direction add(p1,borderlayout.north);

//Now, next panel is created to add all the buttons JPanel p2 = new JPanel(new FlowLayout(50)); //All buttons are added in this panel p2.add(login_button); p2.add(cancel_button); p2.add(edit_button); //Finally, the panel with button is added to Jframe in south direction add(p2,borderlayout.south); //Now, the click events need to be defined. Since click is a action, so firstly, action listener is //added to the button login_button.addactionlistener(new ActionListener() { // In order to perform click event, following function must be overridden @Override public void actionperformed(actionevent e) { try{ // Loads the driver Class.forName("com.mysql.jdbc.Driver"); // Connects java to mysql where jdbc:mysql://localhost:3306/user is the path of the //driver, root is the username and is the password Connection c =DriverManager.getConnection ("jdbc:mysql://localhost:3306/user", "root", ); //Sends every parameter to mysql Statement s = c.createstatement(); /*In order to get the text from the user_id_field textfield, gettext() method is used. This method gets the text written in the textfield. This method returns string by default, hence Integer.parseInt() method is used to convert this string to int and store it in int*/ int id = Integer.parseInt(user_id_field.getText()); /* for password, the process is little different. Since password is not written in plaintext, so getpassword() method is used to get the text of password field. This method writtens char[] by default which is converted to string by String.valueOf() function.*/ char [] pass = password_field.getpassword();

); String passwd = String.valueOf (pass); //ID and password are displayed in console System.out.println("ID:"+id+"\tPassword:"+passwd); //database sql query String sql = "SELECT * FROM user_table WHERE user_id = "+id+" AND password='"+passwd+"'"; //r stores the result set after the sql query is executed ResultSet r = s.executequery(sql); //moves cursor to the first row, i.e. if first row is preset if(r.first()) { JOptionPane.showMessageDialog(new JFrame(), "Log In Successful!!!" ); else { JOptionPane.showMessageDialog(null, "Incorrect UserID/Password"); catch (Exception ex){ System.out.println("Exception occurred"); //Code when cancel button is clicked cancel_button.addactionlistener(new ActionListener() { @Override public void actionperformed(actionevent e) { // exits current program by terminating running Java virtual machine System.exit(0); ); setsize(700,300); //set the size of JFrame setvisible(true); // Finally, making the Jframe visible on user s screen

setdefaultcloseoperation(jframe.exit_on_close); // exit the program on clicking cross button of frame //Main Method public static void main(string []args) { new UserInterfaceDesign();