Group 4 Presentation
Group 4 Presentation
Lecturer : Mr S FURUSA
GROUP 4 PRESENTATION
Graphical user interface , a term used not only in java but in all programming languages that
support the development of GUI’s. A program’s graphical user interface presents an easy to use
visual display to the user. It is made up of graphical components ( e.g., buttons , labels , windows
) through which the user can interact with the page or application.
The GUI includes a range of user interface elements which just means all the elements that display
when you are working in an application. These can include:
Input controls such as buttons , dropdown lists , checkboxes and text fields.
Informational elements such as labels , banners , icons , or notification dialogs.
Navigational elements , including sidebars , breadcrumbs, and menus.
(2) creating a connection class and accessing it within the second class
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
JFrame f;
JButton b;
JComboBox cb;
JTextField name;
b= new JButton("Ok");
String country[]={"India","Aus","U.S.A","England","Newzeland"};
cb=new JComboBox(country);
lbl1.setBounds(2,50,90,20);
cb.setBounds(50, 50,90,20);
lbl2.setBounds(2,90 ,90,20);
b.setBounds(50, 130,90,20);
f.add(lbl1);
f.add(cb);
f.add(lbl2);
f.add(name);
f.add(b);
b.addActionListener(this);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
//here emp is the database name, root is username and the password is blank
Statement stmt=con.createStatement();
f.dispose();
new Dbexample();
con.close();
new Dbexample();
Method 2
- The logic behind this is that we have to first create the connection class be sure to make
the class variables public
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ON
LY);
catch(Exception e) {
System.out.println(e); }
try{
stmt.close();
con.close();
catch(Exception e)
System.out.println(e);
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
JFrame f;
JButton b;
JComboBox cb;
JTextField name;
Dbexample(){
b= new JButton("Ok");
String country[]={"India","Aus","U.S.A","England","Newzeland"};
cb=new JComboBox(country);
lbl1.setBounds(2,50,90,20);
cb.setBounds(50, 50,90,20);
lbl2.setBounds(2,90 ,90,20);
b.setBounds(50, 130,90,20);
f.add(lbl1);
f.add(cb);
f.add(lbl2);
f.add(name);
f.add(b);
b.addActionListener(this);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
if(e.getSource()==b){
try{
cn.open();
int i= cn.stmt.executeUpdate("insert into detail values(NULL, '" + name.getText() + "',' " +
cb.getSelectedItem() + " ')");
new Dbexample();
cn.close();
new Dbexample();
Databases
Java uses something called JDBC (Java Database Connectivity) to connect to databases. There's a
JDBC API, which is the programming part, and a JDBC Driver Manager, which your programmes
use to connect to the database.
JDBC allows you to connect to a wide-range of databases (Oracle, MySQL, etc), but we're going
to use the in-built database you get with the Java/NetBeans software. The database is called Java
DB, a version of Apache Derby. It runs on a virtual server, which you can stop and start from
within NetBeans.
To check that have everything you need, have a look at the Services tab in NetBeans. If you can't
see the Services tab, click Window from the NetBeans menu. From the Window menu,
select Services. You should see something like this:
The idea is that you start the Java DB virtual server, and then create and manipulate databases on
the server. There should be a database called sample already set up: (But don't worry if it's not
there as we'll create our own database.)
In the image above, there are three databases: one is called sample, one is called test1, and the
other is called exams.
For the project in this section, we're going to set up a new database. You'll then learn how to
connect to this database using Java code. The database we'll create will be a simple one-table affair,
rather than multiple tables connected together. You can indeed create multiple tables with Java
DB, but we don't want to complicate things unnecessarily.
The first thing to do is to start the server. So right click on Java DB. You'll see a menu appear.
Select Start Server:
JAVA GUI
GUI stands for Graphical User Interface, a term used not only in Java but in all programming
languages that support the development of GUIs. A program's graphical user interface presents an
easy-to-use visual display to the user. It is made up of graphical components (e.g., buttons, labels,
windows) through which the user can interact with the page or application.
To make graphical user interfaces in Java, use either Swing (older applications) or JavaFX.
Typical Elements
A GUI includes a range of user interface elements — which just means all the elements that
display when you are working in an application. These can include:
Input controls such as buttons, dropdown lists, checkboxes, and text fields.Informational elements
such as labels, banners, icons, or notification dialogs.Navigational elements, including sidebars,
breadcrumbs, and menus.
Java has included Swing, an API for creating GUIs, in its Java Standard Edition since Java 1.2, or
2007. It's designed with a modular architecture so that elements are easily plug-and-play and can
be customized. It has long been the API of choice for Java developers when creating GUIs.
JavaFX has also been around a long time — Sun Microsystems, which owned Java before the
current owner Oracle, released the first version in 2008, but it didn't really gain traction until Oracle
purchased Java from Sun.
If you're an application developer, you need to consider not only the tools and programming
widgets you will use to create your GUI, but also be aware of the user and how he will interact
with the application.
For example, is the application intuitive and easy to navigate? Can your user find what he needs
in the expected places? Be consistent and predictable about where you place things — for instance,
users are familiar with navigational elements on top menu bars or left sidebars. Adding navigation
in a right sidebar or on the bottom will only make the user experience more difficult.
Other issues might include the availability and power of any search mechanism, the behavior of
the application when an error occurs, and, of course, the general aesthetics of the application.
GUI JAVA
One of the big advances that Java made when it was introduced in 1995 was the inclusion of
standardized "graphical user interface" ("GUI") libraries. This was a tremendous leap forward
over the bewildering array of incompatible third-party libraries that were the only avenues
previously open to GUI developers. The Java GUI library was also one of the first major software
packages to explicitly describe its architecture in terms of the then-brand-new language of Design
Patterns.
Main Packages
One confusing aspect of the Java GUI system however is a legacy issue where the system
underwent a large-scale upgrade in 1997, so some of the classes involved are spread over the
(relatively) older java.awt packages and the newer javax.swing packages. In general, if the class
name starts with "J", then it is in the Swing package. Since some functionality appears to be
duplicated in the Swing packages, such as frames and buttons, always use the Swing component
over the older AWT components when there is a choice.
java.awt -- Contains the main superclasses for the GUI components plus a number of utility type
classes, such as Color and Point.java.awt.event -- Contains the classes and interfaces for managing
events from the GUI components, e.g. button clicks and mouse movements.javax.swing --
Contains most of the visible GUI components that are used such as buttons, text fields, frames and
panels.
The Java GUI subsystem consists of a separate, automous task execution thread called the "event
loop". Every action that affects the GUI, e.g. calls to repaint the screen or to manipulate the
properties of a GUI component, or is a result of something happening to the GUI, e.g. the user
clicks the mouse or hits a key, is encapsulated in the form of an "event" that is placed into a queue
for the event loop to process. The result of processing an event may be a manipulation of the bits
of color on the screen or it may result in calls to methods in the developer's code.
If we look at the process to handle the clicking of a button, what we see is that the user's mouse
click sets off a chain of events. The button object responds to the mouse click by creating a button
click event that is placed into the event queue. The event loop, when it is free to do so, picks up
that event and processes it. The processing of a button click event involves calling methods on
specially registered objects called "listeners" who are "listening" for the click event. The listeners
for a button click are objects implementing the java.awt.ActionListener interface and the button
click event processing involves calling the listener's actionPerformedmethod, which the developer
has implemented to do whatever is needed when that particular button is clicked. Note that
multiple listeners may be "added" to any given button and the button click processing will call
each ActionListener's actionPerformed method in turn.
In the other direction, if we look at the painting process, supposed the developer's code wishes to
call repaint a frame component on the screen. The developer's code will call the repaint() method
of the frame, but all that does is to generate a repaint event that is placed into the event queue along
with all the other events. When the event loop is able, it processes that repaint event and in doing
so, the GUI subsystem will generate a specialjava.awt.Graphics instance that is used to represent
the physical screen of the computer. This Graphics object is passed as an input parameter to a call
to the paintComponent method of the frame. (Technically, the system calls the
frame's paint() method which in turn callspaintComponent(), but we don't need to worry about
that.) Since the frame is a container, it as any good Composite Design pattern implementation
will do, in turn calls the paintComponent method of all of its sub-components. If one of those
components is itself a container, then the painting process cascades down the composite tree until
every component has had a chance to paint itself upon the screen (the Graphics object). The
developer can insert custom painting operations by simply overriding the paintComponent method
of the desired component.
Commonly Used ClassesSimple Components
javax.swing.JButton -- A button that can be clicked. Clicking the button will cause
its actionPerformed event to be fired which will call all
installed ActionListener's actionPerformedmethods.
javax.swing.JTextArea -- A box with multiple lines of text displayed. Can be set for editable or
non-editable.
javax.swing.JFrame -- a stand-alone "window" or "frame" with a title and the usual abilities to be
moved, resized, minimized, maximized and closed. For most people, this is the top-level
container in their system. Thus a JFrame cannot hold another JFrame. While a JFramecan hold
any type of component, typically, a JFrame holds JPanels, which are arranged to group together
sets of the components used in the window (frame). The default layout manager of a JFrame is
the BorderLayout. A JFrame has a couple of unique and important methods that are worth
pointing out:
javax.swing.JApplet -- Allows a Java program to be run as a component in a web page. Note that
an applet and a frame are not the same thing and are not interchangeable, though there are some
work-arounds for that which allow the same code to run as either a stand-alone application or an
applet in a web page.
javax.swing.JPanel -- The basic building block for laying components out in a frame or inside of
other panels. A JAva GUI consists of a frame with panels holding panels holding panels,
etc. Note that "panels" here could also be scroll panes, split panes and/or tabbed panes (see
below). The default layout manager for a JPanel is the FlowLayout.
javax.swing.JScrollPane -- Allows you to display a single panel with scroll bars on the sides,
allowing more or larger components to be displayed than will fit on the screen.
javax.swing.JSplitPane -- Allows two panels to be displayed with either a vertical or horizonatal,
user-moveable bar separating them.javax.swing.JTabbedPane -- Allows multiple panels to be
displayed in a "tabbed" format.
void add(Component c) -- adds another component to the container to be laid out as per the
currently installed layout manager. There are overridden forms of this method inherited
from java.awt.Container that allow parameters for the layout manager to be passed along, such as
specifying the component's position in aBorderLayout.
void setLayout(LayoutManager lm) -- installs a new layout manager into the container.
void setPreferredSize(int width, int height) -- sets the size of a component to be used when the
layout manager is able to use that size, e.g. there is enough room. Some layout managers always
ignore the size setting of a component because the size is under other constraints..
String getText(), void setText(String s) -- accessor methods for the text of labels, buttons, text
fields and text areas.
Utility Classes
java.awt.Graphics --This abstract class is rarely instantiated by the developer's code. A machine-
dependent instance of this class is handed to the paintComponent method of a visible GUI
component during the screen painting process -- it is NOT instantiated by the developer's code but
rather by the Java GUI sub-system. The developer can writed code that will use that
supplied Graphicsobject instance to draw lines, shapes and images onto the screen. For advanced
graphics work, it should be noted that the supplied Graphics object can always be safely downcast
to the more capablejava.awt.Graphcs2D class.
java.awt.Color -- Represents a color. Has static fields predefined to common colors plus can be
expressed in a number of different formats, such as 3-bit RGB values.java.awt.Point -- Represents
an point on 2-dimensional plane as specified byinteger-valued Cartesian coordinates.. Has fields
to retrieve the x and ycoordinates plus methods for operations such as calculating the distance
between two points. For floating point-valuedCartesian coordinates, use
thejava.awt.geom.Point2D class.
One of the big advances that Java made when it was introduced in 1995 was the inclusion of
standardized "graphical user interface" ("GUI") libraries. This was a tremendous leap forward
over the bewildering array of incompatible third-party libraries that were the only avenues
previously open to GUI developers. The Java GUI library was also one of the first major software
packages to explicitly describe its architecture in terms of the then-brand-new language of Design
Patterns.
Main Packages
One confusing aspect of the Java GUI system however is a legacy issue where the system
underwent a large-scale upgrade in 1997, so some of the classes involved are spread over the
(relatively) older java.awt packages and the newer javax.swing packages. In general, if the class
name starts with "J", then it is in the Swing package. Since some functionality appears to be
duplicated in the Swing packages, such as frames and buttons, always use the Swing component
over the older AWT components when there is a choice.
java.awt -- Contains the main superclasses for the GUI components plus a number of utility type
classes, such as Color and Point.java.awt.event -- Contains the classes and interfaces for managing
events from the GUI components, e.g. button clicks and mouse movements.javax.swing --
Contains most of the visible GUI components that are used such as buttons, text fields, frames and
panels.
The Java GUI Subsystem
The Java GUI subsystem consists of a separate, automous task execution thread called the "event
loop". Every action that affects the GUI, e.g. calls to repaint the screen or to manipulate the
properties of a GUI component, or is a result of something happening to the GUI, e.g. the user
clicks the mouse or hits a key, is encapsulated in the form of an "event" that is placed into a queue
for the event loop to process. The result of processing an event may be a manipulation of the bits
of color on the screen or it may result in calls to methods in the developer's code.
If we look at the process to handle the clicking of a button, what we see is that the user's mouse
click sets off a chain of events. The button object responds to the mouse click by creating a button
click event that is placed into the event queue. The event loop, when it is free to do so, picks up
that event and processes it. The processing of a button click event involves calling methods on
specially registered objects called "listeners" who are "listening" for the click event. The listeners
for a button click are objects implementing the java.awt.ActionListener interface and the button
click event processing involves calling the listener's actionPerformedmethod, which the developer
has implemented to do whatever is needed when that particular button is clicked. Note that
multiple listeners may be "added" to any given button and the button click processing will call
each ActionListener's actionPerformed method in turn.
In the other direction, if we look at the painting process, supposed the developer's code wishes to
call repaint a frame component on the screen. The developer's code will call the repaint() method
of the frame, but all that does is to generate a repaint event that is placed into the event queue along
with all the other events. When the event loop is able, it processes that repaint event and in doing
so, the GUI subsystem will generate a specialjava.awt.Graphics instance that is used to represent
the physical screen of the computer. This Graphics object is passed as an input parameter to a call
to the paintComponent method of the frame. (Technically, the system calls the
frame's paint() method which in turn callspaintComponent(), but we don't need to worry about
that.) Since the frame is a container, it as any good Composite Design pattern implementation
will do, in turn calls the paintComponent method of all of its sub-components. If one of those
components is itself a container, then the painting process cascades down the composite tree until
every component has had a chance to paint itself upon the screen (the Graphics object). The
developer can insert custom painting operations by simply overriding the paintComponent method
of the desired component.
javax.swing.JButton -- A button that can be clicked. Clicking the button will cause
its actionPerformed event to be fired which will call all
installed ActionListener's actionPerformedmethods.
javax.swing.JTextArea -- A box with multiple lines of text displayed. Can be set for editable or
non-editable.
javax.swing.JFrame -- a stand-alone "window" or "frame" with a title and the usual abilities to be
moved, resized, minimized, maximized and closed. For most people, this is the top-level
container in their system. Thus a JFrame cannot hold another JFrame. While a JFramecan hold
any type of component, typically, a JFrame holds JPanels, which are arranged to group together
sets of the components used in the window (frame). The default layout manager of a JFrame is
the BorderLayout. A JFrame has a couple of unique and important methods that are worth
pointing out:
javax.swing.JApplet -- Allows a Java program to be run as a component in a web page. Note that
an applet and a frame are not the same thing and are not interchangeable, though there are some
work-arounds for that which allow the same code to run as either a stand-alone application or an
applet in a web page.
javax.swing.JPanel -- The basic building block for laying components out in a frame or inside of
other panels. A JAva GUI consists of a frame with panels holding panels holding panels,
etc. Note that "panels" here could also be scroll panes, split panes and/or tabbed panes (see
below). The default layout manager for a JPanel is the FlowLayout.
javax.swing.JScrollPane -- Allows you to display a single panel with scroll bars on the sides,
allowing more or larger components to be displayed than will fit on the screen.
void add(Component c) -- adds another component to the container to be laid out as per the
currently installed layout manager. There are overridden forms of this method inherited
from java.awt.Container that allow parameters for the layout manager to be passed along, such as
specifying the component's position in aBorderLayout.
void setLayout(LayoutManager lm) -- installs a new layout manager into the container.
void setPreferredSize(int width, int height) -- sets the size of a component to be used when the
layout manager is able to use that size, e.g. there is enough room. Some layout managers always
ignore the size setting of a component because the size is under other constraints..
String getText(), void setText(String s) -- accessor methods for the text of labels, buttons, text
fields and text areas.
java.awt.Graphics --This abstract class is rarely instantiated by the developer's code. A machine-
dependent instance of this class is handed to the paintComponent method of a visible GUI
component during the screen painting process -- it is NOT instantiated by the developer's code but
rather by the Java GUI sub-system. The developer can writed code that will use that
supplied Graphicsobject instance to draw lines, shapes and images onto the screen. For advanced
graphics work, it should be noted that the supplied Graphics object can always be safely downcast
to the more capablejava.awt.Graphcs2D class.
java.awt.Color -- Represents a color. Has static fields predefined to common colors plus can be
expressed in a number of different formats, such as 3-bit RGB values.