0% found this document useful (0 votes)
68 views58 pages

Net Beans

The document describes 9 programs created in Java Netbeans to demonstrate different programming concepts. Program 1 displays messages in labels, text boxes and dialog boxes. Program 2 concatenates names entered in text fields. Program 3 builds a basic calculator app. Program 4 determines profit or loss. Program 5 calculates marks, percentage and grade. Program 6 calculates discounts. Program 7 changes label colors. Program 8 displays multiplication tables. Program 9 stores student data in a table. The programs cover GUI applications, arithmetic, logical operations, if/else statements, and using components like buttons, text fields and tables.

Uploaded by

Nabeel Undre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views58 pages

Net Beans

The document describes 9 programs created in Java Netbeans to demonstrate different programming concepts. Program 1 displays messages in labels, text boxes and dialog boxes. Program 2 concatenates names entered in text fields. Program 3 builds a basic calculator app. Program 4 determines profit or loss. Program 5 calculates marks, percentage and grade. Program 6 calculates discounts. Program 7 changes label colors. Program 8 displays multiplication tables. Program 9 stores student data in a table. The programs cover GUI applications, arithmetic, logical operations, if/else statements, and using components like buttons, text fields and tables.

Uploaded by

Nabeel Undre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

PROBLEM

SOLVING USING
JAVA NETBEANS
PROGRAM 1:
Objective: To display a message using Label, Text Box,
Message Dialog using simple GUI applications
Task: Develop an Application to display message in
Message dialog, Label and Text Field.
DESIGN

SOURCE CODE
CODE FOR Click here for Message Dialog Box BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
JOptionPane.showMessageDialog(this,"Hello...! Welcome
to NetBeans");
}
CODE FOR Click here to Display Message in Label
BUTTON
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("Hello...! Have a Nice Day");
}
CODE FOR Click here to Display Message in Text Box
BUTTON
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("Welcome");
}

OUTPUT
PROGRAM 2:
Objective: To concatenate two text entries and display
using simple GUI application.
Task: Develop an Application to concatenate First name
and Second name and display the full name.
DESIGN

CODE FOR CONCAT BUTTON


private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String fname, sname,fullname;
fname= jTextField1.getText();
sname= jTextField2.getText();
fullname=fname.concat(sname);
jTextField3.setText(fullname); }
OUTPUT
PROGRAM 3:
Objective: To perform a simple arithmetic operation (+,-,*,
/) and display the result in MessageDialog or TextBox
using simple GUI application
Task: Develop a Simple Calculator to perform different
arithmetic operations: +,-,* and /.
DESIGN

SOURCE CODE
//Global declaration
int a,b,c;

CODE FOR + BUTTON


private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {

a=Integer.parseInt(jTextField1.getText());

b=Integer.parseInt(jTextField2.getText());
c=a+b;

jTextField3.setText(Integer.toString(c));

CODE FOR - BUTTON


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

a=Integer.parseInt(jTextField1.getText());

b=Integer.parseInt(jTextField2.getText());

c=a-b;

jTextField3.setText(Integer.toString(c));

CODE FOR * BUTTON


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

a=Integer.parseInt(jTextField1.getText());

b=Integer.parseInt(jTextField2.getText());

c=a*b;

jTextField3.setText(Integer.toString(c));

CODE FOR / BUTTON


private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

a=Integer.parseInt(jTextField1.getText());

b=Integer.parseInt(jTextField2.getText());

c=a/b;

jTextField3.setText(Integer.toString(c));

}
CODE FOR CLEAR BUTTON
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

CODE FOR STOP BUTTON


private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);

OUTPUT
OUTPUT ON CLICKING + BUTTON
OUTPUT ON CLICKING * BUTTON
PROGRAM 4:
Objective: To make simple decision making (if statement)
solution and display relevant message using GUI
application.
Task: Develop an Application to find profit or loss by
accepting cost price and selling price.
DESIGN

SOURCE CODE
CODE FOR PROFIT/LOSS BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int cp,sp;
cp=Integer.parseInt(jTextField1.getText());
sp=Integer.parseInt(jTextField2.getText());
if(cp>sp)
JOptionPane.showMessageDialog(this,"It is a
Profit....!");
else
JOptionPane.showMessageDialog(this,"It is a
Loss....!");
}

OUTPUT
PROGRAM 5:
Objective: To create a simple GUI application to perform
both arithmetic and logical operation together.
Task: Develop an Application to accept the student
admission number, name, marks in different subjects and
Calculate & Display the total mark, percentage and grade
of the student.
DESIGN
SOURCE CODE
//Global declaration
double percentage;

CODE FOR TOTAL BUTTON


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int m1,m2,m3,m4,m5;

int total;

m1=Integer.parseInt(jTextField3.getText());

m2=Integer.parseInt(jTextField4.getText());

m3=Integer.parseInt(jTextField5.getText());

m4=Integer.parseInt(jTextField6.getText());

m5=Integer.parseInt(jTextField7.getText());

total=(m1+m2+m3+m4+m5);

percentage=(total*100)/500;

jTextField8.setText(Integer.toString(total));

CODE FOR PERCENTAGE BUTTON


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField9.setText(Double.toString(percentage));

if(percentage>=90)

jTextField10.setText("A1");

else if(percentage>=80 && percentage<90)

jTextField10.setText("A2");

else if(percentage>=70 && percentage<80 )


jTextField10.setText("B1");

else if(percentage>=60 && percentage<70 )

jTextField10.setText("B2");

else if(percentage>=50 && percentage<60 )

jTextField10.setText("C1");

else if(percentage>=40 && percentage<50 )

jTextField10.setText("C2");

else if(percentage>=30 && percentage<40 )

jTextField10.setText("D");

else

jTextField10.setText("E");

CODE FOR CLEAR BUTTON


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jTextField4.setText("");

jTextField5.setText("");

jTextField6.setText("");

jTextField7.setText("");

jTextField8.setText("");

jTextField9.setText("");

jTextField10.setText(""); }
CODE FOR EXIT BUTTON
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0); }

OUTPUT
PROGRAM 6:
Objective: To create a simple GUI application to perform
an operation based on the criteria input by the user in a
CheckBox or a RadioButton.
Task: Develop an Application to calculate the discount as
per the option given by the user.
DESIGN

SOURCE CODE
CODE FOR Click for Discount BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int unit;
float total=0;
float discount=0;
unit= Integer.parseInt(jTextField1.getText());
if(jRadioButton1.isSelected())
{
if(unit>=1 && unit <=15)
total=unit*50;
else if(unit>=16 && unit <=30)
total=unit*45;
else if(unit>=31 && unit <=40)
total=unit*35;
else if(unit>50)
total=unit*30;
}
if(jRadioButton2.isSelected())
{
if(unit>=1 && unit <=15)
total=unit*60;
else if(unit>=16 && unit <=20)
total=unit*55;
else if(unit>=21 && unit <=30)
total=unit*50;
else if(unit>=31 && unit<=40)
total=unit*45;
else if(unit>50)
total=unit*40;
}
if(jCheckBox1.isSelected())
discount=total*(float)0.1;

jTextField2.setText(Float.toString(total));
jTextField3.setText(Float.toString(discount));
}
OUTPUT
PROGRAM 7:
Objective: To create a simple GUI application to change
the property of a swing element based on the selection
made by the user.

Task: Develop an Application to change the background


colour of a Label based on the selection made by the user.
DESIGN

SOURCE CODE
CODE FOR Click to Change Color BOTTUON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setOpaque(true);
switch(jList1.getSelectedIndex())
{
case 0: jLabel1.setBackground(java.awt.Color.RED);
break;
case 1: jLabel1.setBackground(java.awt.Color.BLUE);
break;
case 2:jLabel1.setBackground(java.awt.Color.GREEN);
break;
case 3:
jLabel1.setBackground(java.awt.Color.YELLOW);
break;
case 4: jLabel1.setBackground(java.awt.Color.PINK);
break;
}
}
OUTPUT
PROGRAM 8:
Objective: To create a simple GUI application for
repeatedly doing a task based on the user input.
Task: Develop an Application to display the multiplication
table of a number input by the user.
DESIGN

SOURCE CODE
CODE FOR Click to Show Table BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
int num=Integer.parseInt(jTextField1.getText());
int q=1;
while(q<=10)
{
jTextArea1.append(q+"X"+num+"="+q*num+"\n");
q++;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Enter the
valid number");
}
jTextField1.setText("");
}
CODE FOR Reset BUTTON
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");

jTextArea1.setText("");

}
OUTPUT
PROGRAM 9:
Objective: Understanding the use of jTable component to
store the data.
Task: Develop an Application to store the data (Admission
No, Name, Date of Birth, Grade and Division) of 5 students
in a table.
DESIGN
SOURCE CODE
CODE FOR Click to Add BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int admno=Integer.parseInt(jTextField1.getText());
String name=jTextField2.getText();
String dob=jTextField3.getText();
String classdiv=jTextField4.getText();
DefaultTableModel
model=(DefaultTableModel)jTable1.getModel();
model.addRow(new Object[]{admno,name,dob,classdiv});
}
CODE FOR Re-Set BUTTON
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}
CODE FOR Exit BUTTON
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

OUTPUT
CONNECTIVITY
PROBLEMS:
JAVA NETBEANS
&
MySQL
PROGRAM 10:
Objective: To store data to a database through the java
net beans front end.
Task: Develop an Application to store the contact details
to a table.
DESIGN

SOURCE CODE
Import statements:
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
CODE FOR Add BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String Name=jTextField1.getText();
String Mobile=jTextField2.getText();
String Email=jTextField3.getText();
if(Name.isEmpty())
{
JOptionPane.showMessageDialog (this, "Enter the
name");
}
else if(Mobile.isEmpty())
{
JOptionPane.showMessageDialog (this, "Enter the
mobile");
}
else if(Email.isEmpty())
{
JOptionPane.showMessageDialog (this, "Enter the
email");
}
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection
("jdbc:mysql://localhost:3306/school","root", "123");
Statement stmt = (Statement) con.createStatement();
String query="INSERT INTO contact VALUES
('"+Name+"','"+Mobile+"','"+Email+"');";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog (this, "Inserted");
}
catch(Exception e)
{
JOptionPane.showMessageDialog (this,
e.getMessage());
}
}
CODE FOR Exit BUTTON
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
CODE FOR Clear BUTTON
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}

OUTPUT
mysql> describe contact;
+-----------+------------------+---------+-------+------------+----------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+---------+-------+------------+----------+
| name | varchar(15) | YES | | NULL | |
| mobile | varchar(13) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-----------+------------------+---------+-------+------------+----------+
3 rows in set (0.00 sec)

mysql> select * from contact;


+---------------------+--------------------------+--------------------------+
| name | mobile | email |
+---------------------+--------------------------+--------------------------+
| Jithin Mohan | +971525267455 | [email protected] |
| Ram Kumar | +971565347877 | [email protected] |
+----------------------+-------------------------+--------------------------+
2 rows in set (0.00 sec)
PROGRAM 11:
Objective: To retrieve data from a database table through
the java net beans front end.
Task: Develop an Application that displays & counts the
contacts from the contact table. (Displays in a tabular
format using jTable on the GUI form).
DESIGN

SOURCE CODE
Import statements:
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

CODE FOR Click to Display Contacts BUTTON


private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int rowcount=0;
DefaultTableModel model = (DefaultTableModel)
jTable1.getModel();
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection
("jdbc:mysql://localhost:3306/school","root", "123");
Statement stmt = (Statement) con.createStatement();
String query="SELECT * FROM contact";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
String Name = rs.getString("Name");
String Mobile = rs.getString("Mobile");
String Email = rs.getString("Email");
model.addRow (new Object[] {Name, Mobile,Email});
rowcount++;
}
JOptionPane.showMessageDialog (this, "Total number of
contacts in the table is: "+rowcount);
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
}
}
mysql> describe contact;
+-----------+------------------+---------+-------+------------+----------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+---------+-------+------------+----------+
| name | varchar(15) | YES | | NULL | |
| mobile | varchar(13) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-----------+------------------+---------+-------+------------+----------+
3 rows in set (0.00 sec)

mysql> select * from contact;


+---------------------+--------------------------+--------------------------+
| name | mobile | email |
+---------------------+--------------------------+--------------------------+
| Jithin Mohan | +971525267455 | [email protected] |
| Ram Kumar | +971565347877 | [email protected] |
+----------------------+-------------------------+--------------------------+
2 rows in set (0.00 sec)
OUTPUT
PROGRAM 12:
Objective: To retrieve data from a table based on the
criteria input given by the user through the java net
beans front end.
Task: Develop an Application to search & display the
Mobile & Email of the Contact by giving the Name as
input for search.
DESIGN
SOURCE CODE
Import statements:
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;
CODE FOR Search BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name=jTextField1.getText();
DefaultTableModel model = (DefaultTableModel)
jTable1.getModel();
if (name.isEmpty())
JOptionPane.showMessageDialog(this,"Name not
Entered");
else
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/s
chool", "root","123");
Statement stmt = (Statement) con.createStatement();
String query="SELECT Name,Mobile,Email FROM contact
WHERE Name='"+name+"';";
ResultSet rs=stmt.executeQuery(query);
int Found=0;
while(rs.next())
{
String mobile = rs.getString("Mobile");
String email = rs.getString("Email");
String namee = rs.getString("Name");
model.addRow (new Object[] {namee, mobile,email});
jTextField2.setText(mobile);
jTextField3.setText(email);
JOptionPane.showMessageDialog(null,"Click OK to
continue!!!");
Found++;
}
if (Found==0)
JOptionPane.showMessageDialog(this,"Sorry! No such Name
in Contact List");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
CODE Exit BUTTON
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
OUTPUT
mysql> describe contact;
+-----------+------------------+---------+-------+------------+----------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+---------+-------+------------+----------+
| name | varchar(15) | YES | | NULL | |
| mobile | varchar(13) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-----------+------------------+---------+-------+------------+----------+
3 rows in set (0.00 sec)

mysql> select * from contact;


+---------------------+--------------------------+--------------------------+
| name | mobile | email |
+---------------------+--------------------------+--------------------------+
| Jithin Mohan | +971525267455 | [email protected] |
| Ram Kumar | +971565347877 | [email protected] |
+----------------------+-------------------------+--------------------------+
2 rows in set (0.00 sec)
PROGRAM 13:
Objective: To update the data of a table based on the
criteria input given by the user through the java net
beans front end.
Task: Develop an Application to update the Mobile &
Email of the Contact by giving the Name as input for
search.
DESIGN

SOURCE CODE
Import statements:
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
CODE FOR Load BUTTON
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name=jTextField1.getText();
if (name.isEmpty())
JOptionPane.showMessageDialog(this,"Name not
Entered");
else
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/s
chool", "root","123");
Statement stmt = (Statement) con.createStatement();
String query="SELECT Name,Mobile,Email FROM contact
WHERE Name='"+name+"';";
ResultSet rs=stmt.executeQuery(query);
int Found=0;
while(rs.next())
{
String mobile = rs.getString("Mobile");
String email = rs.getString("Email");
String namee = rs.getString("Name");
jTextField1.setText(namee);
jTextField2.setText(mobile);
jTextField3.setText(email);
JOptionPane.showMessageDialog(null,"Click OK to
continue!!!");
Found++;
}
if (Found==0)
JOptionPane.showMessageDialog(this,"Sorry! No such Name
in Contact List");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
CODE FOR Update BUTTON
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int ans = JOptionPane.showConfirmDialog(null,"Surely wanna
Update The Record");
if( ans == JOptionPane.YES_OPTION){
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)

DriverManager.getConnection("jdbc:mysql://localhost:3306/s
chool", "root","123");
Statement stmt = (Statement) con.createStatement();
String name=jTextField1.getText();
String query = "Update contact Set mobile
='"+jTextField2.getText()+"',email='"+jTextField3.getText()+"'
where name ='"+name+"';";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Record
Susscefully Updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in
Connectivity");
}
}
}
CODE FOR Clear BUTTON
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
CODE FOR Exit BUTTON
private void
jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
OUPUT

You might also like