Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Jslider problem

Thread view: 
Daniel - 26 Oct 2005 02:54 GMT
Hi
I have a minor, but none the less annoying problem with a JSlider.
Simply put the slider has values ranging from 1 to 30.
I want the major ticks (i.e labels) to indicate 1,5,10,15,20,25 and 30
and a minor tick on every in between (i.e 2,3,4,6,7 etc)
Now this seems rather simple, but I can't get my head aroud it.
If I use slider.setMajorTickSpacing(5) i get 1, 6, 11 and with a
.setMajorTickSpacing(4) i get 1,5,9,
the problem it seems is that I want the second label to appear 4 ticks
from the prvious one, and all lables after that every 5 steps.
Is there a resonably simple way to do this?
I have tried using a custom dictionary with lables saying 1,5,10 etc..
But since this is such small numbers the users will notice that the
lables mark the wrong numbers.

regards
Daniel
Michael Dunn - 26 Oct 2005 03:52 GMT
is this what you're trying to do?

import javax.swing.*;
import java.awt.*;
class Testing extends JFrame
{
 public Testing()
 {
   setLocation(400,200);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JSlider slider = new JSlider(1,30,1);
   java.util.Hashtable labelTable = new java.util.Hashtable();
   labelTable.put(new Integer(1), new JLabel("1"));
   labelTable.put(new Integer(5), new JLabel("5"));
   labelTable.put(new Integer(10), new JLabel("10"));
   labelTable.put(new Integer(15), new JLabel("15"));
   labelTable.put(new Integer(20), new JLabel("20"));
   labelTable.put(new Integer(25), new JLabel("25"));
   labelTable.put(new Integer(30), new JLabel("30"));
   slider.setLabelTable(labelTable);
   slider.setMinorTickSpacing(1);
   slider.setPaintTicks(true);
   slider.setPaintLabels(true);
   JPanel jp = new JPanel();
   jp.add(slider);
   getContentPane().add(jp);
   pack();
 }
 public static void main(String[] args){new
Testing().setVisible(true);}
}
Daniel - 26 Oct 2005 05:23 GMT
That is pretty much exactly what I want to do!
I see the difference between what I've tried and what you suggest is
that you do not use slider.setMajorTickSpaceing();
The sollution was right under my nose and I missed it!
Thank you very much for your help!

regards
Daniel

>is this what you're trying to do?
>
[quoted text clipped - 27 lines]
>Testing().setVisible(true);}
>}
Roedy Green - 26 Oct 2005 06:09 GMT
On Wed, 26 Oct 2005 01:54:07 GMT, Daniel
<daik.no-spam@mds.nospam.mdh.se> wrote, quoted or indirectly quoted
someone who said :

>I have a minor, but none the less annoying problem with a JSlider.
>Simply put the slider has values ranging from 1 to 30.
[quoted text clipped - 3 lines]
>If I use slider.setMajorTickSpacing(5) i get 1, 6, 11 and with a
>.setMajorTickSpacing(4) i get 1,5,9,

Here is how I would tackle the problem. I go sniffing around Sun's
code to find the code that paints the ticks and little numbers.

I extend that's method's class with some similar code that takes an
extra parm to shift the origin.  I dig around for a method to replace
the standard class with mine. You might do it with a clipregion
adjustment.  Logically paint some unwanted axis to the left, but don't
display it.

If there is no replacement hook,  extend the mother class with such a
hook.

If it starts to get too ugly, just write your own specialised Slider
from scratch extending JPanel doing everything exactly the way you
want with drawString and fillRect in paintComponent.

Note. it is called JSlider not Jslider.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.