1

I Know these kind of question have been asked but i couldn't find any solution to my problem.

I'm trying to paint some animation in my JPanel, which will be in a JFrame. The JPanel isn't visible, the JFrame is visible and also the testing label I've put in it. Also, i can't set the JFrame background for some reason. Here's the code which doesn't work: (The constructor is in another class in the project).

public class WindowClass extends JPanel implements ActionListener{

Graphics graphics;
JFrame window;
Timer timer;

private JLabel label = new JLabel("Best Label Around");
private int height;
private int width;
private Color bgColor;


public void init(){

    window = new JFrame("Jumping Balls");
    window.add(this);
    window.add(label);
    this.setSize(150,150);
    window.setSize(500, 300);
    window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    window.setVisible(true);
    setVisible(true);
    //timer = new Timer(100, this); //TODO
    //timer.start();

}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    setBackground(Color.BLUE);
}

BTW - here's another, very similar, code for another program, which does work, I have no idea why, it really blows up my mind.. Here's some of he code:

public class ShowClass extends JPanel implements ActionListener{

int count=0;

Graphics graphics;
JFrame window;
Timer timer;
Random random = new Random();

Color generalColor = Color.BLACK;

int wHeight = 400;
int wWidth = 550;

final int MAXSIZE = 60; //Ball's Maximum Size

//BackGround colors
int randomRed = 100;
int randomGreen = 100;
int randomBlue = 100;

//Ball colors 
int randomBallRed = 255;
int randomBallGreen = 255;
int randomBallBlue = 255;

public void init(){

    window = new JFrame("Jumping Balls");
    window.add(this);
    window.setSize(wHeight, wWidth);
    window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    window.setVisible(true);

    timer = new Timer(100, this); //TODO
    timer.start();

}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    setBackground(new Color(randomRed,randomGreen,randomBlue)); 

    for(Ball b : ManagerClass.balls){
        //b.setBallColor(new Color(randomRed,randomGreen,randomBlue)); TODO
        g.setColor(b.getBallColor());
        g.fillOval((int)b.getLocation().getX(),(int)b.getLocation().getY(),b.getHeight(),b.getWidth());
    }

}

Thank you!

YosefBro
  • 596
  • 1
  • 7
  • 17
  • How do you want to arrange the label and the panel in the window? – user253751 Feb 14 '15 at 09:03
  • Please have a look at this thread, regarding [JComponent not showing Picture background](http://stackoverflow.com/a/11372350/1057230). Hopefully this might be able to sort thingies out for you :-) Moreover, avoid, setting properties, like you doing `setBackground(...)` inside the `paintComponent(...)` method. Most Layouts respect the sizes of the component, in this case, `JPanel` is having a size of `(0, 0)`, which is why it is not showing. Try to `override` [JComponent.getPreferredSize()](http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#getPreferredSize--) method (example) – nIcE cOw Feb 14 '15 at 10:19
  • I've changed the location of setBackground() and did and override of getPreferredSize() as well, it still doesn't work.. My biggest question is why this code doesn't work and the second one, which is almost identical, does work? – YosefBro Feb 14 '15 at 12:28
  • Are you running, both the classes(ShowClass and WIndowClass) together, as a single project? Are you running the code related to Swing on [EventDisptacherThread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – nIcE cOw Feb 14 '15 at 12:41

1 Answers1

2

Your window (specifically, your window's content pane) uses a BorderLayout layout manager by default.

A BorderLayout has five positions - top, bottom, left, right and center. When you add a component to a BorderLayout, if you don't specify the position, it defaults to the center. Each position can only hold one component.

This:

window.add(this);
window.add(label);

adds this to the center position. Then it adds label to the center position - which removes this since only one component can be in the center.

You can either use a different layout manager (out of scope of this answer), or keep using BorderLayout and set the positions explicitly. An example of the latter, assuming you want the label to appear above the panel:

window.add(this, BorderLayout.CENTER); // or just window.add(this);
window.add(label, BorderLayout.NORTH);
user253751
  • 57,427
  • 7
  • 48
  • 90
  • The label is only for testing if it's visible. the label is shown in the left size of the frame, so i presume that the panel takes the center of the frame. – YosefBro Feb 14 '15 at 09:31
  • @YosefBro just because the label doesn't display anything in most of the space doesn't mean it's not taking up that space. – user253751 Feb 14 '15 at 09:43
  • The label does display a string, and it's location is on the left (WEST) side of the frame. This makes me think that the panel is taking the center of the frame, but for some reason that I dunno, it is not visible.. – YosefBro Feb 14 '15 at 10:13
  • @YosefBro the label is assigned to take up the entire frame. It just doesn't have anything to display in most of it. `BorderLayout` doesn't care how big the label "wants to be" in this case. – user253751 Feb 14 '15 at 10:24