[Java Tutorial #5] - How to create GUI java aplication for checking The Estimated Reward in Last Week 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 try to explain how to create a java application to check the amount of rewards received by some author in last week. This reward consists of SBD, STEEM, STEEM POWER and VEST that get by some author in a week ago. To create this aplication we need to grab the data from steemit, and in this tutorial I use API from https://helloacm.com/tools/steemit/ . You can see the aplication on image bellow :



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 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 and count the price. Here I rename with getData() method.
public void getData() throws Exception{
}
  • Get The author name from user input.
String Author=UserName.getText();
 String Data = getApi("https://uploadbeta.com/api/steemit/payout7/?cached&id="+Author);
 import org.json.JSONArray;
import org.json.JSONObject;  
  • Because the Data is in Array form(you see on image bellow) so we need to convert it to JSONArray
JSONArray ArrData=new JSONArray(Data);
  • Now, Convert it to JSONObject
JSONObject ObjData=ArrData.getJSONObject(0);
  • Get Some element of object and display it on the form
      sbd.setText(""+ObjData.getDouble("sbd"));
       steem.setText(""+ObjData.getDouble("steem"));
       sp.setText(""+ObjData.getDouble("sp"));
       vests.setText(""+ObjData.getDouble("vests"));


CodeExplanation
ObjData.getDouble("sbd")we use get Double because the value of sbd element in data is in double type.
""+we combine the text with double to convert double to string because the setText method need parameter in string type.
  • Call the getData() method in button. Open the aplication design form and double click on button. then add this code.
        try {
            getData();
        } catch (Exception ex) {
            Logger.getLogger(Reward.class.getName()).log(Level.SEVERE, null, ex);
        }
  • Now try to run the aplication, and input some author name then press check 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/242a87b4bf2b9f7d7c5d3653bb3c9e6d

Sort:  

Go here https://steemit.com/@a-a-a to get your post resteemed to over 72,000 followers.

Thank you for your contribution.

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.31
TRX 0.12
JST 0.033
BTC 64485.37
ETH 3156.53
USDT 1.00
SBD 4.05