Hi all, I have a slight problem with a java applet of mine. I play a video game that tracks realm and world ranks and I'm making this applet to put on my site to display current standings.

My main problem is currently in the testing of this applet. Once I get everything the way it's working, I'll start to set variables to ranks instead of just using predefined numbers; however, my labels are hidden from my background. Is there another way I can set the background and have the labels show in front of the background? I recall in a previous java class this was possible but I don't remember how.

I'll paste my main applet code below to show you the problem:

*Ignore the URL reader. This is where I get my data from*

import java.util.*;
import javax.swing.JApplet;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.BoxLayout;

public class Ranks extends JApplet{
	
	//Declare varibles
	private String address;
	private JLabel guildLogo, worldRankLabel, realmRankLabel, realmRankPos, worldRankPos;
	
	public void init(){
		setLayout(new FlowLayout());
		//contentsY.setLayout(new BoxLayout(contentsY, BoxLayout.Y_AXIS));
		
		JLabel blank1 = new JLabel("    ");
      guildLogo = new JLabel(new ImageIcon("FD_logo.png"));
      JLabel blank2 = new JLabel("    ");
      realmRankLabel = new JLabel("Realm Rank:");
		JLabel blank3 = new JLabel("    ");
		realmRankPos= new JLabel("4");
		worldRankLabel = new JLabel("World Rank:");
		JLabel blank4 = new JLabel("    ");
		worldRankPos= new JLabel("3000");
		
		//Set label fonts and text positions
		
		
      add(blank1);
		add(guildLogo);
		add(blank2);
		add(realmRankLabel);
		add(blank3);
		add(realmRankPos);
		add(worldRankLabel);
		add(blank4);
		add(worldRankPos);
		
		
		setSize(new Dimension(170, 150));
		
	}
	
	public void Run() throws IOException{
	  URL url = new URL("http://www.wowprogress.com/guild/us/elune/From+Dust/json_rank");
 	  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

     String inputLine;
	  String data = null;

		// Set data = to the only line in the input stream from the URL
		data = (inputLine = in.readLine());
		
	}
	
	public void paint(Graphics g){
		getContentPane().setBackground(Color.WHITE);
	}

}

Thank you in advance for any help!

You don't need to set the background in paint(). You can set it in the constructor once and it will persist.

You specifically do not want to override the paint() method like that because you are preventing the labels from being rendered at all. In Swing components, you should override paintComponent() if you need to provide custom rendering.

Just remove the paint method declaration altogether and move your background call up to where you are adding the other components and see if that gets you where you want to be.

Thank you that did it. I actually overrode the paint function AFTER I tried setBackground(Color.white) in the constructor - and it failed. But didn't think to ever try getContentPane().setBackground(Color.white) in the constructor.

Thanks for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.