Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to Use a JSlider - Java Swing

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post How to Use a JSlider - Java Swing

    This example code shows you how to use a JSlider swing component.



    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.JTextField;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
     
    public class Neo_2010_Slider extends JFrame
    {
     
        private static final long serialVersionUID = 1L;
     
        private Container container ;
        private JSlider slider1 ;
        private JSlider slider2 ;
        private JLabel lbl1 ;
        private JLabel lbl2 ;
        private JPanel panel1 ;
        private JPanel panel2 ;
        private JTextField txt1 ;
        private JTextField txt2 ;
     
        public Neo_2010_Slider() {
            super("Demo Slider Sample");
            setAlwaysOnTop(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBackground(new Color(14555));
            setSize(new Dimension(400,400));
            setResizable(true);
            /****************************************************************/
            container = getContentPane();
            BorderLayout containerLayout = new BorderLayout();
            container.setLayout(containerLayout);
     
            /****************** Labels Properties ********************************/
            lbl1 = new JLabel("Slider 1");
            lbl2 = new JLabel("Slider 2");
     
            /****************** TextField Properties ********************************/
            txt1 = new JTextField(4);
            txt2 = new JTextField(4);
     
            /****************** Sliders Properties ***********************************/
            slider1 = new JSlider(JSlider.HORIZONTAL,0,1000,0);//direction , min , max , current
            slider1.setFont(new Font("Tahoma",Font.BOLD,12));
            slider1.setMajorTickSpacing(100);
            slider1.setMinorTickSpacing(25);
            slider1.setPaintLabels(true);
            slider1.setPaintTicks(true);
            slider1.setPaintTrack(true);
            slider1.setAutoscrolls(true);
            slider1.setPreferredSize(new Dimension(500,500));
            slider2 = new JSlider(JSlider.VERTICAL,0,1000,500);//direction , min , max , current
            slider2.setFont(new Font("Tahoma",Font.BOLD,12));
            slider2.setMajorTickSpacing(100);
            slider2.setMinorTickSpacing(25);
            slider2.setPaintLabels(true);
            slider2.setPaintTicks(true);
            slider2.setPaintTrack(true);
            slider2.setAutoscrolls(true);
            /*************************** Controls Events ************************************/
            //When Changing Slider 1 Cursor...do this
            slider1.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    txt1.setText(String.valueOf(slider1.getValue()));
                }
            });
     
            //When Changing Slider 2 Cursor...do this
            slider2.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    txt2.setText(String.valueOf(slider2.getValue()));
                }
            });
     
            //When Press Enter After Change...do this
            txt1.addActionListener(new ActionListener() {    
                @Override
                public void actionPerformed(ActionEvent e) {
                    try
                    {
                        slider1.setValue(Integer.parseInt(txt1.getText()));
                    }
                    catch(Exception ex)
                    {
                        txt1.setText("ERROR");
                        txt1.setToolTipText("Set Value in Range between 0 - 1000 ") ;
                    }
                }
            });
     
            txt2.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    try
                    {
                        slider2.setValue(Integer.parseInt(txt2.getText()));
                    }
                    catch(Exception ex)
                    {
                        txt2.setText("ERROR");
                        txt2.setToolTipText("Set Value in Range between 0 - 1000 ") ;
                    }
                }
            });
     
            this.addFocusListener(new FocusListener() {
                @Override
                public void focusLost(FocusEvent e){
                }
     
                @Override
                public void focusGained(FocusEvent e) {
                    txt1.setText(String.valueOf(slider1.getValue()));
                    txt2.setText(String.valueOf(slider2.getValue()));
                }
            });
            /****************************************************************/
     
            panel1 = new JPanel();
            container.add(panel1, BorderLayout.WEST);
            panel1.add(lbl1);
            panel1.add(txt1);
            panel1.add(slider1);
     
            panel2 = new JPanel();
            container.add(panel2, BorderLayout.NORTH);
            panel2.add(lbl2);    
            panel2.add(txt2);
            panel2.add(slider2);
     
     
            setVisible(true);
        }
     
        public static void main(String args[])
        {
            new Neo_2010_Slider();
        }
    }

  2. The Following 3 Users Say Thank You to neo_2010 For This Useful Post:

    Fendaril (September 1st, 2009), JavaPF (July 8th, 2009), Json (July 8th, 2009)


  3. #2
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: How to Use a JSlider - Java Swing

    Thanks a lot for your comment...I'm just want to do what i have to do in this new and great forum

  4. The Following User Says Thank You to neo_2010 For This Useful Post:

    Cyloc (March 27th, 2010)

  5. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Use a JSlider - Java Swing

    Nice one. I'm a student working on a color picker that has a implemented JSlider with RGB picks. I'd like to change the slider's layout by replacing the slider background, and slider with my images. Here an example:

     
            JLabel redLabel = new JLabel("Red: ");
            JLabel greenLabel = new JLabel("Green: ");
            JLabel blueLabel = new JLabel("Blue: ");
            GridLayout grid = new GridLayout(4, 1);
            FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
            setLayout(grid);

    If it's possible, of course. If you have ideas on what to add to the color picker, I'd love to challenge myself, and code more on it.

    Example of what the slider looks like, and what details should be replaced with an image

  6. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: How to Use a JSlider - Java Swing

    It's bad form to resurrect an old and IMO singularly useless old dead thread (the tutorial linked form the JSlider API is far more comprehensive) and even worse form to hijack a thread with a new, different question.

    Get your own thread and I may have some suggestions.

    db

  7. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to Use a JSlider - Java Swing

    Quote Originally Posted by Darryl.Burke View Post
    It's bad form to resurrect an old and IMO singularly useless old dead thread (the tutorial linked form the JSlider API is far more comprehensive) and even worse form to hijack a thread with a new, different question.

    Get your own thread and I may have some suggestions.

    db
    This thread is a tutorial thread. It is designed to stay open to different questions just as long as they are related to the original post.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers