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. */ package tk.egsf.ddns; import java.util.ArrayList; import java.util.List; import javax.swing.table.DefaultTableModel; import org.json.JSONArray; import org.json.JSONObject; /** * * @author fosa */ public class janela extends javax.swing.JFrame { /** * Creates new form janela */ public janela() { initComponents(); inicializar(); } public janela(ddnsclient.DDNSclient ddns) { this.ddns = ddns; initComponents(); inicializar(); } /** * 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() { jLabel1 = new javax.swing.JLabel(); JLusername = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); JLlogin = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); JBatualizalista = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Usurio:"); JLusername.setText("username"); jLabel3.setText("Login:"); JLlogin.setText("login"); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Nome Host", "ip", "" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.Boolean.class }; boolean[] canEdit = new boolean[] { false, false, true }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(jTable1); if (jTable1.getColumnModel().getColumnCount() > 0) { jTable1.getColumnModel().getColumn(2).setResizable(false); jTable1.getColumnModel().getColumn(2).setPreferredWidth(5); } JBatualizalista.setText("Atualizar Lista"); JBatualizalista.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { JBatualizalistaActionPerformed(evt); } }); jLabel2.setText("0.0.0.0"); jLabel2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jLabel2MouseClicked(evt); } }); jButton1.setText("Atualizar Hosts"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextField1.setText("0.0.0.0"); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(JLusername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(JLlogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addComponent(JBatualizalista) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1).addGap(18, 18, 18).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(JLusername).addComponent(jLabel3) .addComponent(JLlogin)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(JBatualizalista).addComponent(jLabel2).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addGroup(layout.createSequentialGroup().addGap(4, 4, 4).addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void JBatualizalistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBatualizalistaActionPerformed String https_url = "https://ddns.egsf.tk/webservice/gethosts"; String gethostsjson = JSON_helper.getJsonUrl(https_url, ddns.getBasicAuth()); if (gethostsjson == "") { return; } JSONArray jsa = new JSONArray(gethostsjson); DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); List<Boolean> selected = new ArrayList<Boolean>(); int rowcount = jTable1.getRowCount(); for (int i = 0; i < rowcount; i++) { selected.add((Boolean) jTable1.getValueAt(i, 2)); } model.setNumRows(0); try { for (int i = 0; i < jsa.length(); i++) { System.out.println(jsa.get(i)); JSONObject js = jsa.getJSONObject(i); Object[] row = new Object[] { js.getString("host") + "." + js.getString("zone"), js.getString("ip") }; model.addRow(row); } model.fireTableDataChanged(); for (int i = 0; i < selected.size(); i++) { Boolean boolean1 = selected.get(i); jTable1.setValueAt(boolean1, i, 2); } } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } }//GEN-LAST:event_JBatualizalistaActionPerformed private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked String getipjson = JSON_helper.getJsonUrl("https://ddns.egsf.tk/webservice/getip", ddns.getBasicAuth()); JSONObject js = new JSONObject(getipjson); String ip = js.getString("ip"); jLabel2.setText(ip); jTextField1.setText(ip); }//GEN-LAST:event_jLabel2MouseClicked private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed int numlinhas = jTable1.getModel().getRowCount(); JSONObject jo = new JSONObject(); // jo.put("ip", jLabel2.getText()); jo.put("ip", jTextField1.getText()); DefaultTableModel modtable = (DefaultTableModel) jTable1.getModel(); List<String> lista = new ArrayList<String>(); for (int i = 0; i < numlinhas; i++) { if (jTable1.getValueAt(i, 2) != null) { boolean selected = (boolean) jTable1.getValueAt(i, 2); if (selected) { lista.add((String) modtable.getValueAt(i, 0)); } } } if (lista.size() > 0) { jo.put("hosts", lista); try { String resposta = JSON_helper.postToUrl("https://ddns.egsf.tk/webservice/dohostupdate", ddns.getBasicAuth(), jo); JSONObject jres = new JSONObject(resposta); if (jres.getInt("updated") == 1) { atualizarTabela(); jLabel4.setText(jres.getString("msg")); } else { jLabel4.setText(jres.getString("msg")); } } catch (Exception e) { System.out.println("error"); } } }//GEN-LAST:event_jButton1ActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed /** * @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(janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(janela.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 janela().setVisible(true); } }); } public void atualizarTabela() { JBatualizalistaActionPerformed(null); } public void atualizarIp() { jLabel2MouseClicked(null); } public void setip(String ip) { if (!jLabel2.getText().equals(ip)) { jLabel2.setText(ip); jTextField1.setText(ip); jButton1ActionPerformed(null); } } public void atualizarHosts() { } private int loginId; ddnsclient.DDNSclient ddns; Thread tarefa; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton JBatualizalista; private javax.swing.JLabel JLlogin; private javax.swing.JLabel JLusername; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables public void setUserName(String UserName) { JLusername.setText(UserName); } public void setLogin(String Login) { JLlogin.setText(Login); } public void setLoginId(int loginId) { loginId = loginId; } private void inicializar() { Looper looper = new Looper(this); tarefa = new Thread(looper); } }