Results 1 to 8 of 8
  1. #1
    TheGoodDoctor is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Only one extended JPanel showing up

    I have a GUI runner class that manages several different windows, one of
    which is a JFrame with a JPanel set up as a Grid Layout Manager. My problem
    is that when I try to fill the grid with an extended JPanel class, only the first JPanel shows up.
    I have confirmed that the others are there but they do not appear.
    Everything works fine when I use JButtons or generic JPanels.
    I will post code if necessary, but any ideas as to what might cause this?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by TheGoodDoctor View Post
    I will post code if necessary, but any ideas as to what might cause this?
    Yes, it's necessary. otherwise how we know that what you've really done.

  3. #3
    TheGoodDoctor is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Here is the extended JPanel Class constructor (it's for a tic-tac-toe program)



    public TTTSquare (int xPos, int yPos, View theView)
    {
    super ();
    x = xPos;
    y = yPos;
    beenClicked = false;
    toDisplay = new JLabel ("");
    myView = theView;
    this.addMouseListener (this);
    this.add(toDisplay);
    this.setBorder((BorderFactory.createRaisedBevelBor der ()));
    this.setVisible (true);
    }

    And this is the relevant method, which should place a series of the above objects into a Grid Layout JPanel. ('size' is the dimension of the square grid), and the class this method is in is a manager for several different windows corresponding to different parts of the game.

    public void initializeBoard ()
    {
    isGame = true;
    model = new Board (size);
    model.startGame ();
    gameWindow = new JFrame ("Tic Tac Toe");
    gameWindow.setSize (size * 100, size * 100);
    gridPanel = new JPanel ();
    gridPanel.setLayout(new GridLayout(size,size));
    gridPanel.setVisible (true);
    for (int y = 0; y < size; y++)
    {
    for (int x = 0; x < size; x++)
    {
    TTTSquare square = new TTTSquare (x, y, this);
    gridPanel.add (square);
    square.setVisible (true);

    }

    }
    gameWindow.setContentPane (gridPanel);
    gameWindow.setVisible (true);
    gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
    }

    Sorry if this is a lot of code, but I hope it helps!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Suggestions
    • Get rid of all the setVisible(true) wasted code. You only need one setVisible(true) that is called on the JFrame after adding all components and calling pack() on the JFrame.
    • Use code tags when posting code here so that it is readable and we can read it. The link in my signature below should help.
    • You will likely need to post more code for us to be able to understand the problem.
    • You're not using any static variables are you?

  5. #5
    TheGoodDoctor is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the tip on setVisible. I am not using static variables.

    More code:

    Java Code:
    //This class runs the various GUI elements
    public class View implements MouseListener
    {
    	private JPanel gridPanel;
    	private int size;
    	private Board model;
    	private boolean isGame;
    	private JFrame gameWindow, menu;
    	JButton startButton;
    	
    	
    	public View (int dimensions)
    	{
    		size = dimensions;
    		menuScreen ();
    		isGame = false;
    	}
    	
            //The initial menu screen
    	public void menuScreen ()
    	{
    		menu = new JFrame ("T3: ARE YOU READY TO RUMBLE?");
    		menu.setSize (325, 150);
    		startButton = new JButton ("PLAY");
    		startButton.addMouseListener (this);
    		menu.add (startButton);
    		menu.setVisible (true);
    		menu.validate ();
    		menu.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    	}
    	
            //The actual Tic-Tac-Toe game board
    	public void initializeBoard ()
    	{
    		isGame = true;
    		gameWindow = new JFrame ("Tic Tac Toe");
    		model = new Board (size);
    		model.startGame ();
    		gameWindow.setSize (size * 100, size * 100);
    		gridPanel = new JPanel ();
    		gridPanel.setLayout(new GridLayout(size,size));
    		for (int y = 0; y < size; y++)
    		{
    			for (int x = 0; x < size; x++)
    			{
    				TTTSquare square = new TTTSquare (x, y, this);
    				gridPanel.add (square);
    
    			}
    			
    		}
    		gameWindow.setContentPane (gridPanel);
    		gameWindow.setVisible (true);
    	       gameWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);	
    	}
    	
    
            //Checks to see if a new move has been made and then
            //repaints the parameter TTTSquare with the appropriate character
    	public void makeMove(TTTSquare square, MouseEvent e, int x, int y)
    	{
    			boolean moved = model.move(x, y);
    		    if (moved)
    		    {
    		    	square.setDisplayText (model.getLastMove ());
    		    	gameWindow.repaint ();
    		   	}
    	}
    
    
            //Starts the game by calling initializeBoard () once menuScreen ()'s play
            //button is clicked
    	public void mouseClicked(MouseEvent e) 
    	{
    		if (e.getSource().equals (startButton));
    		{
    			 isGame = true;
    			 menu.dispose ();
    			 initializeBoard ();
    		}
    		
    	}
    }
    
    //The extended JPanel class in its entirety
    public class TTTSquare extends JPanel implements MouseListener
    {
    	private int x, y;
    	private boolean beenClicked;
    	private JLabel toDisplay;
    	private View myView;
    	
    	public TTTSquare (int xPos, int yPos, View theView)
    	{
    		super ();
    		x = xPos;
    		y = yPos;
    		beenClicked = false;
    		toDisplay = new JLabel ("");
    		myView = theView;
    		this.addMouseListener (this);
    		this.add(toDisplay);
    		this.setBorder((BorderFactory.createRaisedBevelBorder ()));
    	}
    
            //If this square hasn't been moved on, makes the appropriate move
    	public void mouseClicked(MouseEvent e) 
    	{
    		if (beenClicked)
    		{
    			return;
    		}
    		beenClicked = true;
    		myView.makeMove(this, e, x, y);
    	}
    	
    	public void setDisplayText (String display)
    	{
    		toDisplay.setText (display);
    		repaint ();
    	}
    
            public boolean hasBeenClicked ()
    	{
    		return beenClicked;
    	}
    	
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent(g);
    	}
    
    //The model class for the Tic-Tac-Toe board
    public Board (int size)
    	{
    		isXTurn = true;
    		isGame = false;
    		dimensions = size;
    		board = new char [size][size]; 
    		lastMove = "";
    	}
    	
            //updates the model's representation of the game board
    	public boolean move (int x, int y)
    	{
    		if (isGame)
    		{
    			if (board[x][y] == '\u0000')
    			{
    				if (isXTurn)
    				{
    					board [x][y] = 'X';
    					lastMove = "X";
    					isXTurn = false;
    				}
    				else
    				{
    					board [x][y] = 'O';
    					lastMove = "O";
    					isXTurn = true;
    					
    					
    				}
    				return true;
    			}
    			
    		}
    		return false;
    	}
    I have omitted snippets of code that are unrelated, but this is most of the code.

    I'm sure there are various other problems in this code (this is my first time using Swing) but I would only like help on this one topic as this is an assignment that will be graded and I want to avoid ethical issues. (My instructor has approved asking for help on this one problem).

    Again, sorry if this is too much code, but I'd rather post too much than too little. Thanks for your time!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your problem isn't evident to me based on the code you have posted. You may wish to create a Short, Self Contained, Correct (Compilable), Example or SSCCE. This is a small program that compiles, runs, demonstrates your problem, and contains no extraneous code. Please read the link, and have a go at it.

    Much luck!

  7. #7
    TheGoodDoctor is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    In trying to put together the SSCCE I managed to find a way to fix the problem, although I can't say I am sure why it fixed it. It seems to stem from the getX () and getY () methods in TTTSquare. I'm not sure if I've
    done this correctly, but here is the SSCCE.

    First Class (just create an object of it with an instance variable of 3 to start it)
    Java Code:
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class View
    {
        private JPanel gridPanel;
        private int size;
        //private Board model;
        private boolean isGame;
        private JFrame gameWindow, menu;
        JButton startButton;
        
        
        public View (int dimensions)
        {
            size = dimensions;
            initializeBoard ();
            isGame = false;
        }
        
        public void initializeBoard ()
        {
            isGame = true;
            gameWindow = new JFrame ("Tic Tac Toe");
            //model = new Board (size);
            //model.startGame ();
            gameWindow.setSize (size * 100, size * 100);
            gridPanel = new JPanel ();
            gridPanel.setLayout(new GridLayout(size,size));
            for (int y = 0; y < size; y++)
            {
                for (int x = 0; x < size; x++)
                {
                    TTTSquare square = new TTTSquare (x, y, this);
                    gridPanel.add (square);
    
                }
                
            }
            gameWindow.setContentPane (gridPanel);
            gameWindow.setVisible (true);
            gameWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
        }
        
    }
    And here is the other class.
    Java Code:
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    
    public class TTTSquare extends JPanel
    {
    	private int x, y;
    	private boolean beenClicked;
    	private JLabel toDisplay;
    	private View myView;
    	
    	public TTTSquare (int xPos, int yPos, View theView)
    	{
    		super ();
    		x = xPos;
    		y = yPos;
    		beenClicked = false;
    		toDisplay = new JLabel ("");
    		myView = theView;
    		this.add(toDisplay);
    		this.setBorder((BorderFactory.createRaisedBevelBorder ()));
    	}
    	
    	public int getX ()
    	{
    		return x;
    	}
    	
    	public int getY ()
    	{
    		return y;
    	}
    
    }
    The problem is that only the one TTTSquare appears in the upper-left corner. I have checked that the others are there with MouseListeners and
    print statements and they do exist.

    Even though I have figured this out, it would be interesting to know what went wrong, and this has been a valuable learning experience for me - Thanks!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yeah, now you're finding out the dangers of extending other classes, something you want to avoid if not necessary. JComponents use getX and getY to set the location of themselves, so you don't want to name your methods the same else you will override the original method and mess up the positioning of your components. Rather, the solution is to change your method name to something else.
    Last edited by Fubarable; 03-13-2011 at 03:28 AM.

Similar Threads

  1. JPanel not showing up in JFrame
    By rlindsey in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2010, 07:21 AM
  2. Replies: 6
    Last Post: 06-16-2010, 06:03 AM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  4. Showing JList in a JPanel
    By nico.hvi in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 02:26 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •