File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes textfield value for JSlider Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "textfield value for JSlider" Watch "textfield value for JSlider" New topic
Author

textfield value for JSlider

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I am trying to get my JSlider to accept a value enter in a textfield and move the slider accordingly. I got some help from the API but it's not working. Any advice would be helpful.
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi-
I have a couple of observations.
1.) Since you are adding this as a PropertyChangeListener, I assume that you have implemented PropertyChangeListener for the class.
2.)In your propertyChangedEvent method you are testing the equality of a JTextField and a String (PropertyChangeEvent.getPropertyName() returns a String). These two will never equal.
3.)You would be better suited using a DocumentListener to listen for changes to the JTextField to update the JSlider. You do this using JTextField.getDocument().addDocumentListener(listener).
Kyle
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I haven't used that statement before and what I have tried doesn't seem to work as I get numerous compile errors. Here's how I have it now:

Does JTextField.getDocument().addDocumentListener(listener);
give me the value of textfield?
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi-
getDocument().addDocumentListener(DocumentListener) return void so you cannot caset it to a Number object. What you need to do is instead of implementing PropertyChangeListener and using the propertyChange(PropertyChangeEvent), you chould implement DocumentListener and use its three methods:
1. changedUpdate(Document Event)//just use a null implementation of this
2. insertUpdate(DocumentEvent)
3. removeUpdate(DocumentEvent)
Each time the value in the JTextField is changed then the insertUpdate and/or removeUpdate method will be called.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
How would I then activate this code? I would think that I need textField.<something> but I have tried everything I can find and nothing has worked so far.
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
You use

This will then force the JTextField to "notify" your Class each time the value in the JTextField is changed.
Kyle
[ October 23, 2003: Message edited by: kyle amburn ]
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Here is the compile error that I get on that statement:
addDocumentListener(javax.swing.event.DocumentListener) in javax.swing.text.Document cannot be applied to (BlueSliderBeanFrame)
textField.getDocument().addDocumentListener(this);
^
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi-
Did you implement DocumentListener for the
BlueSliderBeanFrame? The error you got is saying that the object you added (this) is not an instance of a DocumentListener. Your class declaration needs to say the following:


Kyle
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Thank you so much!! My bean is working!!
 
GeeCON Prague 2014
 
subject: textfield value for JSlider