Java swings.

Slides:



Advertisements
Similar presentations
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Advertisements

Distributed Application Development B. Ramamurthy.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Lecture 19 Graphics User Interfaces (GUIs)
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
GUI Programming in Java
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Software Construction Lecture 10 Frameworks
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Learning objectives By the end of this lecture you should be able to:  have a well-earned rest! Ch 24 Beyond the second semester.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
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 Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
JDBC - Connection The programming involved to establish a JDBC connection is fairly simple. Here are these simple four steps − Import JDBC Packages: Add.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
JDBC.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Lecture 11 Object Oriented Programming Using Java
Welcome To java
Interacting with Database
JDBC Database Management Database connectivity
A First Look at GUI Applications
Java GUI.
Lecture 27 Creating Custom GUIs
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Java Tutorial Zhe Li.
Interacting with Database
Super Market Management
Java Database Connectivity
Week 8 Swing NetBeans GUI Builder
Constructors, GUI’s(Using Swing) and ActionListner
JAVA DATABaSE CONNECTIVITY
JDBC Example.
Chapter 12 GUI Basics.
Lecture 4: Standard Java Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Java swings

WHAT IS SWINGS? Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API for providing a graphical user interface (GUI) for Java programs. Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feelthat allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists. Unlike AWT components, Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element. Swing is currently in the process of being replaced by JavaFX

JDBC

Difference between AWT and Swing

The JDBC API provides the following interfaces and classes − DriverManager: This class manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication sub protocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection. Driver: This interface handles the communications with the database server. You will interact directly with Driver objects very rarely. Instead, you use DriverManager objects, which manages objects of this type. It also abstracts the details associated with working with Driver objects. Connection: This interface with all methods for contacting a database. The connection object represents communication context, i.e., all communication with database is through connection object only. Statement: You use objects created from this interface to submit the SQL statements to the database. Some derived interfaces accept parameters in addition to executing stored procedures. ResultSet: These objects hold data retrieved from a database after you execute an SQL query using Statement objects. It acts as an iterator to allow you to move through its data. SQLException: This class handles any errors that occur in a database application.

Creating JDBC Application There are following six steps involved in building a JDBC application − Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice. Register the JDBC driver: Requires that you initialize a driver so you can open a communication channel with the database. Two methods : class.forname() and DriverManager.registerDriver() try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch(ClassNotFoundException ex) { System.out.println("Error: unable to load driver class!"); System.exit(1);

RDBMS JDBC driver name URL format MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/ databaseName ORACLE oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:port Number:databaseName DB2 COM.ibm.db2.jdbc.net.DB2Driver jdbc:db2:hostname:port Number/databaseName Sybase com.sybase.jdbc.SybDriver jdbc:sybase:Tds:hostname: port Number/databaseName Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database. Execute a query: Requires using an object of type Statement for building and submitting an SQL statement to the database. Extract data from result set: Requires that you use the appropriate ResultSet.getXXX() method to retrieve the data from the result set. Clean up the environment: Requires explicitly closing all database resources versus relying on the JVM's garbage collection.

Registration form TEXT FIELDS LABELS RADIO BUTTONS COMBO BOX BUTTON

SWINGS Package use - javax.swing.* Provides a set of light weight components that work the same on all platforms. Classes under swing package - AbstractButtons , ButtonGroup , JFrame JComponent , JButton ….. JFrame- The class JFrame is an extended version of Java.awt.Frame that adds support to swing components architecture. Top level swing container – It provides the support that the swing components need to perform their painting and event handling.

JLABEL import javax.swing.*; import java.awt.event.*; public class MyLabel extends JFrame { JLabel L1 = new JLabel(“REGISTRATION FORM”); JLabel L2 = new JLabel(“name”); JLabel L3 = new Jlabel(“roll no”); JLabel L4 = new Jlabel(“gender”); JLabel L5= new JLabel(“course”); add(L1); . add(L2); A display area for a short text string or an image, or both. add(L3); add(L4); add(L5); setLayout(null); L1.setBounds(50,100,50,100); L2.setBounds(50,120,50,100); L3.setBounds(50,140,50,100); L4.setBounds(50,160,50,100); L5.setBounds(50,180,50,100); setVisible(true); }

JTEXTFIELD JTextField is used for taking input of single line of text . import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MyTextField extends JFrame { JTextField jtf = new JTextField(20); //creating JTextField JTextField jtf2 = new JTextField(20); add(jtf); setLayout(null); jtf.setBounds(80,100,50,100); . add(jtf2); //adding JTextField to frame. Jtf2.setBounds(80,120,50,100); setSize(400, 400); setVisible(true); }

jRADIOBUTTON Radio button is a group of related button in which only one can be selected. JRadioButton class is used to create a radio button in Frames. import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Test extends JFrame { JRadioButton r1 = new JRadioButton(“Male"); //creating JRadioButton. add(r1); setLayout(null); r1.setBounds(80,160,50,100); //adding JRadioButton to frame. r2 = new JRadioButton(“Female"); //creating JRadioButton. add(r2); //adding JRadioButton to frame. r2.setBounds(100,100,50,100); setSize(400, 400); setVisible(true); }

JCOMBOBOX Combo box is a combination of text fields and drop-down list . JComboBox component is used to create a combo box in Swing. import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Test extends JFrame { String course[] = {“CSE",“ECE"}; //list of course JComboBox jc = new JComboBox(course); //initialzing combo box add(jc); setLayout(null); //adding JComboBox to frame Jc.setBounds( 100,180,50,100); setSize(400, 400); setVisible(true); }

JBUTTON It allows a button to be created using icon, a string or both. JButton supports Action Event. When a button is pressed an Action Event is generated import javax.swing.*; import java.awt.event.*; import java.awt.*; public class testswing extends JFrame implements ActionListener { JButton bt = new JButton(“SUBMIT"); //Creating a Button. setLayout(null); setSize(400, 400); //setting size of Jframe add(bt); //adding button to frame. bt.addActionListener(this); setVisible(true); } Public void action Performed(Action Event e) { if(bt is Selected()) JOptionPane.showMessagedialog(this,”Successfully Registered”);

PALETTE Drag-and-drop the components from here onto to the JFrame. The code automatically gets written in the Source. Select a component in the palette, move the cursor to the Design or Structure view and click where you want to add the component.

LOGIN PAGE INSERT UPDATE DELETE FETCH

Insert data into table void jButtonActionPerformed(java.awt.event.A) { String uname=t1.getText(); String pass=t2.getText(); try Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”); PreparedStatement pst=con.prepareStatement(“insert into register2 values(?,?)”); pst.setString(1,uname); pst.setString(2,pass); int x=pst.executeUpdate(); if(x>0) JOptionPane.showMessageDialog(this,”User Successfully Registered”); } catch(Exception e) System.out.println(e);

Update password try { Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”syst em”); PreparedStatement pst=con.prepareStatement(“Update register2 set password=? where username=?”; pst.setString(1,uname); pst.setString(2,pass); int x=pst.executeUpdate(); if(x>0) JOptionPane.showMessageDialog(this,”Password Successfully Updated”); } catch(Exception e) System.out.println(e);

delete try { Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”, ”system”,”system”); PreparedStatement pst=con.prepareStatement(“Delete from register2 where username=?”); pst.setString(1,uname); } catch(Exception e) System.out.println(e);

Fetch data from table try { Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”); PreparedStatement pst=con.prepareStatement(“Select * from register2 where username=?”); pst.setString(1,uname); ResultSet x=pst.executeQuery(); if(x.next()) String name=x.getString(1); String pass=x.getString(2); JOptionPane.showMessageDialog(this,”Password is”+pass); jLabelname1.setText(name); jLabelname2.setText(pass); } catch(Exception e) System.out.println(e);