Search code examples
javaswinglayoutjslider

How to set position of JSlider in window?


i am trying to put JSlider at bottom of application but it doesn't work. JSlider actually works, display everything, i just have to set it in bottom that only remain.

if anyone knows it then suggest me what i will have to do ?

my code :

       final JSlider slider1 ;
       slider1 = new JSlider(JSlider.HORIZONTAL,0,5,1);//direction , min , max , current

       slider1.setMajorTickSpacing(100);
       slider1.setMinorTickSpacing(25);
       slider1.setPaintLabels(true);
       slider1.setPaintTicks(true);
       slider1.setPaintTrack(true);
       slider1.setAutoscrolls(true);

       slider1.setPreferredSize(new Dimension(200,50));
       add(slider1);
       slider1.addChangeListener(new ChangeListener() 
       {

        @Override
        public void stateChanged(ChangeEvent arg0) 
        {
            // TODO Auto-generated method stub
            fval=slider1.getValue();
            repaint();
        }
       });

any suggestion please...


Solution

  • If you want your JSlider at the bottom try this:

    frame.add(slider, BorderLayout.SOUTH);
    

    at your code I think it is enough if you add to

    add(slider1);
    

    this:

    add(slider1, BorderLayout.SOUTH);
    

    EDIT: To display a set of slider on a JFrame with your picture, add your picture at a JPanel and your slider to a JPanel. So it is easier to place it.

    Than you could use:

    JPanel panel = new JPanel(new GridLayout(1, 0, 1, 0);
    panel.add(slider1);
    panel.add(slider2);
    ...
    add(panel, BorderLayout.SOUTH);
    add(picturepanel, BorderLayout.CENTER);
    

    All kinds of Layouts

    If you need more precision use a grid Layout!