Results 1 to 4 of 4
  1. #1
    JohnST is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default How to improve Jslider

    I'm using the following to control a dimmer. The problem is very slow to react. I would like some suggestions of how to improve this code. I would like to open a socket and keep it open and send str value from slider to output stream. I guess what I would like to do is bind the value from slider to output stream, but only open socket once.
    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    
    public class CreateSlider{
      Socket c4Socket = null;
      JSlider slider;
      JLabel label;
      public static void main(String[] args){
        CreateSlider cs = new CreateSlider();
      }
    
      public CreateSlider(){
        JFrame frame = new JFrame("Light Dimmer");
        slider = new JSlider();
        slider.setValue(0);
        slider.addChangeListener(new MyChangeAction());
        label = new JLabel("0");
        JPanel panel = new JPanel();
        panel.add(slider);
        panel.add(label);
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(400, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public class MyChangeAction implements ChangeListener{
        public void stateChanged(ChangeEvent ce){
          int value = slider.getValue();
          String str = Integer.toString(value);
          label.setText(str);
        
          DataOutputStream os = null;
            
    
    // Try to open output stream
            try {
                c4Socket = new Socket("192.168.1.107", 5020);
                os = new DataOutputStream(c4Socket.getOutputStream());
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to controller");
            }
    // If everything has been initialized then we want to write some data
    // to the socket we have opened a connection to on port 5020
        if (c4Socket != null && os != null) {
                try {
    // message
    
                
                    os.writeBytes("<c4soap name=\"SendToDeviceAsync\" session=\"0\" operation=\"RWX\" category=\"composer\" async=\"True\"><param name=\"iddevice\" type=\"number\">218</param><param name=\"data\" type=\"string\"><devicecommand owneridtype=\"\" owneriditem=\"-1\"><command>SET_LEVEL</command><params><param><name>LEVEL</name><value type=\"INTEGER\"><static>");
                    os.writeBytes(str);
                    os.writeBytes("</static></value></param></params></devicecommand></param></c4soap>\0");
                    
                    
           
                    
    
    // close the socket
            os.close();
                 
                    c4Socket.close();   
                } catch (UnknownHostException e) {
                    System.err.println("Trying to connect to unknown host: " + e);
                } catch (IOException e) {
                    System.err.println("IOException:  " + e);
                }
              }
           }
        }         
     }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I think the problem lies in that you are trying to create an entirely new output stream every time there is a change -- which can happen several times per second.

    I think it would be better to create the server connection when the app is launched. Then on a change event, simply send the updated information.

    If you really want to make it efficient, only send the update once the user has let go of the mouse button -- or add an 'Update' button that submits the changes only on click.

  3. #3
    JohnST is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default

    Thanks for the suggestions it's working much better now. My only other question is where should I close the socket or do I need to?

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    
    public class CreateSlider{
      private static Socket c4Socket = null;
      private static DataOutputStream os = null;
      JSlider slider;
      JLabel label;
      
      public static void main(String[] args){
        try {
      c4Socket = new Socket("192.168.1.107", 5020); 
      os = new DataOutputStream(c4Socket.getOutputStream());
       } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to controller");
            }
        CreateSlider cs = new CreateSlider();
      
         }
      public CreateSlider(){
        
        JFrame frame = new JFrame("Light Dimmer " );
        slider = new JSlider(JSlider.VERTICAL);
        slider.setValue(0);
        slider.addChangeListener(new MyChangeAction());
        label = new JLabel("0");
        JPanel panel = new JPanel();
        panel.add(slider);
        panel.add(label);
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public class MyChangeAction implements ChangeListener{
        public void stateChanged(ChangeEvent ce){
          int value = slider.getValue();
          String str = Integer.toString(value);
          label.setText(str);
          if (!slider.getValueIsAdjusting()){      
          if (c4Socket != null && os != null) {
                try {
    
                    os.writeBytes("<c4soap name=\"SendToDeviceAsync\" session=\"0\" operation=\"RWX\" category=\"composer\" async=\"True\"><param name=\"iddevice\" type=\"number\">218</param><param name=\"data\" type=\"string\"><devicecommand owneridtype=\"\" owneriditem=\"-1\"><command>SET_LEVEL</command><params><param><name>LEVEL</name><value type=\"INTEGER\"><static>");
                    os.writeBytes(str);
                    os.writeBytes("</static></value></param></params></devicecommand></param></c4soap>\0");
                    
                } catch (UnknownHostException e) {
                    System.err.println("Trying to connect to unknown host: " + e);
                } catch (IOException e) {
                    System.err.println("IOException:  " + e);
                }
            }
          }
        }
      }
    }

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    My only other question is where should I close the socket or do I need to?
    Well, you technically don't have to close it, eventually the other end will time out. However, if you have a 'server' listening for a 'client[s]' then it'd be a nice idea to have each client close the socket when closing the client. You could do it when the window closes for example - I believe you can override the default window close behavior and add in a close socket statement before the application terminates -- that way when a user clicks the close button, it disconnects the quits.

Similar Threads

  1. Jslider changeListner
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 09-14-2009, 04:04 PM
  2. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 01:48 PM
  3. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 05:02 PM
  4. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 07:56 AM
  5. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum New To Java
    Replies: 0
    Last Post: 05-05-2008, 05:22 PM

Posting Permissions

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