[Java Tutorial #6] - How to create GUI java aplication for checking STEEM POWER DETAIL using Netbeans

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn How to create GUI form for this aplication
  • You will learn API to get Estimated reward Data
  • You will learn how to split data or take the important data from API
  • You will learn how to dislay data to the form

Requirements

  • Basic knowledges about java programing
  • Basic Knowledges about API
  • Need to Install Netbeans for easy pratice

Difficulty

  • Basic

Tutorial Contents

In this tutorial, I will explain how to create an app from java to check the steam power detail of a steemit account. The application we will make will show details of how much steempower efective, the amount of steampower that is delegated, the amount of steempower received and the total amount of steempower owned by an account :



Let's start the tutorial :

  • Open Netbeans IDE software and create new project. Choose Java Aplication and click next. Then Rename the project and click finish.

  • Add new jFrameForm in your Source Package

  • Rename the form

  • Click on panel component and drag it to the form

  • Add Label, TextField and Button on Panel, just follow the components on picture bellow :

    here the component detail:

  • We already have a form, Now open source and start to edit the code.

  • Create The method to get Data from URL. This method in java.net.* method. So before your start use this method import the package first

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
  • Then Create getApi() method.
public String getApi(String url) throws Exception{
        URL oracle=new URL(url);
        HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
        String inputLine;
         while ((inputLine = in.readLine()) != null){
             return inputLine;
         }  
        in.close();
        return null;   
    }


CodeExplanation
getApi()Method Name
String urlThe Parameter
throws ExceptionSome method to catch error
URL oracle=new URL(url);Decrlarating new URL
HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();class initiation to get URL connection.
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");Add browser property, Here I add Mozilla 5.-0
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));method Read input from http connection.
return inputLine;return the output
in.close();closie input stream reader



  • Now create the method to get Data from url API. Here I rename with getDeati() method.
public void getDetail() throws Exception{
}
  • Get The author name from user input.
 String User=Author.getText();
 String Data = getApi("https://uploadbeta.com/api/steemit/account/steempower/?cached&id="+User);
import org.json.JSONObject;  
  • Before spliting data we need convert it to JSONObject to easy in Spliting
 JSONObject ObjData=new JSONObject(Data);
  • Get Some important data in JSONObject and retrieve it in a variabel
     Double Esp=ObjData.getDouble("esp");
       Double Delegated=ObjData.getDouble("delegated_sp");
       Double Received=ObjData.getDouble("received_sp");


CodeExplanation
Double EspVariable decleration with the name is Esp and the type is Double
ObjData.getDouble("delegated_sp");ObjData is the Object Name, getDouble some method to get Double value from some element, And ("delegated_sp") the element name in object
  • The Api didn't provide total SP owned by some author. It just provide efectice, delegated and received SP. To get total SP owned by some autor we need to do arithmatic operation. Here the code :
Double Total=Esp-Received+Delegated;
  • Display the data to the form
      total.setText(""+Total);
       delegated.setText(""+Delegated);
       received.setText(""+Received);
       esp.setText(""+Esp);


CodeExplanation
totalthe name of textfield variabel
setText()a method to set Text in java component
""+TotalThe Variable that will be set as text in componen, because the variabel type is double we need combine with a string. So I use ""+.
  • Call the getDetail() method in button. Open the aplication design form and double click on button. then add this code.
        try {
            getDetail();
        } catch (Exception ex) {
            Logger.getLogger(SpDetail.class.getName()).log(Level.SEVERE, null, ex);
        }
  • Now try to run the aplication, and input some author name then press CHECK IT button

  • Finish, the aplication is running rightly. You can get the full code on my gist. The link is bellow

Curriculum

Proof of Work Done

https://gist.github.com/team2dev/f056957dd36d735a888e829eb4de33d5

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Screenshots: Provide better quality, clearer, and more professional screenshots
  • Avoid repetition: Frequent use of words or phrases makes reading the tutorial more annoying.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @team2dev
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.032
BTC 62710.59
ETH 3048.49
USDT 1.00
SBD 3.77