Results 1 to 6 of 6
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default multi array of JButtons

    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Screen implements ActionListener 
    {
    	public JButton[][] b=new JButton[200][200];
    	
    	Screen()
    	{
    		JFrame j = new JFrame("A Star Algorithm");
    		j.setSize(700,500);
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		j.setLayout(new GridLayout(20,20));
    		j.setResizable(true);
    		
    		//create Buttons
    		for(int row=0;row<200;row++)
    		{
    			for(int col=0;col<200;col++)
    			{
    				b[row][col]=new JButton();
    				b[row][col].setBackground(Color.YELLOW);
    				b[row][col].addActionListener(this);
    				j.add(b[row][col]);
    			}
    		}
    
    		j.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		
    	}
    }
    this is where the issue is:
    Java Code:
    //create Buttons
    		for(int row=0;row<200;row++)
    		{
    			for(int col=0;col<200;col++)
    			{
    				b[row][col]=new JButton();
    				b[row][col].setBackground(Color.YELLOW);
    				b[row][col].addActionListener(this);
    				j.add(b[row][col]);
    			}
    		}
    I am trying to create the A* Algorithm and I REALLY need a 2D array to handle this.
    This is the error:
    Java Code:
    at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
    	at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
    	at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
    	at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
    	at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
    I am stumped!!!!

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    168
    Rep Power
    8

    Default Re: multi array of JButtons

    I ran this on my computer with no problems. Can we see the class where you have the main method to actually start the program?

  3. #3
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: multi array of JButtons

    Java Code:
    public class AStar extends Screen
    {
    	
    
    	public static void main(String[] args) 
    	{
    		new Screen();
    		AStar a = new AStar();
    	}
    
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: multi array of JButtons

    How do you want to cram 200x200 == 40000 buttons in a grid that can only store 20x20 == 400 components?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: multi array of JButtons

    Completely feel retarded.... Thanks......

    Got it working :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: multi array of JButtons

    Quote Originally Posted by jocdrew21 View Post
    Completely feel retarded.... Thanks......

    Got it working :)
    Hm, I never paid much attention to it, but what happens if one adds more than NxM components to a container, managed by a NxM GridLayout manager? Something should go wrong, but I never realized that it would be that ugly ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Array of JButtons
    By Chas3down in forum New To Java
    Replies: 12
    Last Post: 09-19-2013, 05:42 AM
  2. How can I make an array of JButtons?
    By LuxoJr in forum New To Java
    Replies: 2
    Last Post: 02-27-2012, 04:51 AM
  3. Replies: 8
    Last Post: 04-22-2011, 02:55 PM
  4. Creating array of JButtons
    By tabchas in forum New To Java
    Replies: 20
    Last Post: 04-16-2011, 06:36 AM
  5. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:42 AM

Posting Permissions

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