Results 1 to 8 of 8
- 03-11-2011, 10:49 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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?
- 03-12-2011, 01:06 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-12-2011, 03:31 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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!
-
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?
- 03-12-2011, 05:52 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thanks for the tip on setVisible. I am not using static variables.
More code:
I have omitted snippets of code that are unrelated, but this is most of the 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'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!
-
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!
- 03-13-2011, 01:40 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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)
And here is the other class.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); } }
The problem is that only the one TTTSquare appears in the upper-left corner. I have checked that the others are there with MouseListeners andJava 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; } }
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!
-
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
-
JPanel not showing up in JFrame
By rlindsey in forum AWT / SwingReplies: 2Last Post: 06-25-2010, 07:21 AM -
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Showing JList in a JPanel
By nico.hvi in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 02:26 PM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks