Results 1 to 8 of 8

Thread: JSlider problem

  1. #1
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default JSlider problem

    i want to pass the variable radio from class SliderListener implements ChangeListener to clase1

    why this doesn't work?
    java code:
    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    
    
     public class clase1 extends JFrame{
         
          clase2_dibujo di1=new clase2_dibujo(); 
          JSlider slide1=new JSlider(0,100,50); 
          JPanel panel2=new JPanel();
        int radio;
          
    public clase1(){ 
                     
            super ("jose");
            super.setSize(500,500);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            BorderLayout lay1=new BorderLayout();
            Container contenedor = getContentPane();       
            contenedor.setLayout(lay1);
    
               //panel sur
            
            GridLayout lay2=new GridLayout();
            
            contenedor.add(panel2,BorderLayout.SOUTH);
                   
            slide1.setMajorTickSpacing( 20 );
            slide1.setMinorTickSpacing(10);
            slide1.setPaintTicks( true );
            slide1.setPaintLabels(true);
         
            SliderListener slide2=new SliderListener();
            slide1.addChangeListener(slide2);
            
            
    System.out.println(radio);
            
            panel2.add(slide1);
           //panel central
        
            contenedor.add(di1,BorderLayout.CENTER);//(di1 es un panel!!!)
            di1.a=radio;    
                   panel2.repaint();
            setVisible(true);  
    }
    
    
    
    class SliderListener implements ChangeListener {
    
    public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider) e.getSource();
    radio=slide1.getValue();
    }
    }
    }
    Last edited by ktden; 03-29-2013 at 06:58 PM. Reason: added [code] ... [/code] tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: JSlider problem

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: JSlider problem

    What evidence do you have that it isn't working? Did you put a print statement in your listener to see if it was responding? Also, you are missing some code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: JSlider problem

    what i want is to read line 57 --> radio=slide1.getValue();
    in line 40 -->System.out.println(radio);
    its just a tipical exercise where u manage a radious circle with a jslider, im trying to make it by my own and in the way i understand it, but i dont know what is wrong and i didnt find the solution googeling

    the piece of code missing is in another class;


    java code:
    Java Code:
    public class clase2_dibujo extends JPanel {
    public int a=100;
    
        public void paintComponent (Graphics g){
            
            super.paintComponent(g);
            this.setBackground(Color.GREEN);//fondo de pantalla
            g.setColor(Color.RED);//de que color queremos pintar
            g.fillOval(150, 150, a, a);//dibujar circulo
        }        
    }
    
    public class main {
         public static void main(String[]args){
              clase1 c1=new clase1();
         }
    }
    Last edited by ktden; 03-29-2013 at 06:56 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: JSlider problem

    Your sliderListener is working fine. But once radio is set, you don't have any event taking place to alert you to that fact.

    So take advantage of the slider event itself. Add the following in your sliderListener after you get the source of the event.

    Java Code:
    di1.a =slide1.getValue();
    repaint();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: JSlider problem

    thanks jim,
    it works to solve the exercise, my ball goes big and small while i move the slider...but....
    is there a way to pass the slide1.getValue();
    to System.out.println(radio);(line38)?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: JSlider problem

    Not as far as I know. The problem is that the events can happen at any time and are handled by the event dispatch thread which is different the one in which your main program is running. You could use the propertyChangeListener to fire off an event that indicates radio was updated. But you still have essentially the same problem. There are other ways when programming such as using different threads. When using threads you can wait for a variable to change and then act upon that change. But I would just recommend that you process the slider events within the sliderListener. However, if you take too long to do the processing, other events will be held up and your app will either be slow or non-responsive. So you should then kick off a thread to do the processing. All of this is pretty well described in the Java Tutorials and related supporting articles. Check out my signature link.


    On a different note. Your class names should start with a capital letter by convention and be in camel case. And naming a class Main is not a good idea (in my opinion) because it could become confusing when used with the static main entry point of the same name. And there is really no need to extend JFrame. Just create an instance and use that.

    Regards,
    Jim
    Last edited by jim829; 03-29-2013 at 09:11 PM. Reason: JFrame
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: JSlider problem

    thanks for your answer and your suggestions jim

Similar Threads

  1. How to improve Jslider
    By JohnST in forum New To Java
    Replies: 3
    Last Post: 01-12-2010, 02:04 AM
  2. Jslider changeListner
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 09-14-2009, 05:04 PM
  3. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 02:48 PM
  4. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 06:02 PM
  5. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •