Results 1 to 4 of 4
Thread: How to improve Jslider
- 01-10-2010, 11:26 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
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); } } } } }
- 01-11-2010, 12:49 AM #2
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.
- 01-11-2010, 03:26 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
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); } } } } } }
- 01-12-2010, 01:04 AM #4
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.My only other question is where should I close the socket or do I need to?
Similar Threads
-
Jslider changeListner
By Basit56 in forum Java AppletsReplies: 1Last Post: 09-14-2009, 04:04 PM -
How can we zoom a map using JSlider
By barney in forum AWT / SwingReplies: 5Last Post: 02-23-2009, 01:48 PM -
Got Trouble with JSlider
By hungleon88 in forum Advanced JavaReplies: 6Last Post: 08-30-2008, 05:02 PM -
jmf & JSlider
By nathanr_kamal in forum AWT / SwingReplies: 6Last Post: 06-28-2008, 07:56 AM -
Scale 2 or more pictures using a JSlider
By Panchitopro in forum New To JavaReplies: 0Last Post: 05-05-2008, 05:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks