aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JSlider issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider issue" Watch "JSlider issue" New topic
Author

JSlider issue

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi,

I am new to Swing components. I am trying a scenario where in using a loop, I have an object(ball), which moves from one end to another end (source -- destination). Accordingiy, I want to keep a slider to view/control the movement of ball(just like a progress bar). Below is my code. The ball is moving well and there is no movement in the slider. I have used slider.setValue() method but is not working for me. Am I missing any thing? Any help would be highly appreciated.


Regards,
Kiran.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Do you see this output on the console?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Yes Maneesh. I can see the output. The ball is also moving but not the Slider bar.

Regards,
Kiran.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Run and try to understand this code. It is not clean and good code. But this is the best which I can come up with right now. Got to rush for a meeting.

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Thanks Maneesh. Your code is working. I tried to implement the same in my code. After further R&D, I found out that JFrame is a top level container and cannot be added like JPanel.
If I extend the class with JPanel, the code is not working(slider movement) but with JFrame, the code is working.
I have come to a conclusion that if at all we are doing some GUI, then its better to add all the JPanel's in a single JFrame class. Please let me know whether I have perceived correctly or not and whether it is a good practice.

Regards,
Kiran.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

The basic problem with your original code was the repaint call. You forgot that call repaints all the children!

In general, try and adhere to the following pattern.
1) One base container (typically JFrame)
2) JFrame has a content pane (again a container) to which you should be adding all the child components
3) Try to use simple components instead of complex components when you are playing with the graphics.

You are on the right path. With a little bit of more practice you should get the hang of it.
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Thanks a lot Maneesh for your help. One last query. I have created a button and implemented ActionListener for this button, so that when I click on this button, the ball should start moving(slider anyways is moving in sync with ball). Some how I am not getting the desired result. I have checked in debug mode, and surprisingly paintComponent() is not getting called even after the execution of repaint() method. I did try with this.repaint(), revalidate() and this.revalidate() methods. But is not working for me. Any clue to get the desired result would be of great help.

Regards,
Kiran.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

How are you getting the ball moving? Are you calling the flow() on actionPerformed?
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Yes Maneesh. I am using the below code. TestPanel.java remain the same (the one that you gave earlier).
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi,

After much struggle and R&D I found out a workaround for my issue. If I use Threads, then I will get the required output. this is because When an event is fired, EDT(EventDrivenThread) will be executing(taken from MainThread) and during this time, there won't be any updation on the screen. Once this thread ends, then paintGraphics() method is called and we can only view the starting and ending frame of our graphics. The changes that I made in my code are given below.

However in most of the articles, it is recommended to use Timer than Threads.



Regards,
Kiran.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Hello,

For updating GUI, can invokeLater help in this ?

Gail Anderson
Author
Ranch Hand

Joined: Jul 07, 2009
Posts: 44
Well, since this week this is also a JavaFX forum, I thought I would show all of you the sample Swing code implemented in JavaFX. I did add an "easeout" interpolator that slows down the movement at the end. If you remove the tween Interpolator.EASEOUT, then you'll get normal linear interpolation. I resisted the urge to add drop shadows to the circle, linear gradients to the background, etc.

The code does show the scene graph metaphor well. You can see that you "set things up" by defining your nodes (circles, rectangles, slider) and then it just goes! No painting, no threading, no runnables.



Gail
Gail Anderson
Author
Ranch Hand

Joined: Jul 07, 2009
Posts: 44
And, perhaps more elegant, but not exactly the same behavior is using this slider and completely removing the second timeline (the 100ms update timeline).



With a second slider you could reflect/control movement of the ball in the y direction.

I hope you like!

Gail
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi Miko,

The code that you gave is:


I have tested in my code and unfortunately it is not working. There is one good link to go through the paint() methods and different ways of using it. Here it is:
http://java.sun.com/products/jfc/tsc/articles/painting/

My sample code is below. (note that my code in not an ideal code to implement)


Regards,
Kiran.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Hey,

You are putting EDT to sleep, so it wont paint nothing. Creating new Thread to do painting and sleeping and calling SwingUtilities.invokeLater to update GUI via EDT.

Or you can just use Swing timer for this. I writed the code for you otherwise it's too complicated for me to explain...

And i modified the code quite much too...

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi Mikko,
What you said was correct. I should be using Timers instead of Thread for the time delay. I have added you code to JFrame and ran it. It was cool. Thanks for the update.
Regards,
Kiran.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

K Kiran Kumar wrote:I should be using Timers instead of Thread for the time delay.

The added bonus is that you don't need EventQueue.invokeLater / SwingUtilities.invokeLater - the Timer's ActionListener's actionPerformed method is called on the EDT.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: JSlider issue