0

I am trying to get a jbutton from a jframe. this is my code:

JButton button2 = (JButton) frame.getComponentAt(move);

I get this error when I do this: javax.swing.JFrame cannot be cast to javax.swing.JButton

These are the two class hierarchies http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html

They are the same at container, and getComponentAt is on the level of component. How can I get the button from the jframe?

EDIT

I am able to get a button once it is pressed. The purpose of my code is to make a game in which a computer will respond to a user's move on the grid, a given click, with a response click (as if two humans were playing and the second person, the computer, clicked a button just like the first person). I have to find a way to access the button in the jframe through the code.

Martin Awano
  • 86
  • 1
  • 8

1 Answers1

3

You should give your class that holds the JButton a public method, getMyButton() that returns the JButton of interest. Otherwise you'll be trying to get your reference in a very kludgy and brittle way (as you're finding out).

There may be even better ways of doing what you're trying to do without having to get the JButton reference, but in order to know this, we will need to better understand your current code and desired functionality.


Edit
You state in comments:

What I am trying to do is find the button given the frame and the point object (move). Do you know a way that I could find a button on gridlayout given a point object?

This information should be part of your original question as it is pertinent, and in fact I recommend that you give us even more of your problem's details.

If the button is pressed, then you can easily get a reference to the pressed button via its ActionListener. The actionPerformed ActionEvent parameter has a getSource() method that will return the source of the object that caused the ActionListener to fire.


Edit
You add:

I am able to get a button once it is pressed. The purpose of my code is to make a game in which a computer will respond to a user's move on the grid, a given click, with a response click (as if two humans were playing and the second person, the computer, clicked a button just like the first person). I have to find a way to access the button in the jframe through the code.

Consider putting your JButtons into a logical grid of some sort, say an array or even a 2-dimensional array. Then you can easily find your pressed button on the grid, and then have the computer work with any next button it wishes to select.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • What I am trying to do is find the button given the frame and the point object (move). Do you know a way that I could find a button on gridlayout given a point object? – Martin Awano Apr 05 '14 at 01:54
  • 1
    @user3224584: I've edited my answer. If you need further help, consider creating and posting a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Apr 05 '14 at 02:04
  • 1
    **"Consider putting your JButtons into a *logical* grid of some sort"** [Chess Champ](http://stackoverflow.com/a/21142687/418556) achieves that by creating a `JButton[8][8]` to represent the rows and columns of positions on the chess board. – Andrew Thompson Apr 05 '14 at 02:28