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 8015607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:18:04+00:00 2026-06-04T20:18:04+00:00

I have a question about Java and JFrame in particular. Let’s say I have

  • 0

I have a question about Java and JFrame in particular. Let’s say I have a JFrame that I am using with a GridLayout. Supposing that I have added JButtons in the JFrame, how can I gain access to the one I want using it’s position (by position, I mean a x and a y, used to define the exact place on the Grid). I have tried several methods, for instance getComponentAt(int x, int y), and have seen that those methods do not work as intended when combined with GridLayout, or at least don’t work as intended in my case. So I tried using getComponent(), which seems fine.

The latest method, that seems to be on a right track for me is (assuming I have a JFrame with a GridLayout with 7 rows and 7 columns, x as columns, y as rows):

public JButton getButtonByXAndY(int x, int y) {
    return (JButton) this.getContentPane().getComponent((y-1) * 7 + x);
}

image http://www.freeimagehosting.net/newuploads/9d6hq.jpg

Using the above, say I want to get the JButton at (4, 4), meaning the 25th JButton in the JFrame, I would index through the first 21 buttons at first, and then add 4 more, finally accessing the JButton I want. Problem is this works like that in theory only. Any ideas?

P.S sorry for linking to an external website, but stack overflow won’t allow me to upload the image, because I do not have the required reputation.

  • 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-06-04T20:18:05+00:00Added an answer on June 4, 2026 at 8:18 pm

    This example compares two approaches:

    1. Maintain a List<JButton> and calculate the index algebraically.

    2. Extend JButton and allow each button to maintain its own grid location.

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

Sidebar

Related Questions

I Have a question about finding html tags using Java and Regex. I am
I have a simple question about the java classloading mechanism. I think that the
I have a question about local classes in Java (classes that declares in the
I had a question about Java Class Path variables. If I have multiple jars
So, i have some question about xml Documents in Java. Can i get all
I have a question about Java Generics. If I define a class like this:
I have a question about linked lists in java. I saw an interesting approach
I have a question about working with time in java, more specificly in Android.
I have a question about the behaviour of Timer class in Java. This is
I have a question about MIMEParsingException. I use Java EE 6 with NetBeans 6.8.

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.