Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: multi array of buttons

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default multi array of buttons

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class Screen implements ActionListener 
    {
    	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();
    				j.add(b[row][col]);
    				b[row][col].addActionListener(this);
    			}
     
    		}
     
    		j.setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
    }

    I am trying to program an A* algorithm and I cannot even get past the buttons. I need a 2D array of buttons to handle this problem but I am getting the following errors.

    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)
    	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)
    	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)
    	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)


  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: multi array of buttons

    //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.RED);
    				b[row][col].addActionListener(this);
    				j.add(b[row][col]);
    			}
    		}

    Please help this is not creating a multi array of buttons

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: multi array of buttons

    Why 200 x 200 buttons in a 20 x 20 grid?

    What happens if you add a main() method and create an instance of Screen?

    When you're asking for help with errors or code that doesn't do what you think it should, provide compilable, runnable code that demonstrates the problem. With a few minor changes (mostly scaling the buttons to 20 x 20) and a main() method, your code works fine for me.

Similar Threads

  1. How to define a multi-dimensional array?
    By javaiscool in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2013, 11:40 PM
  2. Convert ArrayList to multi-dimensional array
    By dougie1809 in forum Loops & Control Statements
    Replies: 4
    Last Post: April 5th, 2013, 12:01 PM
  3. Replies: 4
    Last Post: December 19th, 2011, 09:57 PM
  4. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  5. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 04:28 AM