Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

textfield value for JSlider

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!! My bean is working!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic