Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.awt.Cursor; import java.io.StringReader; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonValue; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.GzipDecompressingEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.awt.Color; import java.net.URL; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; import javax.swing.text.html.HTMLEditorKit; /** * * @author Administrator */ public class DesignGUI extends javax.swing.JFrame { /** * Creates new form DesignGUI */ public DesignGUI() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); panel_stackEx = new javax.swing.JPanel(); jPanel_header = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); lb_qusetion = new javax.swing.JLabel(); tb_question = new javax.swing.JTextField(); lb_site = new javax.swing.JLabel(); cb_Site = new javax.swing.JComboBox<>(); bt_search = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jEditorPane_areaShow = new javax.swing.JEditorPane(); jTable1.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(jTable1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/stacklogo.jpg"))); // NOI18N jLabel2.setText(" "); lb_qusetion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N lb_qusetion.setText("question :"); tb_question.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tb_questionActionPerformed(evt); } }); lb_site.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N lb_site.setText("Site:"); cb_Site.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Stack Overflow", "Web Applications", "Game Development", "Programmers", "Web Master" })); cb_Site.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cb_SiteActionPerformed(evt); } }); bt_search.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N bt_search.setText("Search"); bt_search.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bt_searchActionPerformed(evt); } }); javax.swing.GroupLayout jPanel_headerLayout = new javax.swing.GroupLayout(jPanel_header); jPanel_header.setLayout(jPanel_headerLayout); jPanel_headerLayout.setHorizontalGroup(jPanel_headerLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_headerLayout.createSequentialGroup() .addGroup(jPanel_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_headerLayout.createSequentialGroup().addGap(18, 18, 18) .addComponent(lb_qusetion) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tb_question, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(lb_site) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(cb_Site, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(bt_search)) .addGroup(jPanel_headerLayout.createSequentialGroup().addGap(28, 28, 28) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 642, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel_headerLayout .setVerticalGroup( jPanel_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel_headerLayout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel_headerLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lb_qusetion) .addComponent(tb_question, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lb_site) .addComponent(cb_Site, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(bt_search)) .addContainerGap(51, Short.MAX_VALUE))); javax.swing.GroupLayout panel_stackExLayout = new javax.swing.GroupLayout(panel_stackEx); panel_stackEx.setLayout(panel_stackExLayout); panel_stackExLayout.setHorizontalGroup(panel_stackExLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel_stackExLayout.createSequentialGroup().addContainerGap().addComponent(jPanel_header, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); panel_stackExLayout .setVerticalGroup(panel_stackExLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel_header, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); jEditorPane_areaShow.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jScrollPane2.setViewportView(jEditorPane_areaShow); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panel_stackEx, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 736, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panel_stackEx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void tb_questionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tb_questionActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tb_questionActionPerformed private void cb_SiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_SiteActionPerformed }//GEN-LAST:event_cb_SiteActionPerformed private void bt_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_searchActionPerformed // StackExchangeAPI stack = new StackExchangeAPI(); String search = this.lb_qusetion.getText(); search = search.replaceAll(" ", "%20"); this.cb_Site.getItemListeners(); String url = "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&accepted=True&title=" + search + "&site=stackoverflow&filter=withbody"; DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(url); HttpResponse httpResponse; try { httpResponse = httpclient.execute(getRequest); HttpEntity entity = httpResponse.getEntity(); System.out.println("----------------------------------------"); System.out.println(httpResponse.getStatusLine()); Header[] headers = httpResponse.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { entity = new GzipDecompressingEntity(entity); String jsonStr = EntityUtils.toString(entity); // System.out.println(jsonStr); parseStackExchange(jsonStr); } else { System.out.println("NOTHING"); } } catch (Exception ex) { ex.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); } }//GEN-LAST:event_bt_searchActionPerformed private void parseStackExchange(String jsonStr) { JsonReader reader = null; StringBuilder content = new StringBuilder(); this.jEditorPane_areaShow.setContentType("text/html"); content.append("<html></body>"); try { reader = Json.createReader(new StringReader(jsonStr)); JsonObject jsonObject = reader.readObject(); reader.close(); JsonArray array = jsonObject.getJsonArray("items"); for (JsonObject result : array.getValuesAs(JsonObject.class)) { JsonObject ownerObject = result.getJsonObject("owner"); // int ownerReputation = ownerObject.getInt("reputation"); // System.out.println("Reputation:"+ownerReputation); int viewCount = result.getInt("view_count"); content.append("<br>View Count :" + viewCount + "<br>"); int answerCount = result.getInt("answer_count"); content.append("Answer Count :" + answerCount + "<br>"); String title = result.getString("title"); content.append("Title: <FONT COLOR=green>" + title + "</FONT>.<br>"); String link = result.getString("link"); content.append("URL :<a href="); content.append("'link'>" + link); content.append("</a>.<br>"); // String body = result.getString("body"); // content.append("Body:"+body); /* JsonArray tagsArray = result.getJsonArray("tags"); StringBuilder tagBuilder = new StringBuilder(); int i = 1; for(JsonValue tag : tagsArray){ tagBuilder.append(tag.toString()); if(i < tagsArray.size()) tagBuilder.append(","); i++; } content.append("Tags: "+tagBuilder.toString());*/ } content.append("</body></html>"); this.jEditorPane_areaShow.setText(content.toString()); System.out.println(content.toString()); } catch (Exception e) { e.printStackTrace(); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(DesignGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DesignGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DesignGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DesignGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DesignGUI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bt_search; private javax.swing.JComboBox<String> cb_Site; private javax.swing.JEditorPane jEditorPane_areaShow; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel_header; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JLabel lb_qusetion; private javax.swing.JLabel lb_site; private javax.swing.JPanel panel_stackEx; private javax.swing.JTextField tb_question; // End of variables declaration//GEN-END:variables }