Jump to content

dont know why my slider wont draw

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
Hey guys,

I cant seem to get it to draw the slider on my JFrame. Here is the code for the GUIApp


import javax.swing.*;

import java.awt.*;


public class GUIApp

{

    DrawingPanel dp;

    LabelPanel lp;

    XMovePanel xmp;

    YMovePanel ymp;

    

    public GUIApp()

    {        

        JFrame win = new JFrame("Drawing App");

        win.setSize(500, 450);

        win.setLocation(50,50);

        win.setLayout(new BorderLayout()); 

                       

        win.add(new DrawingPanel(),BorderLayout.CENTER);

        win.add(new XMovePanel(),BorderLayout.NORTH);

        win.add(new YMovePanel(),BorderLayout.EAST);

        win.add(new Widgets(),BorderLayout.SOUTH);

        win.setVisible(true); 

        win.repaint();

        

    }

    



}


I also have the XMovePanel which has the slider in it.


import java.awt.*;

import javax.swing.*;


public class XMovePanel extends JPanel

{

        static final int MIN = 0;

        static final int MAX = 450;

        static final int INIT = 100;


    

    

    public XMovePanel()

    {

        

        JSlider XCoord = new JSlider(JSlider.HORIZONTAL,MIN, MAX, INIT);

        //XCoord.addChangeListener(this);

        

        

            setBackground (Color.lightGray);

            setPreferredSize (new Dimension (10, 30));

            setLayout(null);

            


    }

    


}


Any help would be fantastic thanks.

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Well currently the code you provided doesn't even build. What is a DrawingPanel or YMovePanel object? And the reason the XMovePanel class isn't drawing the JSlider is because you didn't add() the JSlider object to the XMovePanel. You should also maintain an object-based reference to the JSlider, like this:
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.BoxLayout;

public class XMovePanel extends JPanel
{
    public static final int MIN = 0;
    public static final int MAX = 450;
    public static final int INIT = 100;

    private JSlider xcoord;

    public XMovePanel()
    {
        super(new BoxLayout(this, BoxLayout.LINE_AXIS));
        xcoord = new JSlider(JSlider.HORIZONTAL, MIN, MAX, INIT);
        [b]add(xcoord);[/b]
        setBackground(java.awt.Color.lightGray);
        setPreferredSize(new java.awt.Dimension(10, 30));
    }
}

That should paint the JSlider object whenever the JPanel would be painted.
Wow I changed my sig!

#3
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
Yeah it doesnt build because I didnt include the other parts. Sorry I didnt think they were relevant :)
Ill give that a go and see what happens, Thanks Zeke.

#4
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
No good, couldnt get that to work either. Here is another part of the code which draws it.

import javax.swing.*;

import java.awt.*;


public class GUIApp

{

    DrawingPanel dp;

    LabelPanel lp;

    XMovePanel xmp;

    YMovePanel ymp;

    

    public GUIApp()

    {        

        JFrame win = new JFrame("Drawing App");

        win.setSize(500, 450);

        win.setLocation(50,50);

        win.setLayout(new BorderLayout()); 

               

        

        win.add(new DrawingPanel(),BorderLayout.CENTER);

        win.add(new XMovePanel(),BorderLayout.NORTH);

        win.add(new YMovePanel(),BorderLayout.EAST);

        win.add(new Widgets(),BorderLayout.SOUTH);

        win.setVisible(true); 

        win.repaint();

        

    }

    



}

and .....

import java.awt.*;

import javax.swing.*;


public class DrawingPanel extends JPanel

{


    

    public DrawingPanel()

    {

            setBackground (Color.white);

            setPreferredSize (new Dimension (450, 400));

            setLayout(null);


    }

    

  

}

and...

import javax.swing.*;

import java.awt.*;


public class Widgets extends JPanel

{

    

    public Widgets ()

    {

        

            setBackground (Color.lightGray);

            setPreferredSize (new Dimension (10, 100));

            setLayout(new GridLayout(3,1));

            add(new LabelPanel());

            add(new TextPanel());

            add(new ButtonPanel());

        

    }

}


I still dont understand..

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
All right, well I tested this EXACT program, and it does work:

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.BoxLayout;
import javax.swing.JFrame;

public class Simple extends JPanel
{
    private JSlider xcoord;

    public Simple()
    {
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        xcoord = new JSlider(JSlider.HORIZONTAL, 0, 450, 100);
        add(xcoord);
        setBackground(java.awt.Color.lightGray);
        setPreferredSize(new java.awt.Dimension(10, 30));
    }

    public static void main(String[] args)
    {
        Simple s = new Simple();
        JFrame f = new JFrame();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(s);
        f.setSize(300, 200);
        f.setVisible(true);
    }
}

Are you avoiding adding a layout to your JPanel? I know you seem to be deliberately setting the layout to null in several of your objects, why? This is probably the source of your problem, just throw in a BoxLayout to appease the JPanel and be on your way. :)
Wow I changed my sig!

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I changed your XMovePanel's constructor into:

 public XMovePanel()

    {

        

        JSlider XCoord = new JSlider(JSlider.HORIZONTAL,MIN, MAX, INIT);

        //XCoord.addChangeListener(this);

        [COLOR="orange"][B]super.add(XCoord);[/B][/COLOR]

        

            setBackground (Color.lightGray);

            setPreferredSize(new Dimension (10, 30));

            [COLOR="orange"][B]//[/B][/COLOR]setLayout(null);   

    }

    


And i got a slider on top.

Note that if you set the layout of a panel to null, you must proved extra info to the item you want to add.
This extra info consists of:
  • Position(x, y)
  • Size(width, height)

So if you really want a null-layout you must do something like:

public XMovePanel()

    {


        JSlider XCoord = new JSlider(JSlider.HORIZONTAL,MIN, MAX, INIT);

        //XCoord.addChangeListener(this);

        setLayout(null);

        [COLOR="orange"][B]XCoord.setLocation(10,10);

        XCoord.setSize(200,10);[/B][/COLOR]

        super.add(XCoord);


        setBackground(Color.lightGray);

        setPreferredSize (new Dimension (10, 30));

    }



#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@oxano: You shouldn't need to use super.add() at all, add() should work just fine unless he wrote an overriding method for add(). :P

I noticed you commented out the setLayout() call, which is what I think the problem is.
Wow I changed my sig!

#8
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
Genius. It worked. Thank you so much.

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
But if i don't add the slider to the jpanel, it won't show, will it? Or because it's a property of the class it's allready added?