6 Replies - 616 Views - Last Post: 06 December 2012 - 11:56 AM Rate Topic: -----

#1 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

occassional JLabel not showing

Posted 06 December 2012 - 09:49 AM

Hello fellow coder friends,

ok, so I am using JLabel to have 75 numbers show up on the users screen, the thing is though, is that on occassion 1 of the JLables doesn't show up?? It doesnt seem to be any certain one as once it was the number 8, another time it was 61, etc...

I am building the JLabels as so:
public JLabel GB[] = new JLabel[75]; // before the constructor

//in the constructor
                for(int i = 0;i<GB.length;i++) {
                    
                    y = y + 30;
                    
                    if(i==15) { x = x + 30; y = 218; }
                    if(i==30) { x = x + 30; y = 218; }
                    if(i==45) { x = x + 30; y = 218; }
                    if(i==60) { x = x + 30; y = 218; }
                    
                    GB[i] = new JLabel(Integer.toString(i+1), JLabel.CENTER);
                    GB[i].setForeground(Color.LIGHT_GRAY);
                    GB[i].setFont(new Font("Verdana", Font.BOLD, 15));
                    GB[i].setBounds(x,y,28,28);                    
                    
                }


//in the method when I want it to appear
                    for(int i = 0;i<GB.length;i++) {

                        Mainwindow.getContentPane().add(GB[i]);   
                        GB[i].setVisible(true);
                    }


the wierd thing is that most of the time they all show up, but just occassionally one doesn't?
is there something im not doing right?
is there a way to check to be sure they all load properly?


Thank you for any help..

Is This A Good Question/Topic? 0
  • +

Replies To: occassional JLabel not showing

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: occassional JLabel not showing

Posted 06 December 2012 - 10:04 AM

We'll need mode code
What is MainWindow ? When is it filled with the JLabel ? What else is in it ?

And please call getContentPane() only once before the loop, not 75 times in a row within a loop
Was This Post Helpful? 0
  • +
  • -

#3 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: occassional JLabel not showing

Posted 06 December 2012 - 10:38 AM

Hi Pbl,

here is the complete file so you can see what I am (trying) to do..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



/*
 * The Client with its GUI
 */
public class ClientGUI extends JFrame implements ActionListener {
    
        private static final long serialVersionUID = 1L;

        public JFrame MainWindow = new JFrame();

        public JFrame LogInWindow = new JFrame();
        public JTextField UserName = new JTextField();
        public JPasswordField PassWord = new JPasswordField();
        public JButton B_LOGIN = new JButton();
        private JPanel panel = new JPanel();

        private JButton testB = new JButton();

        private Image im = Toolkit.getDefaultToolkit().getImage("C:\\OtherStuff\\BingoBack.png");
        private ImageIcon BG = new ImageIcon(im);
        public ImageIcon Img, Img2, Img12;
        public JLabel Lab1, Lab2, Lab3, Lab4, Lab5;
        public JLabel GB1,GB2,GB3,GB4,GB5,GB6,GB7,GB8,GB9,GB10;
        public String UN,PW;

	private boolean connected;
	// the Client object
	private Client client;
	// the default port number
	private int defaultPort;
	private String defaultHost;
        public JLabel GB[] = new JLabel[75];

	// Constructor connection receiving a socket number
	ClientGUI() {

		super("Chat Client");
		defaultPort = 1500;
		defaultHost = "localhost";
                
                //Build images
                Lab1 = new JLabel(new ImageIcon("C:\\OtherStuff\\BingoLogin.png"));
                Lab1.setBounds(0,0,1000,700);

                Lab2 = new JLabel(new ImageIcon("C:\\OtherStuff\\BingoSetUp.png"));
                Lab2.setBounds(0,0,1000,700);

                Lab3 = new JLabel(new ImageIcon("C:\\OtherStuff\\BingoLobby.png"));
                Lab3.setBounds(0,0,1000,700);

                Lab4 = new JLabel(new ImageIcon("C:\\OtherStuff\\BingoWait.png"));
                Lab4.setBounds(0,0,1000,700); 
                
                Lab5 = new JLabel(new ImageIcon("C:\\OtherStuff\\BingoBack.png"));
                Lab5.setBounds(0,0,1000,700);
                
                int y = 188;
                int x = 14;
                
                for(int i = 0;i<GB.length;i++) {
                    
                    y = y + 30;
                    
                    if(i==15) { x = x + 30; y = 218; }
                    if(i==30) { x = x + 30; y = 218; }
                    if(i==45) { x = x + 30; y = 218; }
                    if(i==60) { x = x + 30; y = 218; }
                    
                    GB[i] = new JLabel(Integer.toString(i+1), JLabel.CENTER);
                    GB[i].setForeground(Color.LIGHT_GRAY);
                    GB[i].setFont(new Font("Verdana", Font.BOLD, 15));
                    GB[i].setBounds(x,y,28,28);                    
                    
                }
        
        
        
        		//Window
                Mainwindow.setTitle("Bob's Bingo");
		Mainwindow.setSize(1000,700);
		Mainwindow.setLocation(150,100);
		Mainwindow.setResizable(false);
 
                //Login UserName
                UserName.setText("");
		Mainwindow.getContentPane().add(UserName);
		UserName.setBounds(425,255,150,20); 
                
                //Login PassWord
                PassWord.setText("");
		Mainwindow.getContentPane().add(PassWord);
		PassWord.setBounds(425,285,150,20);
                
                B_LOGIN.setBackground(new java.awt.Color(0,0,255));
		B_LOGIN.setForeground(new java.awt.Color(255,255,255));
		B_LOGIN.setText("LOGIN");
		B_LOGIN.setToolTipText("");
                B_LOGIN.addActionListener(this);
		Mainwindow.getContentPane().add(B_LOGIN);
		B_LOGIN.setBounds(450,315,100,25);
                
                //BackGround
                Mainwindow.getContentPane().add(Lab1);
 
                Mainwindow.setVisible(true);
                Mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             

	}


	// called by the GUI is the connection failed
	// we reset our buttons, label, textfield
	void connectionFailed() {
            
		connected = false;
                System.out.println("Failed!!\n");
	}
		
	/*
	* Button or JTextField clicked
	*/
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		
		if(o == B_LOGIN) {
			// ok it is a connection request
			String username = UserName.getText().trim();
			// empty username ignore it
			if(username.length() == 0) { return; }
                        
			
			String password = PassWord.getText().trim();
			// empty username ignore it
			if(password.length() == 0) { return; }                        
                        
			// empty serverAddress ignore it
			String server = defaultHost;
			if(server.length() == 0)
				return;
			// empty or invalid port numer, ignore it
			Integer port = defaultPort;


			// try creating a new Client with GUI
			client = new Client(server, port, username, this);
			// test if we can start the Client
			if(!client.start()) { return; }
                        
			connected = true;
 
                        UserName.setVisible(false);
                        PassWord.setVisible(false);
                        B_LOGIN.setVisible(false);
                        Lab1.setVisible(false);  
                        
                        LOC_Lobby();
			
		}

	}
        
        public void LOC_Lobby() {
            
                    Lab2.setVisible(false);
                    Lab3.setVisible(false);
                    Lab4.setVisible(false);
                    Lab5.setVisible(false);

                    testB.setBackground(new java.awt.Color(0,0,255));
                    testB.setForeground(new java.awt.Color(255,255,255));
                    testB.setText("Test");
                    testB.setToolTipText("");
                    Mainwindow.getContentPane().add(testB);
                    testB.setBounds(450,315,100,25); 



                    Mainwindow.getContentPane().add(Lab3);
                    Lab3.setVisible(true);

        } 
        
        public void LOC_GameSetUp() {

                    Lab2.setVisible(false);
                    Lab3.setVisible(false);
                    Lab4.setVisible(false);
                    Lab5.setVisible(false);
                    Mainwindow.getContentPane().add(Lab2);
                    Lab2.setVisible(true);

        }

        public void LOC_Wait() {

                    Lab2.setVisible(false);
                    Lab3.setVisible(false);
                    Lab4.setVisible(false);
                    Lab5.setVisible(false);
                    Mainwindow.getContentPane().add(Lab4);
                    Lab4.setVisible(true);

        }
        
        public void LOC_Play() {

                    Lab2.setVisible(false);
                    Lab3.setVisible(false);
                    Lab4.setVisible(false);
                    Lab5.setVisible(false);                                    
                    
                    for(int i = 0;i<GB.length;i++) {

                        Mainwindow.getContentPane().add(GB[i]);   
                        GB[i].setVisible(true);
                    } 
                    
                    Mainwindow.getContentPane().add(Lab5);
                    Lab5.setVisible(true); 

        }        
        
        public void buildGame(String gs, int gm, String message) {
            

            
        }

	// to start the whole thing the server
	public static void main(String[] args) {
		new ClientGUI();
	}

}



as for the getContentPane, I apologize im not really sure how I would do that outside of a loop? I was under the impression that you call the getContentPane each time you want to add the JLabel to the window?
In the loop I am setting the JLabel to visible and adding it to the "mainwindow" so my thought process was that it was needed to add each JLabel?

Once again thank you for the assistance

Bob
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: occassional JLabel not showing

Posted 06 December 2012 - 10:48 AM

Does not tell us what MainWindow is
a JFrame mos probably if you call getContentPane() on it

so you can do:

Container c = Mainwindow.getContentPane();
the big loop
    c.add(component);


actually since JRE 1.5 you don't even need to getContentPane() to add to a frame.
mainwindow.add(label[i]) will do the job

Now, as we don't know what is the layout of Mainwindow... not much we can say, I I really don't understand why you don't put your JLabel in a GridLayout(5,15) yiou would have to worry about all the setBounds() you do to them

JComponent to a cContainer at run time is never a good idea
Add all your component at construction time (make them setVisible(false) if required) and toggle their visibility later on
Was This Post Helpful? 0
  • +
  • -

#5 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: occassional JLabel not showing

Posted 06 December 2012 - 11:04 AM

Hi Pbl,

Yes, MainWindow is the JFrame:
public JFrame MainWindow = new JFrame(); 


I chose not to use Gridlayout because when I was designing how I was gonna lay this all out I went with 1 image (BingoBack) and layed it out across the entire window size, the image has most of the graphics and framework to the look of it, then I used the setbounds because everything needs to be at certain pixels to look right. My thought process on it was that it would take a little more work to setup, but i didn't have to worry about having table rows and cells everywhere and aligning things just right.

I moved the adding of componants up to the constructor as you recommended and took out the getcontentpane from the loop. Thank you for that bit of info.

one more question while I am here if I may, I was torn between using text as I have or small images with the numbers on them, is there a big difference in resources and possibly slowing down the program if I went with images instead of text?

Thanks once again,

Bob
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: occassional JLabel not showing

Posted 06 December 2012 - 11:52 AM

Unless you have a 20 years old IBM PC AT 360 with 32MB of RAM I don't think it will do a difference :)

P.S.
still think the GridLayout is the best solution to your problem :)
Was This Post Helpful? 1
  • +
  • -

#7 Randor   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: occassional JLabel not showing

Posted 06 December 2012 - 11:56 AM

ok, great! Once again thank you very much.

I am going to pull up a tutorial on gridlayout and take a look, see if I can come up with a plan to try that out as I trust you would know better what should be used.

Pbl, once again you save the day..

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1