Results 1 to 7 of 7
  1. #1
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default japplet - refreshing windows

    Hi

    I have a small japplet that gets data from a webserver
    the data is a voltage reading that changes when I turn a button.

    The applet shows the data, but only once.. it does not update.

    I am guessing I need some sort of refresh, update, repaint but I don't know what code to use.

    so far my paint method is VERY simple..

    Java Code:
    public void paint(Graphics g)
        {
            // simple text displayed on applet
    
            g.setColor(Color.black);
            g.drawString(output, 20, 20);
    
    
        }
    How can I update the applet over and over and over again so it just runs showing the data from the webserver "live" ?

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You could use a Swing Timer to get the information from the websever every xxxx mseconds, change the output text and then call repaint.

    As an aside, it appears that you're drawing directly on the JApplet via its paint method and you never want to do that. If this were my app, I'd display the output in a JLabel and simply change the JLabel's text via setText(...), but if you absolutely need to paint in the applet, then paint in a JPanel's paintComponent method and display this JPanel in the applet. Else you loose all advantages of Swing graphics.

  3. #3
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Hi
    Thanks for your answer... but ack.. I am too new to use your answer properly... :(

    until I get an understanding of what you're saying... could I continue doing what I'm doing?

    I read about repaint()? can this be used somehow?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Symbiot View Post
    Hi
    Thanks for your answer... but ack.. I am too new to use your answer properly... :(
    The solution is to study, study, study, and you'll get more familiar with Java and Swing.


    until I get an understanding of what you're saying... could I continue doing what I'm doing?
    You can answer that: Is what you're currently doing working?


    I read about repaint()? can this be used somehow?
    Without updating the data from the source, you can repaint til you're blue in the face and it will have little benefit. The key is to get the data intermittently from the source, and one way to do this is to use a Swing Timer to poll the source intermittently. Another is to somehow notify your app when the source data changes, but I don't know how to do this (or if it's possible). As for using a Swing Timer, your best bet is to go to the Sun Swing tutorials and read the tutorial that covers Swing Timers and then study the source code that comes with the tutorial. They're not that hard to use if you already know how to use ActionListeners with JButtons.

    Que tenga suerte!

  5. #5
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    hi

    Data from the source is streaming already... I have run the code as a normal java application, and it updates fine, so getting the data is not the problem (I also checked via wireshark to verify that the data is continuous).

    of course.. I may be assuming that the stream is enough?

    here's the complete raw code:

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.math.*;
    import java.text.DecimalFormat;
    import java.awt.*;
    import javax.swing.*;
    import java.lang.Exception;
    import java.io.IOException;
    import java.net.UnknownHostException;
    import java.lang.String;
    
    
    public class webnet_001 extends javax.swing.JApplet {
    
           String sentence;
           String line;
           String output;
    
    
        /** Initializes the applet webnet_001 */
        public void init() {
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        initComponents();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
          
            try
            {
            Socket clientSocket = new Socket("192.168.2.38",876);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            outToServer.writeBytes("<b");
            sentence = inFromServer.readLine();
        }
            catch (IOException ie)
            {
                System.out.println(ie.getMessage());
            }
     
           
    
        }
    
        public void start()
        {
    StringBuffer b = new StringBuffer(sentence);
                b.deleteCharAt(0);
                b.deleteCharAt(0);
                String s = b.toString();
    
                int dec = Integer.parseInt(s,16);
               // double faktu = 150 / 1023;
                double res = 0.146628 * dec;
    
                DecimalFormat formattingObject = new DecimalFormat("0.00");
                //String output = formattingObject.format(res);
                output = formattingObject.format(res);
        }
    
     public void paint(Graphics g)
        {
            // simple text displayed on applet
    
            g.setColor(Color.black);
            g.drawString(output, 20, 20);
            
    
        }
    
        /** This method is called from within the init() method 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">                          
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>                        
    
    
        // Variables declaration - do not modify                     
        // End of variables declaration               
    
    }
    as you can see, it's not advanced stuff...

  6. #6
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    oh and..study study study.. indeed... I can only agree.. but there is sooooooo much to study ! But I do try..!

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I'm no pro at this, but my guess is that you need a while loop to continually accept input from the client socket's input stream. Then inside of the while loop you get the String, update class fields, and call repaint() on the JApplet. Again, I'd display the information in a JLabel not in a paint method. You also may need to do the while loop in a background thread so as not to freeze Swing's main event thread, the EDT -- more complicated stuff to look up. Google "Concurrency in Swing" (first hit) for more.

Similar Threads

  1. Refreshing JList - need help
    By Unsub in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 06:31 AM
  2. JTableHeader not refreshing
    By aznboarder in forum AWT / Swing
    Replies: 4
    Last Post: 04-11-2009, 04:31 AM
  3. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM
  4. Why my JTree always collasped after refreshing?
    By lmsook10 in forum AWT / Swing
    Replies: 2
    Last Post: 06-24-2008, 05:55 AM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •