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 4 of 4

Thread: problem of creating matrix with JButton or Panel

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem of creating matrix with JButton or Panel

    Hi java programmers!

    I got a run time problem with my code in below


    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class Aline extends JFrame {
    JFrame window = new JFrame("");
    JButton[][] butonlar;

    public Aline() {
    window.setSize(600, 600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new GridLayout(3, 3));
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;
    window.setLocation(x, y);
    butonlar= new JButton[5][5];

    // for (int i = 1; i <= 5; i++) {
    // for (int j = 1; j <= 5; j++) {
    // butonlar[i][j]=new JButton();
    // }
    //
    // }

    // for (int i = 1; i <= 5; i++) {
    // for (int j = 1; j <= 5; j++) {
    // window.add(butonlar[i][j]);
    // }
    //
    // }
    // window.add(butonlar[5][5]);
    window.setVisible(true);
    }

    public static void main(String args[]) {
    new Aline();
    }

    }


    as you can see, I tried to write different ways but i couldnt run it. And i ignored these lines and tried different way but again it did not work

    Error message is java.lang.ArrayIndexOutOfBoundsException: 5

    just i want to create a matrix with jbutton or panel and i ll use it to define my path on this matrix. But i m still getting error in first step of my program and i couldnt continue to complete


  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: problem of creating matrix with JButton or Panel

    You are trying to access an array element at the index which is greater than the array's size. Change i <= 5 to i < 5 it should work.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 03:04 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem of creating matrix with JButton or Panel

    thanks yes i wasnt aware of that
    thank you very much but still it doesnt run. My codes now like that:



    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Toolkit;

    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class Astar extends JFrame {
    JFrame window = new JFrame("GG");
    JButton[][] butonlar=new JButton[5][5];


    public Astar() {
    window.setSize(600, 600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new GridLayout(5, 5));
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;
    window.setLocation(x, y);
    butonlar= new JButton[5][5];

    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
    window.add(butonlar[i][j]);
    }

    }
    window.add(butonlar[5][5]);
    window.setVisible(true);
    }

    public static void main(String args[]) {
    new Astar();
    }

    }


    what is problem now?

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem of creating matrix with JButton or Panel

    ovv it is ok! now i made it.

    PRoblem was solved
    it is needed to define every button in for loop like that

    for (int i = 0; i < 5; i++) {

    // butonlar[i]=new JButton[5];
    for (int j = 0; j < 5; j++) {
    butonlar[i][j]=new JButton();
    }

    }

    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
    window.add(butonlar[i][j]);
    }

    }

    thanks again for your best java guide web side!

Similar Threads

  1. Matrix multiplication problem with different dimensions
    By mightyking in forum Collections and Generics
    Replies: 5
    Last Post: September 25th, 2011, 04:59 PM
  2. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  3. Inverse of a Matrix problem
    By shivamchauhan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 17th, 2011, 01:40 PM
  4. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 07:15 PM
  5. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM