Results 1 to 4 of 4
- 03-23-2012, 06:29 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
How do I make a grid of buttons work within a border layout??
I've been at this for hours and I'm sure its something simple I overlooked. I am making a mine sweep type game for class but It requires a header that holds a message and a grid of buttons. My intention is to use the border layout for the primary container and put the play field as the center component. Only I get an exception every time I run it. I've tried a myriad of things but I've been unable to make any difference. This is the code:
import javax.swing.*;
import java.awt.*;
public class GridAndBorder extends JFrame
{
Container con = getContentPane();
JLabel message1 = new JLabel("top");
JLabel message2 = new JLabel("bottom");
JPanel[] pane = new JPanel[20];
JButton[] button = new JButton[20];
JPanel play = new JPanel();
public GridAndBorder()
{
play.setLayout(new GridLayout(4,5));
con.setLayout(new BorderLayout());
add(message1, BorderLayout.NORTH);
populatePlay();
add(play, BorderLayout.CENTER);
add(message2, BorderLayout.SOUTH);
setSize(500,500);
}
public void populatePlay()
{
int x;
for(x=0;x<20;++x)
{
pane[x].setLayout(new GridLayout(1,1));
play.add(pane[x]);
pane[x].add(button[x]);
}
}
public static void main(String[] args)
{
GridAndBorder test = new GridAndBorder();
test.setVisible(true);
}
}
Here are the current Exceptions
Exception in thread "main" java.lang.NullPointerException
at GridAndBorder.populatePlay(GridAndBorder.java:30)
at GridAndBorder.<init>(GridAndBorder.java:19)
at GridAndBorder.main(GridAndBorder.java:39)
There is of course additional functionality required before it works as a game but I'm sure I can handle that part on my own.
- 03-23-2012, 06:42 PM #2
Re: How do I make a grid of buttons work within a border layout??
If you look at the exception, it's telling you that you have a NullPointer. That means that you are trying to do some action to an object that has not been initialized yet. You declared a JPanel[] of size 20, but you never initialized any of the JPanels, so pane[x] is null. You are going to have the same problem with your array of JButtons. You have to initialize each JButton, or else button[x] will be null as well.
You can either:
-initialize each of the JPanels and JButtons using a for loop in your constructor(since your JPanel and JButton arrays are the same size, you can do this in one loop)
-initialize each of the JPanels and JButtons in the beginning of your for loop in your populatePlay() methodLast edited by sehudson; 03-23-2012 at 06:46 PM.
- 03-23-2012, 07:28 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: How do I make a grid of buttons work within a border layout??
Thank you, I guess I was thinking that instantiating the array would instantiate the elements as well. That will be a valuable lesson for the future. It now works perfectly.
- 03-23-2012, 07:43 PM #4
Re: How do I make a grid of buttons work within a border layout??
Well, what I said wasn't completely true. If you have an array of primitives, then they are automatically initialized. For instance, if you had:
int[] myArray = new int[10]
Each element (int) in the array is automatically initialized to 0
If you had an array of boolean values, each element in the array is automatically initialized to false.
Since you had an array of JPanels, and an array of JButtons, you have to initialize each element in the array.Last edited by sehudson; 03-23-2012 at 07:45 PM.
Similar Threads
-
Grid Layout change layout alignment of control s
By rellicott in forum SWT / JFaceReplies: 1Last Post: 02-13-2012, 05:11 PM -
How do I make my buttons work in the GUI of this code?
By Interframe in forum AWT / SwingReplies: 3Last Post: 06-18-2011, 11:12 AM -
Grid layout frames GUI
By fritz1474 in forum AWT / SwingReplies: 1Last Post: 10-15-2008, 02:04 AM -
Border Layout
By mark-mlt in forum Java AppletsReplies: 5Last Post: 05-12-2008, 09:48 AM -
Help with Grid Layout
By coco in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:03 PM
Bookmarks