Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java swings.

Similar presentations


Presentation on theme: "Java swings."— Presentation transcript:

1 Java swings

2 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

3 JDBC

4 Difference between AWT and Swing

5 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.

6 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);

7 RDBMS JDBC driver name URL format MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/ databaseName ORACLE oracle.jdbc.driver.OracleDriver 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.

8 Registration form TEXT FIELDS LABELS RADIO BUTTONS COMBO BOX BUTTON

9 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.

10 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); }

11 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); }

12 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); }

13 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); }

14 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”);

15 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.

16 LOGIN PAGE INSERT UPDATE DELETE FETCH

17 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”); 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);

18

19 Update password try { Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); 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);

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

21 Fetch data from table try { Connection con=null; Class.forName(“oracle.jdbc.driver.OracleDriver”); 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);


Download ppt "Java swings."

Similar presentations


Ads by Google