Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/09/2015, 14:03
American2010
 
Fecha de Ingreso: abril-2003
Mensajes: 1.129
Antigüedad: 21 años, 1 mes
Puntos: 34
Pregunta Actualizar un valor en un JPanel

Hola gente del foro, les quería hacer una consulta, aclarando que no soy muy bueno con Java, pero quiero armar una pequeña aplicación que, leyendo un JSon de internet, me devuelve el número de participantes en un chat de Twitch.

El problema que tengo, es que no se como hacer que ese valor se "refresque" pasado X tiempo.

Mi código están en 2 partes, uno que arma la ventana y el otro que lee la URL y me devuelve un INT con el número de personas en el chat (Ese valor necesito actualizar mientras el programa esta corriendo.

TwitchMonitor.java
Código Java:
Ver original
  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.Font;
  4. import java.io.IOException;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.SwingConstants;
  10. import javax.swing.border.EmptyBorder;
  11.  
  12. import org.json.JSONException;
  13.  
  14. public class TwitchMonitor extends JFrame
  15. {
  16.  
  17.     /**
  18.      *
  19.      */
  20.     private static final long serialVersionUID = 1L;
  21.    
  22.     private String channelName = "pokerstaples";
  23.    
  24.     private JPanel thePanel;
  25.    
  26.     private JLabel labelChatters;
  27.  
  28.     private JLabel labelChattersCount;
  29.     private JLabel lblNewLabel;
  30.    
  31.  
  32.    
  33.  
  34.     /**
  35.      * Launch the application.
  36.      */
  37.     public static void main(String[] args) {
  38.         EventQueue.invokeLater(new Runnable() {
  39.             public void run() {
  40.                 try {
  41.                     TwitchMonitor frame = new TwitchMonitor();
  42.                     frame.setVisible(true);
  43.                 } catch (Exception e) {
  44.                     e.printStackTrace();
  45.                 }
  46.             }
  47.         });
  48.     }
  49.    
  50.     /**
  51.      * Create the frame.
  52.      * @throws JSONException
  53.      * @throws IOException
  54.      */
  55.     public TwitchMonitor() throws IOException, JSONException {
  56.         setResizable(false);
  57.         setBackground(Color.BLACK);
  58.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.         setTitle("Chatters Monitor for Twitch Channels by MrAmericanMike");
  60.        
  61.         try{
  62. //      URL urlIcon = TwitchMonitor.class.getResource("/resources/icon.png");
  63. //      ImageIcon img = new ImageIcon(urlIcon);
  64. //      this.setIconImage(img.getImage());
  65.         }
  66.         catch (Exception e) {
  67.             e.printStackTrace();
  68.         }
  69.  
  70.        
  71.         setSize(200, 130);
  72.         setLocationRelativeTo(null);
  73.         thePanel = new JPanel();
  74.         thePanel.setBackground(Color.BLACK);
  75.         thePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  76.         setContentPane(thePanel);
  77.        
  78.         labelChatters = new JLabel(channelName);
  79.         labelChatters.setForeground(Color.WHITE);
  80.         labelChatters.setBounds(5, 5, 185, 30);
  81.         labelChatters.setHorizontalAlignment(SwingConstants.CENTER);
  82.         labelChatters.setFont(new Font("Tahoma", Font.PLAIN, 20));
  83.        
  84.         int chattersCount = JsonReader.count(channelName.toLowerCase());
  85.        
  86.         labelChattersCount = new JLabel(""+chattersCount);
  87.         labelChattersCount.setForeground(Color.WHITE);
  88.         labelChattersCount.setBounds(5, 0, 185, 130);
  89.         labelChattersCount.setHorizontalAlignment(SwingConstants.CENTER);
  90.         labelChattersCount.setFont(new Font("Tahoma", Font.PLAIN, 60));
  91.        
  92.         lblNewLabel = new JLabel("");
  93.        
  94.         try{
  95. //      URL url = TwitchMonitor.class.getResource("/resources/background.png");
  96. //      lblNewLabel.setIcon(new ImageIcon(url));
  97.         }
  98.         catch (Exception e) {
  99.             e.printStackTrace();
  100.         }
  101.        
  102.         lblNewLabel.setBounds(0, 0, 200, 130);
  103.         thePanel.setLayout(null);
  104.         thePanel.add(labelChatters);
  105.         thePanel.add(labelChattersCount);
  106.         thePanel.add(lblNewLabel);
  107.     }
  108.  
  109.  
  110.  
  111. }//END CLASS


JsonReader.java
Código Javascript:
Ver original
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.Reader;
  6. import java.net.URL;
  7. import java.nio.charset.Charset;
  8.  
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. public class JsonReader {
  13.  
  14.       private static String readAll(Reader rd) throws IOException {
  15.         StringBuilder sb = new StringBuilder();
  16.         int cp;
  17.         while ((cp = rd.read()) != -1) {
  18.           sb.append((char) cp);
  19.         }
  20.         return sb.toString();
  21.       }
  22.  
  23.       public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
  24.         InputStream is = new URL(url).openStream();
  25.         try {
  26.           BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  27.           String jsonText = readAll(rd);
  28.           JSONObject json = new JSONObject(jsonText);
  29.           return json;
  30.         } finally {
  31.           is.close();
  32.         }
  33.       }
  34.  
  35.       public static void main(String[] args) throws IOException, JSONException {
  36.       }
  37.      
  38.       public static int count(String channelName) throws IOException, JSONException
  39.       {
  40.           JSONObject json = readJsonFromUrl("http://tmi.twitch.tv/group/user/"+channelName+"/chatters");
  41.           int chattersCount = (Integer)json.get("chatter_count");
  42.           return chattersCount;
  43.       }
  44. }

Agradezco cualquier ayuda que me puedan dar. ;)
__________________
elGastronomo