gooberdope
gooberdope

Reputation: 75

Swing slider not showing up

I'm trying to add a slider to my frame using JSwing components, but for some reason it's not working. I tried looking up possible solutions but they either don't apply or haven't worked for me such as setting the size or visibility and what not. Basically, why doesn't my slider show up?

I've got a carIcon that shows up just fine, but the slider won't. Here's my code.

   public static void main(String[] args)
    {
    JFrame frame = new JFrame("Slider Tester");

    MoveableShape shape = new CarShape(125, 100, CAR_WIDTH);
    slider bar = new slider();

    ShapeIcon icon = new ShapeIcon(shape,
        ICON_WIDTH, ICON_HEIGHT);

    JLabel label = new JLabel(icon);
    frame.setLayout(new FlowLayout());
    frame.add(label);
    frame.add(bar);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(100,100);
    frame.pack();
    frame.setVisible(true);

    }

and my slider

public class slider extends JPanel implements ChangeListener
{

    private JSlider carSlider;

    public slider()
    {
    carSlider = new JSlider();
    carSlider.addChangeListener(this);
    }

    @Override
    public void stateChanged(ChangeEvent ce)
    {
    }
}

Upvotes: 0

Views: 950

Answers (2)

tenorsax
tenorsax

Reputation: 21223

The slider is not added to the panel. Add this line to slider constructor:

add(carSlider);

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347194

So, when were you intending to add the slider??

public slider()
{
    carSlider = new JSlider();
    carSlider.addChangeListener(this);
}

Creates the slider nicely, but does nothing with it...

public slider()
{
    carSlider = new JSlider();
    carSlider.addChangeListener(this);
    add(carSlider); // <-- Look and me, I'm new :D
}

Will work better

Upvotes: 3

Related Questions