Jump to content

JFrame.remove does not work

- - - - -

  • Please log in to reply
5 replies to this topic

#1
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
Code:

import java.awt.*;

import javax.swing.*;


@SuppressWarnings("serial")

public class GUI extends JFrame{

	private JPanel panel1 = new JPanel();

	public GUI() {

		ImageIcon bootingUp = new ImageIcon("bootscreen.png");

		JLabel bootLabel = new JLabel(bootingUp);

		panel1.add(bootLabel);

		this.add(panel1);

		Toolkit tools = Toolkit.getDefaultToolkit();

		Dimension d = tools.getScreenSize();

		this.setSize(d.width, d.height);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.setTitle("Game");

		this.setResizable(false);

		this.setBackground(Color.BLUE);

		this.setForeground(Color.BLUE);

		this.setVisible(true);

		try {

			Thread.sleep(5000);

		} catch (InterruptedException e) {}

		this.remove(bootLabel);

	}

}


When the this.remove is called, nothing happens!

Oh, and the main method:


public class Begin {


	/**

	 * @param args

	 */

	public static void main(String[] args) {

		new GUI();

	}


}


The this.setBackground and this.setForeground do nothing also.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
*Edited...

The post after mine looks like the correct solution.
You've added bootLabel to a JPanel. You need to remove your bootLabel from the JPanel. You're currently trying to remove it from the JFrame.

Edited by lethalwire, 22 January 2012 - 01:53 PM.


#3
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
What container is bootLabel in? Where did you add it?

#4
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

lethalwire said:

*Edited...

The post after mine looks like the correct solution.
You've added bootLabel to a JPanel. You need to remove your bootLabel from the JPanel. You're currently trying to remove it from the JFrame.

OH! Okay I'll try to change that.

Results:
import java.awt.*;

import javax.swing.*;


@SuppressWarnings("serial")

public class GUI extends JFrame{

	private JPanel panel1 = new JPanel();

	public GUI() {

		ImageIcon bootingUp = new ImageIcon("bootscreen.png");

		JLabel bootLabel = new JLabel(bootingUp);

		panel1.add(bootLabel);

		this.add(panel1);

		Toolkit tools = Toolkit.getDefaultToolkit();

		Dimension d = tools.getScreenSize();

		this.setSize(d.width, d.height);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.setTitle("Gorthar");

		this.setResizable(false);

		this.setBackground(Color.BLUE);

		this.setForeground(Color.BLUE);

		this.setVisible(true);

		try {

			Thread.sleep(5000);

		} catch (InterruptedException e) {}

		panel1.remove(bootLabel);

	}

}

did NOT work. ???
And the background still does not show.

EDIT: I found out that it always keeps showing the picture until I put something on top of it. Then the new picture just covers it up. ???

#5
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
After you remove the component you need to tell the system you are done changing the GUI and that it needs to do something to show the new contents. Try calling repaint

#6
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

Norm said:

After you remove the component you need to tell the system you are done changing the GUI and that it needs to do something to show the new contents. Try calling repaint

YAY YAY YAY YAY YAY YAY YAY YAY!!!!!!!

Sorry. It worked!!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users