i think theres something wrong with my computer, im trying to create a GUI for my program but it wont display my JLabels (which contain images)
i downloaded a program with a GUI i made at the computer lab in my university which works there, but on my laptop it doesnt display any JLabels either. Can someone run these codes for me to check if the error is in my code or with my computer?

Heres the code with the main method and JFrame:

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

class TestHomeController
{
    public static void main(String[] args) 
    {
		// Creates the JFrame
        JFrame myWindow = new JFrame("Home Controller");
               
        // Set the default close operation of the JFrame object to dispose
       	myWindow.setDefaultCloseOperation(3);
        
  		// Instance the standalone class containing the components
  		HomeController Remote1 = new HomeController();		
 
      	// Add intermediate level container to top level container
        myWindow.add(Remote1,BorderLayout.CENTER);     
           
		// Displays the window.
        myWindow.pack();
        myWindow.setVisible(true);
	    
    }// end of main method
    
}// end of class

and heres the code with the GUI components:

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

class HomeController extends JPanel
{
	ImageIcon blank = new ImageIcon("blank.jpg");
	
	JLabel Appliance1 = new JLabel(blank);
	JLabel Appliance2 = new JLabel(blank);
	JLabel Appliance3 = new JLabel(blank);
	JLabel Appliance4 = new JLabel(blank);
	JLabel Appliance5 = new JLabel(blank);
	JLabel Appliance6 = new JLabel(blank);
	JLabel Appliance7 = new JLabel(blank);
	JLabel Appliance8 = new JLabel(blank);
	JLabel Appliance9 = new JLabel(blank);
	
	HomeController()
	{
		this.setLayout(new BorderLayout());
		
		JPanel Panel1 = new JPanel();
		JPanel Panel2 = new JPanel();
		JPanel Panel3 = new JPanel();
		
		Appliance1.setOpaque(true);
		Panel1.add(Appliance1,BorderLayout.WEST);
		Appliance2.setOpaque(true);
		Panel1.add(Appliance2,BorderLayout.CENTER);
		Appliance3.setOpaque(true);
		Panel1.add(Appliance3,BorderLayout.EAST);
		
		Appliance4.setOpaque(true);
		Panel2.add(Appliance4,BorderLayout.WEST);
		Appliance5.setOpaque(true);
		Panel2.add(Appliance5,BorderLayout.CENTER);
		Appliance6.setOpaque(true);
		Panel2.add(Appliance6,BorderLayout.EAST);
		
		Appliance7.setOpaque(true);
		Panel3.add(Appliance7,BorderLayout.WEST);
		Appliance8.setOpaque(true);
		Panel3.add(Appliance8,BorderLayout.CENTER);
		Appliance9.setOpaque(true);
		Panel3.add(Appliance9,BorderLayout.EAST);
	}	
}

NOTE: when run it should display a three by three grid containing blank images.(you will need to create an image called blank for it to work)

Recommended Answers

All 4 Replies

Ive solved the above problem but now i have another one occuring.
Here is the code containing the Driver class

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

class TestHomeController
{
    public static void main(String[] args) 
    {
		// Creates the JFrame
        JFrame myWindow = new JFrame("Home Controller");
               
        // Set the default close operation of the JFrame object to dispose
       	myWindow.setDefaultCloseOperation(3);
        
  		// Instance the standalone class containing the components
  		HomeController Remote1 = new HomeController();		
 
      	// Add intermediate level container to top level container
        myWindow.add(Remote1,BorderLayout.CENTER);     
           
		// Displays the window.
        myWindow.pack();
        myWindow.setVisible(true);
	    
    }// end of main method
    
}// end of class

And here is the code containing the components:

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

class HomeController extends JPanel
{
	//Calling the Blank icon		
	ImageIcon blank = new ImageIcon("blank.jpeg");
	
	//Creation of the Appliance JLabels
	JLabel Appliance1 = new JLabel(blank);
	JLabel Appliance2 = new JLabel(blank);
	JLabel Appliance3 = new JLabel(blank);
	JLabel Appliance4 = new JLabel(blank);
	JLabel Appliance5 = new JLabel(blank);
	JLabel Appliance6 = new JLabel(blank);
	JLabel Appliance7 = new JLabel(blank);
	JLabel Appliance8 = new JLabel(blank);
	JLabel Appliance9 = new JLabel(blank);
	
	
	//Creation of the JButtons
	JButton add1 = new JButton("ADD APPLIANCE");
	JButton clear1 = new JButton("CLEAR SCREEN");
	JButton load1 = new JButton("LOAD FILE");
	JButton save1 = new JButton("SAVE FILE");	
	
	public HomeController()
	{	
		//Setting the Home Controller class' Layout	
		this.setLayout(new BorderLayout());
		
		//Creation of the Panel1 JPanel
		JPanel Panel1 = new JPanel();
		this.add(Panel1,BorderLayout.NORTH);
		
		//Creation of the Panel2 JPanel
		JPanel Panel2 = new JPanel();
		this.add(Panel2,BorderLayout.CENTER);
		
		//Creation of the Panel3 JPanel
		JPanel Panel3 = new JPanel();
		this.add(Panel3,BorderLayout.SOUTH);
		
		//Creation of the Panel4 JPanel
		JPanel Panel4 = new JPanel();
		Panel3.add(Panel4,BorderLayout.NORTH);
		
		//Creation of the Panel5 JPanel
		JPanel Panel5 = new JPanel();
		Panel3.add(Panel5,BorderLayout.WEST);
		
		//Creation of the Panel6 JPanel
		JPanel Panel6 = new JPanel();
		Panel3.add(Panel6,BorderLayout.EAST);
				
		//Ordering of the Appliance1 JLabel	
		Appliance1.setOpaque(true);
		Panel1.add(Appliance1,BorderLayout.WEST);
		
		//Ordering of the Appliance2 JLabel	
		Appliance2.setOpaque(true);
		Panel1.add(Appliance2,BorderLayout.CENTER);
		
		//Ordering of the Appliance3 JLabel	
		Appliance3.setOpaque(true);
		Panel1.add(Appliance3,BorderLayout.EAST);
		
		//Ordering of the Appliance4 JLabel	
		Appliance4.setOpaque(true);
		Panel2.add(Appliance4,BorderLayout.WEST);
		
		//Ordering of the Appliance5 JLabel	
		Appliance5.setOpaque(true);
		Panel2.add(Appliance5,BorderLayout.CENTER);
		
		//Ordering of the Appliance6 JLabel	
		Appliance6.setOpaque(true);
		Panel2.add(Appliance6,BorderLayout.EAST);
		
		//Ordering of the Appliance7 JLabel	
		Appliance3.setOpaque(true);
		Panel4.add(Appliance7,BorderLayout.WEST);
		
		//Ordering of the Appliance8 JLabel	
		Appliance3.setOpaque(true);
		Panel4.add(Appliance8,BorderLayout.CENTER);
		
		//Ordering of the Appliance9 JLabel	
		Appliance3.setOpaque(true);
		Panel4.add(Appliance9,BorderLayout.EAST);
		  		
		//Ordering of the JButtons
		Panel5.add(add1,BorderLayout.WEST);
		Panel5.add(clear1,BorderLayout.EAST);
		Panel6.add(load1,BorderLayout.WEST);
		Panel6.add(save1,BorderLayout.EAST);
	}
}

The above Code compiles but when i run it the 9 JLabels marked appliance dont show. It should show up as a 3X3 grid containing the IconImage called blank. can someone help me fix this

If you want things to show up in a grid then use GridLayout. I'm not saying its impossible to do otherwise but why make your job harder than it needs to be.

If you want things to show up in a grid then use GridLayout. I'm not saying its impossible to do otherwise but why make your job harder than it needs to be.

i could but after i have the images done i have to add four buttons to the bottom, if i used a grid layout i cant have a three by three grid and then four buttons on the line below.

ive done more work since i posted this and i have managed to get the images to show. my only problem now is the bottom three images show up on the same line as the buttons

edit: sorry, I thought you were adding multiple things to W/N, but you were adding them to different panels. In any case, post the updated code you have and I'll take a look at it and run it on my computer if necessary.

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.