Results 1 to 10 of 10
Like Tree3Likes
  • 3 Post By awinston

Thread: Help with sliders in java

  1. #1
    Ajinkya is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Help with sliders in java

    i have three sliders s1,s2 and s3. s3 is non-editable where as s1 and s2 are editable. s1 and s2 have min and max of 0 and 200 whereas s3 has a min and max of 0 and 50.value of s3 by default is 50. what i want is when i increase s1 or s2 by 1 or more, s3 should go down by same value and if s3 reaches 0 s1 or s2 cannot be futher increased. same with decrease.when i decrease s1 or s2 by 1 or more, s3 should go up by same value and if s3 reaches 50 s1 or s2 cannot be futher decreased.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with sliders in java

    Do you have code you are working on to do this? Please post a small, simple program that compiles and executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ajinkya is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with sliders in java

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * delete.java
     *
     * Created on Jul 31, 2012, 5:53:38 PM
     */
    /**
     *
     * @author Owner
     */
    public class delete extends javax.swing.JFrame {
    
        /** Creates new form delete */
        public delete() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            s1 = new javax.swing.JSlider();
            s2 = new javax.swing.JSlider();
            jSlider1 = new javax.swing.JSlider();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            s1.setMaximum(200);
            s1.setValue(0);
    
            s2.setMaximum(200);
            s2.setValue(0);
    
            jSlider1.setMaximum(50);
            jSlider1.setEnabled(false);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(40, 40, 40)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(s2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(s1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(160, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(57, 57, 57)
                    .addComponent(s1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)
                    .addComponent(s2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(43, 43, 43)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(103, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new delete().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JSlider jSlider1;
        private javax.swing.JSlider s1;
        private javax.swing.JSlider s2;
        // End of variables declaration
    }
    this is the code. what i want is if i increase s1 or s2 , s3 should go down by same value and if s3==0 then s1 or s2 cannot be futher increased

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with sliders in java

    A bit of 101 math would show that the range of s1 and s2 is also [0, 50].

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with sliders in java

    The code does not have any listeners to react to changes in the sliders's values. You'll need to add them to do what you want.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Ajinkya is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with sliders in java

    Quote Originally Posted by Norm View Post
    The code does not have any listeners to react to changes in the sliders's values. You'll need to add them to do what you want.
    what listener should i add and what should i code in it

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with sliders in java

    Read the API doc for the JSlider class to see how to use listeners with it.
    Add listeners to the sliders in your code and to see data is available when they are called, print out the event object that is passed to the listener. Also look at the API doc for the event object passed to the listener to see what data you can get from the object. Once you see what data is available and how to get it, you can then start working on the logic to solve your problem.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default Re: Help with sliders in java

    And when you know what sort of listener, here's the tutorial for how to use them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Ajinkya is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with sliders in java

    i'm a noob so can u write me a sample program as an example. plz ?

  10. #10
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Help with sliders in java

    Quote Originally Posted by Ajinkya View Post
    i'm a noob so can u write me a sample program as an example. plz ?
    Self-deprecation is never a good idea. The tutorial provided by Tolls has sample programs and is very thorough.
    Norm, DarrylBurke and Fubarable like this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. How to use SWT Sliders
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:06 PM
  2. Replies: 0
    Last Post: 07-02-2008, 08:06 PM

Posting Permissions

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