Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:38:08+00:00 2026-05-13T14:38:08+00:00

I have an array of JPanel s and I’m trying to add a mouseadapter

  • 0

I have an array of JPanels and I’m trying to add a mouseadapter to each one so it will be possible to identify which one was clicked and then change its background colour. Everything seems to work fine except when I run it from eclipse when a page will appear that says EventDispatchThread.run() line: not available, Source not found, and in the debug window it says:

  Thread [AWT-Shutdown] (Running) 
  Daemon Thread [AWT-Windows] (Running) 
  Thread [AWT-EventQueue-0] (Suspended (exception ArrayIndexOutOfBoundsException)) 
   EventDispatchThread.run() line: not available 
  Thread [DestroyJavaVM] (Running) 
  Thread [AWT-EventQueue-0] (Running) 

This is the code:

private void drawBoard() {
  LayoutManager layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLS);
  boardPanel.setLayout(layout);
  boardPanel.setPreferredSize(new Dimension(200, 400));
  chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
  MoveArrays move = new MoveArrays();
  move.initialisePieceMoves();
  for (int i = 0; i < NUMBER_OF_ROWS; i++) {
   for (int j = 0; j < NUMBER_OF_COLS; j++) {
    int index = i * 4 + j;
    chessBoard[i][j] = new JPanel();
    chessBoard[i][j].addMouseListener(clickSquare(j, i, index, move));
    chessBoard[i][j].setBackground(getColor(i,j));
    if (!(boardArray.chessBoard[index].square.isEmpty())) {
     Piece piece = (Piece) boardArray.chessBoard[index].square.firstElement();
     JLabel pieceString = new JLabel(piece.toString()); 
     chessBoard[i][j].add(pieceString);
    }
    boardPanel.add(chessBoard[i][j]);
   }
  }
 } // drawBoard()

 private MouseAdapter clickSquare(final int xCo, final int yCo, final int index, final MoveArrays move) {
  return new MouseAdapter() {
   public void mousePressed(MouseEvent me) {
    resetColors();
    JPanel selectedSquare = (JPanel) me.getSource();
    selectedSquare.setBackground(selectedColor());
    System.out.println("xCo: " + xCo + " yCo: " + yCo);
    Vector validMoves = move.DroneMovesNorth[index].Moves;
    int totalMoves = move.DroneTotalMovesNorth[index];
    if (!validMoves.isEmpty()) {
     for (int n = 0; n <= totalMoves; n++) {
      String stringMove = validMoves.elementAt(n).toString();
      int intMove = Integer.parseInt(stringMove);
      System.out.println("intMove: " + intMove);
     }
    }
   }
  };
 }

I think it might be the fact that I cast the me.getSource to a JPanel but shouldn’t it be one anyway? If I don’t put the cast it says that it cannot bind an ‘Object’ to a JPanel, when I do System.out.print(me.getSource()) it prints a line saying that it’s a JPanel so I don’t get what the problem is. Any help would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:38:08+00:00Added an answer on May 13, 2026 at 2:38 pm

    It appears the problem is not to do with the source of the mouse event. Indeed I think there’s two different things being called “source” here, and you’re mixing them together. I think the message:

    EventDispatchThread.run() line: not available, Source not found
    

    Is Eclipse telling you that the library does not have the source code attached to it, so it cannot find line numbers or show you the source code. The problem does not appear to be with me.getSource().

    The problem is that you are trying to reference an index within an array that is outwith the bounds of the array (hence the ArrayIndexOutOfBoundsException in the stack trace).

    Since the stack trace is on the AWT Event Queue thread, it is likely the exception is stemming from within the mousePressed() method of your MouseAdapter. Since you’re already using Eclipse, I suggest you get to know the Debugger, which is extremely useful. Put a breakpoint at these lines:

    Vector validMoves = move.DroneMovesNorth[index].Moves;
    int totalMoves = move.DroneTotalMovesNorth[index];
    

    And check that both the array fields of move are large enough to reference an element at the index. If you don’t want to use the debugger (I’d really recommend that way), then you could wrap those two lines with an exception catch, like this:

    try {
        Vector validMoves = move.DroneMovesNorth[index].Moves;
        int totalMoves = move.DroneTotalMovesNorth[index];
    catch(ArrayIndexOutOfBoundsException e) {
        System.out.println("Exception thrown: index = " + index + 
            "Array lengths: " + move.DroneMovesNorth.length + ", " +
            move.DroneTotalMovesNorth.length);
    }
    

    When/if the exception is caught, you want to find out why the index is larger than the size of each of these arrays. That’s left as an exercise 😉


    Edit: there’s some other code in there which looks suspicious.

    First you declare the array for the chessboard:

    chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
    

    Then you begin to iterate for every square in the board:

    for (int i = 0; i < NUMBER_OF_ROWS; i++) {
       for (int j = 0; j < NUMBER_OF_COLS; j++) {
    

    Note that i will run up to the value NUMBER_OF_ROWS, which is the length of the chessboard array. Then for some reason I don’t understand, you change the value of index to something that could be 4x as large as the length of the array:

    int index = i * 4 + j;
    

    And later try to reference that position in the chessboard array:

    if (!(boardArray.chessBoard[index].square.isEmpty())) {
    

    This means it’s possible for index to be a value higher than NUMBER_OF_ROWS, and if you try to access the element at that index of the array, it will throw the ArrayIndexOutOfBoundsException.

    So, my second suggestion would be to take another look at the logic involved in:

    int index = i * 4 + j;
    

    … as that may also be the problem.

    P.S. the debugger in Eclipse is awesome, you should use it 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel to which I'd like to add JPEG and PNG images
I created a array of JPanels which contains a common JLabel class gui {
i have array like below which is sorted by array_count_values function, Array ( [Session
I have array of two textboxes in a table, on blur of one text
I have searched many places to add and display images dynamically on JPanel but
So, I have a grid layout which stores JScrollPane's in each cell. These are
I have array like this: array('1224*', '543*', '321*' ...) which contains about 17,00 "masks"
I have 5 JLabels inside a JPanel which is inside a JFrame . I
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.