Change look and feel of your jframe(Java)

Today we will see how change "Look and feel of your application"

Normally running jframe made by NetBeans, look of jframe called "Nimbus".

It is default previwe comes with NetBeans.

Following image is example for look of "Nimbus".
We can change it with NetBeans to a 5 types.


  • Metal
  • Nimbus
  • CDE/Motif
  • Windows
  • Windows Classic

You can test the preview before change it by following steps.


  1. Right click on your jframe
  2. Go to Preview Design
  3. Choose Metal,Windows or somthing
  4. jframe will show how it look like

Now we will see how change this permanently

  1. Go to your source code
  2. Go to botom of your code and find the main method of your codes
  3. In this picture you can see code like this /*Set the Nimbus look the form*/
  4. Right side of this code can you see some you can see "+" mark. Click it and extract code there.
  5. You can see here it default set as Nimbus
  6. To change that simply delete Nimbus and change it to your choice to a Windows,etc.
  7. Following images show you change of this 5 types
  8. Metal

    Nimbus

    CDE/Motif

    Windows

    Windows Classic

How make a new Java project using NetBeans(Java)

Make a new project in NetBeans. If you don't have NetBeans please download it from following link

NetBeans



Select "Java" and "Java Application"  click Next.


Now give a name for a your project & set path where your project to save.

Unchecked  Create main class and click finish.


Your project will create. It will show in your left side.

Double click on your project & again double click on Source Packages .

Right click on <default package>, New,  JFrame Form...


Give a name & Finish.


Now your Frame is created. 


Next time we will see How send a e-mail by using Java application.







How send e-mail with java(e-mail sending application)

Today I'm going to send a e-mail using java application. In here I'm using G mail SMTP(Simple Mail Transfer Protocol ) server.

First you have to download JavaMail API. Using following link you can download it.

JavaMail API1.4.5

Download it and extract it .

Open your last NetBeans Java project.

Now you have to add you downloaded JAR file to your project.

To do that  Right click on your Project Libraries and click "Add JAR/Folder..."


Go to your extracted folder.


Select mail.jar and click "Open".

Next again go to "Add JAR/Folder" and go to folder "bil" select all jar file their and "Open".


Now adding libraries part is over .

Go to your form and add a button . I name it as "Send Mail". I change it variable name as "btnMail".

OK.

Now it is cording part..

First you have to import following 

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

Double click on your button and type this codes..

        Properties props= new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocket");
        props.put("mail.smtl.auth", "true");
        props.put("mail.smtp.port", "465");
        
        Session ssn= Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("myemail@gmail.com", "password");
                
                
                }
                }
                
                
                
                );
        try{
            Message msg=new MimeMessage(ssn);
            msg.setFrom(new InternetAddress("myemail@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("youremail@gmail.com"));
            msg.setSubject("subject");
            msg.setText("Mail Text");
            Transport.send(msg);
            
            JOptionPane.showMessageDialog(null, "sent");
        }
        catch(Exception e){JOptionPane.showMessageDialog(null, e);}



In hear
  • myemail@gmail.com is your e mail adress
  • password is your password
  • youremail@gmail.com is receiver's e mail adress.
  • Subject is email sibject
  • Mail text is message
You change them own ...

here you can use only g mail address only. Because we here we use G Mail SMTP server.

If you have any problem please leave a comment..









How show current date & time in your application (C#)

Hello ...

Today I'm going to show you how add current date and time in your application .

First open your recent Windows form,


Go toToolbox and serch for "Timer" then drag & drop it. Timer is not a tool like button or label etc.

After add the time you can see it under the your form as separately.



Add a Label to your form where you want to show date & time. I change it name as time.

Double click on timer and go to codes, Enter code below..

DateTime dateTime= DateTime.Now;
this.time.Text = dateTime.ToString();


 Next go upper of your code & find the
InitializeComponent();
It is in your main Form method,

after find it you have to type this code

timer1.Start();

If you have any trouble please see the image below.


Now current Date & time will show in your application..


If You have any problem please leave a comment or message to me..








Update & Delete data from database (C#)

Add two button "Update" & "Delete"

In hear i added icon image to my button for it's look like better..

To add a image go to Properties , Image


Click "Import" & choose your icon. Then click OK.



Icon will add to button. But it's not looks like better..

Go to Properties ,ImageAlign, & set it as MiddleLeft.

Now it's look like better.


Now add this codes

Update
string conString = "datasource=localhost;port=3306;username=root;password=";
            string Query = "update  blog.empdata set  FName='" + this.txtFName.Text + "',LName='" + this.txtLName.Text + "',Age='" + this.txtAge.Text + "'where FName='" + this.txtFName.Text + "';";
            MySqlConnection conDataBase = new MySqlConnection(conString);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                MessageBox.Show("Updated");
                while (myReader.Read())
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Delete
 string conString = "datasource=localhost;port=3306;username=root;password=";
            string Query = "delete from  blog.empdata where FName='" + this.txtFName.Text + "';";
            MySqlConnection conDataBase = new MySqlConnection(conString);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                MessageBox.Show("Deleted");
                while (myReader.Read())
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            }

Add data to database (C#)

Open your previous Windows application we made.

Add Fields to your windows form where you need to add to database.

As a example hear, I used First Name, Last Name, Age fields to add data.

In hear I added 3 labels and 3 TextBox to my form

I change the TextBox names to ,

  • First Name - txtFName
  • LastName - txtLName
  • Age - txtAge
Add a button, I name it as "Save"

Double click on button & type this code hear..

string conString = "datasource=localhost;port=3306;username=root;password=";

            string Query = "insert into blog.empdata(fname,lName,age) values('" + this.txtFName.Text + "','"+this.txtLName.Text+"','"+this.txtAge.Text+"');";

            MySqlConnection conDataBase = new MySqlConnection(conString);

            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);

            MySqlDataReader myReader;

            try

            {

                conDataBase.Open();

                myReader = cmdDataBase.ExecuteReader();

                MessageBox.Show("Saved");

                while (myReader.Read())

                {


                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }




If your code is correct Message box will show the message "Saved"

That mean your data is saved in your database..


My data saved in my database


Before you try this you must have made a database named blog(something else ) and table named empdata(something else)

If you want to know how make a database please follow this link Working with MySQL Workbench

Make a database using MySQL Workbench

You can download MySQL Workbench from this link

MySQL Workbench

After installing this , in the left side you can see all the schemes on your computer.

You can right click on schemes and select Create schema..

I made schema named blog.

Drop down the blog schema and Tables there.


By right clicking Tables choose create Table..


Enter column names and data type 
After entering data press "Apply"

It will make  SQL query and asking from you for a any errors there..

By clicking apply your database will create.





How connect your application to a Database? (C#)

1. Make a new project named "databaseConnect"

2. Drag and drop a button
3. Name it as "Connect"

4. You have to add a reference to your project. To do that go to solution explorer, right click on Reference , Add reference
5. Search for a "MySql.Data"
6. After find that tick it and click "OK"
7. The reference file will be added.
8. Import the following libery
using MySql.Data.MySqlClient;
8. Double click on the button & enter codes given below.
try
            {
                string myConnection = "datasource=localhost;port=3306;username=root;password=root";
                MySqlConnection myconn = new MySqlConnection(myConnection);
                MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                myDataAdapter.SelectCommand = new MySqlCommand("select * databaseName.tableName;", myconn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
                myconn.Open();
              
                MessageBox.Show("Connected");
                myconn.Close();
            }
catch (Exception ex)
     {
                MessageBox.Show(ex.Message);
      }



9. Now check your connection. If it is success "Connected" Message will be show.

Next time we will see how add data to database?