Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

dont know why my slider wont draw

setpreferredsize

  • Please log in to reply
8 replies to this topic

#1 JaseR33

JaseR33

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 17 October 2010 - 01:42 AM

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.
  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 17 October 2010 - 03:03 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 JaseR33

JaseR33

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 17 October 2010 - 03:06 AM

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.
  • 0

#4 JaseR33

JaseR33

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 17 October 2010 - 03:13 AM

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..
  • 0

#5 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 17 October 2010 - 03:28 AM

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. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 17 October 2010 - 03:32 AM

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));
    }

  • 0

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 17 October 2010 - 03:36 AM

@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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 JaseR33

JaseR33

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 17 October 2010 - 04:10 AM

Genius. It worked. Thank you so much.
  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 17 October 2010 - 05:23 AM

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?
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download