Hi. I've got a JSlider in my applet which I want to change the position of.

int initValue = 10;
int minimum = 10;
int maximum = 20;

JSlider slider = new JSlider(JSlider.HORIZONTAL, minimum, maximum, initValue);

I've got this in the applets init() method.

add(slider); //add the slider to the panel

I've tried using:

slider.setLocation(400, 40);

That doesn't seem to work. I've tried googling around. Can't find anything :S.
Thanks

It depends on the layout.
What is layot of panel? //add the slider to the panel
In which layouts the .setLocation-method is effective?

I haven't set a layout for the panel. The slider gets placed on the North of the panel. I can't move it from this position. I don't know how.

train 2 examples:

import java.awt.BorderLayout;
import javax.swing.JApplet;
import javax.swing.JSlider;

public class NewJApplet extends JApplet {

    int initValue = 10;
    int minimum = 10;
    int maximum = 20;
    JSlider slider = new JSlider(JSlider.HORIZONTAL, minimum, maximum, initValue);

    @Override
    public void init() {
        add(slider,BorderLayout.NORTH); //add the slider to the panel
        slider.setLocation(400, 40);
        System.out.println(this.getLayout());
    }    
}
//result: java.awt.BorderLayout[hgap=0,vgap=0]   <-- default border
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class NewJApplet1 extends JApplet {

    int initValue = 10;
    int minimum = 10;
    int maximum = 20;
    JSlider slider = new JSlider(JSlider.HORIZONTAL, minimum, maximum, initValue);

    @Override
    public void init() {
        JPanel northPanel = new JPanel();
        northPanel.setPreferredSize(new Dimension(600, 100));
        northPanel.setBackground(Color.red);
        northPanel.setLayout(null);//!!!!!!!!!! all in your hands


        northPanel.add(slider); //add the slider to the panel
        slider.setLocation(400, 40);
        slider.setSize(160, 20);// !!!!!!!!!!!!!!! try comment this line

        add(northPanel, BorderLayout.NORTH);
        System.out.println(northPanel.getLayout());
        System.out.println(slider.getLocation());
        
         System.out.println(new Point());
    }
    // TODO overwrite start(), stop() and destroy() methods
}
//result: null
//java.awt.Point[x=400,y=40]
//java.awt.Point[x=0,y=0] <-- default point
commented: Very good code examples. Shows time spent on answer. Thanks +1

Thanks once again for your help quuba. Really appreciate it. With your code, I worked out a really simple method to the problem.

setLayout(null); //set layout of this applet	
      
slider.setLocation(380, 100); //set slider location	      
slider.setSize(200, 40); //set slider size	      
slider.setMajorTickSpacing( 10 ); //add the tick spacing	      
slider.setMinorTickSpacing( 2 );	      
slider.setPaintTicks( true );		      
slider.setPaintLabels( true ); //add tick labels	      
slider.addChangeListener( this ); //add event  

add(slider); //add slider to applet

I just needed to set the layout for the applet to null which then allowed me to use slider.setLocation() to move the slider around.

I did manage to get it working using your example before though. I used the setOpaque() method to set the panel to transparent.

setLayout(new GridLayout(4,3)); //set layout of this applet	      
JPanel northPanel = new JPanel(); //new panel
northPanel.setPreferredSize(new Dimension(600, 600));
northPanel.setOpaque(false);
northPanel.setLayout(null);
northPanel.add(slider); //add the slider to the panel
slider.setLocation(380, 100);
slider.setSize(200, 40);
slider.setMajorTickSpacing( 10 );
slider.setMinorTickSpacing( 2 );
slider.setPaintTicks( true );
slider.setPaintLabels( true );
slider.addChangeListener( this );

add(northPanel);

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.