Results 1 to 8 of 8
Thread: adjusting bar in java (GUI)
- 06-27-2010, 05:45 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
adjusting bar in java (GUI)
hei, good day everyone.
I want to draw an GUI that can let the user input the value by adjusting the bar. The bar is similiar to normal volume bar (adjusting the value by draging the pointer to the specific scale.), anyone know the name of the GUI component ? Somemore what is the name of the Listener that should be used to register with the component?
Thanks in advance.
- 06-27-2010, 06:28 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Sounds like you want a JSlider. Read the section from the Swing tutorial on How to Use Sliders for an example.
- 06-27-2010, 12:54 PM #3
Different components have different listeners. Read the API doc for the component to see which one to use.what is the name of the Listener that should be used to register with the component
- 06-27-2010, 03:14 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
To camickr: thanks, JSlider is the component that i want.
Now, i know how to use JSlider already. But one things make me so curious:
Java Code:slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); // get the slider if (!source.getValueIsAdjusting()) { val = source.getValue(); // get slider value } } });
The code above, there is one statement like:
Java Code:JSlider source = (JSlider)e.getSource(); // get the slider
Why we need to cast it to JSlider? ( I have compiled it, without it i get error)
But when i do the checking i get no error, like
Java Code:if( e.getSource == slider)
hmmmm, so strange...Last edited by it2512; 06-27-2010 at 03:17 PM.
- 06-27-2010, 03:17 PM #5
You don't have to cast it for: e.getSource == sliderWhy we need to cast it to JSlider
because that statement is comparing if two object references are the same.
You do have to cast it for:Because source needs to be a reference to a class that has a getValue() method.JSlider source = (JSlider)e.getSource(); // get the slider
if (!source.getValueIsAdjusting()) {
val = source.getValue(); // get slider value
- 06-28-2010, 03:04 AM #6
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Sorry previous post i forgot declare:
So, slider should be JSlider type. Since we can compare them e.getSource == slider, this should mean they are same type?Java Code:JSlider slider = new JSlider();
Once after i wrote the statement:, and i compile i got the error. Error happen before i call the getValue() function.Java Code:JSlider source = e.getSource();
Is e.getSouce() don't return JSlider type object? hmmm...... it should return it.........
-
per the API (which you should probably read), getSource returns an object of Object type which you will need to cast to a JSlider -- if you know for certain that it is in fact a JSlider object.
Last edited by Fubarable; 06-28-2010 at 03:21 AM.
- 06-30-2010, 10:55 AM #8
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Adjusting function
By rootpi in forum New To JavaReplies: 1Last Post: 02-02-2010, 12:40 PM -
how to make JScrollpane not to fetch data while scrollbar is adjusting?
By saba in forum AWT / SwingReplies: 4Last Post: 01-21-2010, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks