10 Kasım 2013 Pazar



prob.properties

# To change this template, choose Tools | Templates  
# and open the template in the editor.  
renk1=sarı  
renk2=kırmızı  
renk3=yesil  
PropertiesFile.java

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.FileInputStream;  
  import java.util.Properties;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class PropertiesFile {  
    public static void main(String[] args){  
      Properties properties = new Properties();  
      try{  
        //properties.load(getClass().getClassLoader().getResourceAsStream("config.properties")); ---> non-static method için properties dosyasının yüklenmesi için yöntem  
        //properties.load(new FileInputStream("external.properties")); ---> burada ise dışarıdan harici bir properties dosyasını programa yüklemek için bir yöntem  
        //properties dosyasını classpath'den yüklüyoruz.  
        properties.load(PropertiesFile.class.getClassLoader().getResourceAsStream("main/prob.properties"));  
        //properties değerlerine erişim  
        System.out.println(properties.getProperty("renk1"));  
        System.out.println(properties.getProperty("renk2"));  
        System.out.println(properties.getProperty("renk3"));  
        //properties değerlerini değiştirme  
        properties.setProperty("renk1", "siyah");  
        properties.setProperty("renk2", "gri");  
        properties.setProperty("renk3", "mavi");  
        System.out.println(properties.getProperty("renk1"));  
        System.out.println(properties.getProperty("renk2"));  
        System.out.println(properties.getProperty("renk3"));  
      }catch(Exception e){  
        e.printStackTrace();  
      }  
    }  
  }

Sonuç:

  run:  
  sarı  
  kırmızı  
  yesil  
  siyah  
  gri  
  mavi  
  BUILD SUCCESSFUL (total time: 0 seconds)

1.Yol

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  import javax.xml.parsers.DocumentBuilderFactory;  
  import javax.xml.parsers.DocumentBuilder;  
  import org.w3c.dom.Document;  
  import org.w3c.dom.NodeList;  
  import org.w3c.dom.Node;  
  import org.w3c.dom.Element;  
  import java.io.StringReader;  
  import org.xml.sax.InputSource;  
  public class ReadXMLFile {  
    public static void main(String argv[]) {  
      try {  
        //File xmlFile = new File("/Users/hasan/belge.xml");  //dilerseniz xml dosyasındanda okuma işlemi gerçekleştirebilirsiniz.  
        String xml = "\n"  
            + "\n"  
            + " \n"  
            + "  200\n"  
            + "  İşlem başarılı\n"  
            + " \n"  
            + "";  
        InputSource source = new InputSource(new StringReader(xml));  
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
        Document doc = dBuilder.parse(source);  
        doc.getDocumentElement().normalize();  
        NodeList liste = doc.getElementsByTagName("status");  
        for (int temp = 0; temp < liste.getLength(); temp++) {  
          Node node = liste.item(temp);  
          System.out.println("Root element :" + doc.getDocumentElement().getNodeName());  
          System.out.println("Element :" + node.getNodeName());  
          if (node.getNodeType() == Node.ELEMENT_NODE) {  
            Element eElement = (Element) node;  
            System.out.println("message : " + eElement.getElementsByTagName("message").item(0).getTextContent());  
            System.out.println("code : " + eElement.getElementsByTagName("code").item(0).getTextContent());  
          }  
        }  
      } catch (Exception e) {  
        e.printStackTrace();  
      }  
    }  
  }

Sonuç

  run:  
  Root element :response  
  Element :status  
  message : İşlem başarılı  
  code : 200  
  BUILD SUCCESSFUL (total time: 0 seconds)  

2.Yol

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.StringReader;  
  import javax.xml.parsers.DocumentBuilder;  
  import javax.xml.parsers.DocumentBuilderFactory;  
  import javax.xml.xpath.XPath;  
  import javax.xml.xpath.XPathFactory;  
  import org.w3c.dom.Document;  
  import org.xml.sax.InputSource;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class ReadXmlFile2 {  
    public static void main(String argv[]) {  
      String message;  
      String code;  
      try {  
        //File xmlFile = new File("/Users/hasan/belge.xml");  //dilerseniz xml dosyasındanda okuma işlemi gerçekleştirebilirsiniz.  
        String xml = "\n"  
            + "\n"  
            + " \n"  
            + "  200\n"  
            + "  İşlem başarılı\n"  
            + " \n"  
            + "";  
        InputSource source = new InputSource(new StringReader(xml));  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        DocumentBuilder db = dbf.newDocumentBuilder();  
        Document document = db.parse(source);  
        XPathFactory xpathFactory = XPathFactory.newInstance();  
        XPath xpath = xpathFactory.newXPath();  
        message = xpath.evaluate("/response/status/message", document);  
        code = xpath.evaluate("/response/status/code", document);  
        System.out.println("message = " + message);  
        System.out.println("code = " + code);  
      } catch (Exception e) {  
        System.out.println(e.getMessage());  
      }  
    }  
  } 

Sonuç

  run:  
  message = İşlem başarılı  
  code = 200  
  BUILD SUCCESSFUL (total time: 0 seconds) 

Standard HttpURLConnection ve Apache HttpClient kütüphanesi kullanarak Http GET isteği nasıl yapılır onun hakkında bilgi vermek istiyorum.

1-) Standard HttpURLConnection

HttpGetRequest.java 

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.BufferedReader;  
  import java.io.InputStreamReader;  
  import java.net.HttpURLConnection;  
  import java.net.URL;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class HttpGetRequest {  
    static int responseCode;  
    static String responseMessage;
  
    public static void main(String[] args) throws Exception {
  
      URL url = new URL("http://www.blog.berkadem.com/");  
      HttpURLConnection con = (HttpURLConnection) url.openConnection();  
      con.setRequestMethod("GET");  
      responseCode=con.getResponseCode();  
      responseMessage=con.getResponseMessage();  
      BufferedReader in = new BufferedReader(new InputStreamReader(  
                    con.getInputStream()));  
      String inputLine;  
      StringBuffer sonuc = new StringBuffer(); 
 
      while ((inputLine = in.readLine()) != null)   
        //System.out.println(inputLine); //dilerseniz response sonucunu bu şekilde de alabilirsiniz  
        sonuc.append(inputLine);  
      in.close();  
      //System.out.println("Sonuc = "+sonuc); //dilerseniz response sonucunu bu şekilde de alabilirsiniz. Burada dönen response değeri xml formatında da olabilirdi.  
      System.out.println("responseCode = "+responseCode);  
      System.out.println("responseMessage = "+responseMessage);  
    }  
  } 
Sonuç 

run:  
  responseCode = 200  
  responseMessage = OK  
  BUILD SUCCESSFUL (total time: 1 second)


1-) Apache HttpClient 

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.BufferedReader;  
  import java.io.InputStreamReader;  
  import org.apache.http.HttpResponse;  
  import org.apache.http.client.HttpClient;  
  import org.apache.http.client.methods.HttpGet;  
  import org.apache.http.impl.client.DefaultHttpClient;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class HttpGetRequest2 {  
    static int responseCode;  
    static String responseMessage;
  
    public static void main(String[] args) throws Exception {  

      String url = "http://www.blog.berkadem.com/";  
      HttpClient client = new DefaultHttpClient();  
      HttpGet request = new HttpGet(url);  
      HttpResponse response = client.execute(request);  
      responseCode=response.getStatusLine().getStatusCode();  
      responseMessage=response.getStatusLine().getReasonPhrase();  
      BufferedReader in = new BufferedReader(  
          new InputStreamReader(response.getEntity().getContent()));  
      String inputLine;  
      StringBuffer sonuc = new StringBuffer();
 
      while ((inputLine = in.readLine()) != null) //System.out.println(inputLine); //dilerseniz response sonucunu bu şekilde de alabilirsiniz  
      {  
        sonuc.append(inputLine);  
      }  

      in.close();  
      //System.out.println("Sonuc = "+sonuc); //dilerseniz response sonucunu bu şekilde de alabilirsiniz. Burada dönen response değeri xml formatında da olabilirdi.  
      System.out.println("responseCode = " + responseCode);  
      System.out.println("responseMessage = " + responseMessage);  
    }  
  } 
Sonuç 

  run:  
  responseCode = 200  
  responseMessage = OK  
  BUILD SUCCESSFUL (total time: 0 seconds)  


6 Kasım 2013 Çarşamba



  public void update() {  
      jLabel1.setText(jTextArea3.getText().length()+"/ sayi");  
    }  




  private void jTextArea3KeyTyped(java.awt.event.KeyEvent evt) {                    
      // Listen for changes in the text  
      // text alanındaki değişikliği dinle  
      jTextArea3.getDocument().addDocumentListener(new DocumentListener() {  
        public void changedUpdate(DocumentEvent evt) {  
          update();  
        }  
        public void removeUpdate(DocumentEvent evt) {  
          update();  
        }  
        public void insertUpdate(DocumentEvent evt) {  
          update();  
        }  
      });  
    }  

4 Kasım 2013 Pazartesi



 String userHomeFolder = System.getProperty("user.home") + "/Desktop"; 
 File textFile = new File(userHomeFolder, "mytext.xls");  

2 Kasım 2013 Cumartesi





  int selectedRow =0;  
....
  private void jXTable2MousePressed(java.awt.event.MouseEvent evt) {                     
      try {  
        //Tabloda üzerine tıklanılan satırın bilgilerini ilgili form elemanlarına yazar.  
        selectedRow = jXTable2.getSelectedRow();  
      } catch (Exception e) {  
        //  
      }    
...
  private void jXHyperlink4ActionPerformed(java.awt.event.ActionEvent evt) {                         
      DefaultTableModel model = (DefaultTableModel) jXTable2.getModel();  
      model.removeRow(selectedRow);  
    }  


1:  JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));  


....

  DefaultTableModel model = (DefaultTableModel) table.getModel();
  model.addRow(new Object[]{"Id", "Name"});