Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Slider - digital/Anolog...

1. When chk is checked (is Anolog) the slider selection should be able to read 1.00, 2.00, 3.00 and the values in between like 1.10, 2.01, 2.56 etc. And the slider shouldn't jump from one tick mark to another.

2. But, when chk is unchecked (is Digital) the slider selection should be able to read 1, 2, 3 etc. And the slider should jump from one tick mark to another.

Part 1 is working fine.

Question: How part 2 could be made to work?

Thank you.


import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
import java.text.DecimalFormat;

public class GraduatedSlider extends JFrame implements ChangeListener, ItemListener {

    JSlider slider;
    JLabel lbl = new JLabel();
    JCheckBox chk = new JCheckBox();
    boolean digital=false;
    
    DecimalFormat df=new DecimalFormat("0.00");
    DecimalFormat df2=new DecimalFormat("0");
    
    public GraduatedSlider(){

       Hashtable h = new Hashtable();

        for(int i=0;i<21;i++){
            String str=df2.format((double)i);
            h.put(i,new JLabel(str));
        }

        JPanel panel = new JPanel();
        slider = new JSlider (0,20);
        slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);

        slider.setSnapToTicks(true);
        slider.setPaintTicks(true);
        slider.setLabelTable(h);
        slider.setPaintLabels(true);
        slider.addChangeListener(this);//*************************

        lbl.setText("Slider Value");
        chk.setText("Check for Anolog");
        chk.addChangeListener(this);//addItemListener(this);//********************

        panel.setLayout(new BorderLayout());
        panel.add(lbl, BorderLayout.NORTH);
       
        panel.add(slider);

        panel.add(chk, BorderLayout.SOUTH);

        Container c = this.getContentPane();
        c.add(panel);

        this.setSize(750,300);
        this.setTitle("Test Slider");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

//    public void setValue(double i){
//        lbl.setText(Double.toString(i));
//    }

    public void ItemListener(ItemListener e) {
        System.out.println("checked");
    }
    
    public void stateChanged(ChangeEvent e){
        
        Object source = e.getSource();
        if(e.getSource()==chk){
            digital=chk.isSelected();
            if (!digital)
                 chk.setText("Check for Digital");               
            else
                chk.setText("Uncheck for Anolog");    
        }else if(e.getSource()==slider){
            System.out.println(slider.getValue());
//            setValue(slider.getValue());
            int kd = slider.getValue();
            double k = slider.getValue();
//            double val = 10.0*k/1000.0;
            if(digital)
                lbl.setText("Slider Value: " + df2.format(kd));        
            else
                lbl.setText("Slider Value: " + df.format(k));            
        }
    }

    public static void main(String[] args) {
        new GraduatedSlider();
    }

    class UpperPanel{
        public UpperPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,80);
        }
    }

    class LowerPanel{
        public LowerPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,20);
        }
    }
    public void itemStateChanged(ItemEvent ie){
        boolean b =    slider.getSnapToTicks();
        slider.setSnapToTicks(!b);
        digital=!digital;
    }
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 11
  • 7
1 Solution
 
for_yanCommented:
But it jumps form one tick to another - you cannot stop it in between - I don'ty understadn what you mean?
0
 
for_yanCommented:
Once you make

   slider.setSnapToTicks(true);

and it will not allow to stop in between - doesn't it work for you this way?
It works for me
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
please use the following code, it is a bit different:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
import java.text.DecimalFormat;

public class GraduatedSlider extends JFrame implements ChangeListener, ItemListener {

    JSlider slider;
    JLabel lbl = new JLabel();
    JCheckBox chk = new JCheckBox();
    boolean digital=true;
    
    DecimalFormat df=new DecimalFormat("0.00");
    DecimalFormat df2=new DecimalFormat("0");
    
    public GraduatedSlider(){

       Hashtable h = new Hashtable();

        for(int i=0;i<21;i++){
            String str=df2.format((double)i);
            h.put(i,new JLabel(str));
        }

        JPanel panel = new JPanel();
        slider = new JSlider (0,20);
        slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);

        slider.setSnapToTicks(true);
        slider.setPaintTicks(true);
        slider.setLabelTable(h);
        slider.setPaintLabels(true);
        slider.addChangeListener(this);//*************************

        lbl.setText("Slider Value");
        chk.setText("Check for Digital");
        chk.addChangeListener(this);//addItemListener(this);//********************

        panel.setLayout(new BorderLayout());
        panel.add(lbl, BorderLayout.NORTH);
       
        panel.add(slider);

        panel.add(chk, BorderLayout.SOUTH);

        Container c = this.getContentPane();
        c.add(panel);

        this.setSize(750,300);
        this.setTitle("Test Slider");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

//    public void setValue(double i){
//        lbl.setText(Double.toString(i));
//    }

    public void ItemListener(ItemListener e) {
        System.out.println("checked");
    }
    
    public void stateChanged(ChangeEvent e){
        
        Object source = e.getSource();
        if(e.getSource()==chk){
            digital=chk.isSelected();
            if (!digital)
                 chk.setText("Check for Digital");               
            else
                chk.setText("Uncheck for Anolog");    
        }else if(e.getSource()==slider){
            System.out.println(slider.getValue());
//            setValue(slider.getValue());
            int kd = slider.getValue();
            double k = slider.getValue();
//            double val = 10.0*k/1000.0;
            if(digital)
                lbl.setText("Slider Value: " + df2.format(kd));        
            else
                lbl.setText("Slider Value: " + df.format(k));            
        }
    }

    public static void main(String[] args) {
        new GraduatedSlider();
    }

    class UpperPanel{
        public UpperPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,80);
        }
    }

    class LowerPanel{
        public LowerPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,20);
        }
    }
    public void itemStateChanged(ItemEvent ie){
        boolean b =    slider.getSnapToTicks();
        slider.setSnapToTicks(!b);
        digital=!digital;
    }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
But that one worked - why to change and then try to get back ?
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
can we say:


if(digital==true){
     slider.setSnapToTicks(true);
else
     slider.setSnapToTicks(false);
0
 
for_yanCommented:
sure we can, that's how it was
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
You are write. The reason I starting revising was to simplify

        for(int i=0;i<21;i++){
            String str=df2.format((double)i);
            h.put(i,new JLabel(str));
        }

portion and also make the chk caption to change based on user action on it.
0
 
for_yanCommented:

This is now again changing to snap and npt to snap
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
import java.text.DecimalFormat;

public class GraduatedSlider extends JFrame implements ChangeListener, ItemListener {

    JSlider slider;
    JLabel lbl = new JLabel();
    JCheckBox chk = new JCheckBox();
    boolean digital=true;
    
    DecimalFormat df=new DecimalFormat("0.00");
    DecimalFormat df2=new DecimalFormat("0");
    
    public GraduatedSlider(){

       Hashtable h = new Hashtable();

        for(int i=0;i<21;i++){
            String str=df2.format((double)i);
            h.put(i,new JLabel(str));
        }

        JPanel panel = new JPanel();
        slider = new JSlider (0,20);
        slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);

        slider.setSnapToTicks(false);
        slider.setPaintTicks(true);
        slider.setLabelTable(h);
        slider.setPaintLabels(true);
        slider.addChangeListener(this);//*************************

        lbl.setText("Slider Value");
        chk.setText("Check for Digital");
        chk.addChangeListener(this);//addItemListener(this);//********************

        panel.setLayout(new BorderLayout());
        panel.add(lbl, BorderLayout.NORTH);
       
        panel.add(slider);

        panel.add(chk, BorderLayout.SOUTH);

        Container c = this.getContentPane();
        c.add(panel);

        this.setSize(750,300);
        this.setTitle("Test Slider");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

//    public void setValue(double i){
//        lbl.setText(Double.toString(i));
//    }

    public void ItemListener(ItemListener e) {
        System.out.println("checked");
    }
    
    public void stateChanged(ChangeEvent e){
        
        Object source = e.getSource();
        if(e.getSource()==chk){
            digital=chk.isSelected();
            if (!digital)   {
                 chk.setText("Check for Digital");
                   slider.setSnapToTicks(false);
            }
            else       {
                chk.setText("Uncheck for Anolog");
                   slider.setSnapToTicks(true);
            }
        }else if(e.getSource()==slider){
            System.out.println(slider.getValue());
//            setValue(slider.getValue());
            int kd = slider.getValue();
            double k = slider.getValue();
//            double val = 10.0*k/1000.0;
            if(digital)
                lbl.setText("Slider Value: " + df2.format(kd));        
            else
                lbl.setText("Slider Value: " + df.format(k));            
        }
    }

    public static void main(String[] args) {
        new GraduatedSlider();
    }

    class UpperPanel{
        public UpperPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,80);
        }
    }

    class LowerPanel{
        public LowerPanel(){
        }

        public Dimension getPreferredSize() {
            return new Dimension(80,20);
        }
    }
    public void itemStateChanged(ItemEvent ie){
        boolean b =    slider.getSnapToTicks();
        slider.setSnapToTicks(!b);
        digital=!digital;
    }
}

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
It will be helpful to apply the changes back to the my latest version so that I will not get dizzy and folow it easily.
0
 
for_yanCommented:
The code above ID:36967426
is from your code which you just posted
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Below as you can see, the slider is on about 3.5 but it shows 4.00 on the lbl.
Slider.png
0
 
for_yanCommented:
But on my code it was all working - I didn't chek this part now.
0
 
for_yanCommented:
Fisrt you write a program - then when you achieve already some point (especially in the interface realted programs) - you change one small point at a time - then run and check -
if that small point didn't work (or spoiled some other point) rollback and change again.
From certain point it pays off being very very incremental - you just cliimb one small bump at a time - and always being on the ay up - this is believe me much more
effective than trying to jum in big leaps losing something on the way.
0
 
for_yanCommented:
I suggest tio return back to that code _ I think I even had different events for checkbox (itemStateChanged or something) and for slider
- it was mucgh more conveneient
Besides you never write if(e.getSource() == chk) - this is just java  blasphemy - you know ytou never should use == for objects
0
 
for_yanCommented:
Let's go back to that code and start from there.
What did you want to change from that point?
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
ok, thank you for good advise. I will go back to yourgood solution to include the samll changes I had and then get back to you when I am done and there are pending issues with it.
0
 
for_yanCommented:
OK - just do everything very very incrementally - never losing wha you already have - the slower you drive - the farther you'll be - that's about programming - at least after your program reaches certain size.

I may be sleeping for a few hours from now - but I'll chheck with you in the morning (or btter to say in the late morning, as it is formally moring already)
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
There was some noise in the neighborhood so I couldn't sleep hence trying to dring (do) some Java. lol

I was sleepy and messed up your good solution, sorry about it. I will correct it and get back to you.  

You are with EE almost 24/7 and it is very much appreciated. You probably having lots of Java yourself.

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now