aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Apply 2d array of answer to the right position of JTextField grid with JCheckBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Apply 2d array of answer to the right position of JTextField grid with JCheckBox" Watch "Apply 2d array of answer to the right position of JTextField grid with JCheckBox" New topic
Author

Apply 2d array of answer to the right position of JTextField grid with JCheckBox

ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
I'm trying to make a JCheckBox that will apply answer to the JTextField when being checked. The answer is in 2Dimensional array and I want to apply the answer in the right/exact position as the topLeftNum. When i change the answer into ans, i got the answer displayed but in the wrong position. But, what i want to ask is why my JCheckBox didn't work(nothing happened)? Anyone can help me with this? Thank's

I changed it, no error but nothing happened.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
In this code:


What is the source? It's the component that when clicked/pressed/checked tells its actionlisteners to perform their actionPerformed method.

So what sets off the actionlistener here? A JTextField? Or is it the JCheckBox? If the latter, what's going to happen if you try to cast it to a JTextField?
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote:In this code:


What is the source? It's the component that when clicked/pressed/checked tells its actionlisteners to perform their actionPerformed method.

So what sets off the actionlistener here? A JTextField? Or is it the JCheckBox? If the latter, what's going to happen if you try to cast it to a JTextField?


JCheckBox sets off the actioListener. Do i have to use ItemListener for the JCheckBox or actionListeners can also do it?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
ethaniel landfough wrote:JCheckBox sets off the actioListener.

Correct

Do i have to use ItemListener for the JCheckBox or actionListeners can also do it?

They both would work, but one advantage to an ItemListener is that's a little more straightforward to get the checkbox state from the listener method's Event parameter :

ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote:
ethaniel landfough wrote:JCheckBox sets off the actioListener.

Correct


I have changed it into something like this : (No error, but nothing happen). Is there something else that i missed?




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
ethaniel landfough wrote:

What is answer = ans supposed to be doing here? Showing all of the text held in your JTextFields? If so, it won't be this simple to do as to accomplish this you'll have to iterate through your JTextField array changing each field as you go. In fact, it appears that you have to declare a JTextField 2-D array here (or perhaps a 2-D Box array) in order to be able to access them or change them.
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote:
What is answer = ans supposed to be doing here? Showing all of the text held in your JTextFields? If so, it won't be this simple to do as to accomplish this you'll have to iterate through your JTextField array changing each field as you go. In fact, it appears that you have to declare a JTextField 2-D array here (or perhaps a 2-D Box array) in order to be able to access them or change them.


You mean something like this?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
ethaniel landfough wrote:You mean something like this?


Before asking on the forum, "something like this?", first compile and run the code, and answer for yourself the question, "does this work? Does it change the display in any positive way?"

To figure out the logic here, please note that within this actionPerformed method you are creating a bunch of new Box objects. Is it possible that setting the text of these new non-displayed Box objects will change the displayed text of the Box objects shown in the JFrame? Let me state again: "...In fact, it appears that you have to declare a JTextField 2-D array here (or perhaps a 2-D Box array) in order to be able to access them or change them."
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote:

Before asking on the forum, "something like this?", first compile and run the code, and answer for yourself the question, "does this work? Does it change the display in any positive way?"

To figure out the logic here, please note that within this actionPerformed method you are creating a bunch of new Box objects. Is it possible that setting the text of these new non-displayed Box objects will change the displayed text of the Box objects shown in the JFrame? Let me state again: "...In fact, it appears that you have to declare a JTextField 2-D array here (or perhaps a 2-D Box array) in order to be able to access them or change them."


I'm sorry. My friend accidentally clicked the submit button when i left the computer for a moment (because i'm using the computer at the college library). And i forgot to edit the question.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
ethaniel landfough wrote:I'm sorry. My friend accidentally clicked the submit button when i left the computer for a moment (because i'm using the computer at the college library). And i forgot to edit the question.


I'm not sure what you're getting at here. Regardless, best of luck.
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote:
ethaniel landfough wrote:I'm sorry. My friend accidentally clicked the submit button when i left the computer for a moment (because i'm using the computer at the college library). And i forgot to edit the question.


I'm not sure what you're getting at here. Regardless, best of luck.


I have been thinking about what you said earlier if i'm not getting it wrong. Is this already right?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
ethaniel landfough wrote:
I have been thinking about what you said earlier if i'm not getting it wrong. Is this already right?

I'm not sure what you're trying to do here, but of course the true test of "rightness" is "if I try this, does my program work?" If what you are doing is truly in an actionPerformed method, then no, this isn't right.

Perhaps the best way to demonstrate what I mean is to give you a code example:

Let's say I have a similar but simpler program than yours, that mine uses a grid of "MyBox" objects rather than Box objects. Both are derived from JTextField:



Say I wanted to display a 10x10 grid of MyBoxes and also add a button that adds a single character random String into each box. I could start out like so:


but how would I access the MyBoxes/textfields to change them when the button is changed? What would I put in the button's actionPerformed method? I suppose I could recursively go through the GUI's components checking if a component is a MyBox object and changing it then, but that's messy and prone to error. Simply put, as written here there is no easy way to do this.

The way to solve this is to give my panel a 2 dimensional array or grid of MyBox objects (in your case, of Box objects), initialize this array in my constructor and use the objects from the array to fill the GUI. Then it's easy to access the text fields and change them from within the button's actionlistener:



Does this make sense?
ethaniel landfough
Greenhorn

Joined: Aug 16, 2009
Posts: 21
pete stein wrote: Does this make sense?


Thank's a lot, Pete...You have been a great help, thank you very much. After i try to understand your code example ,my program has working now...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Apply 2d array of answer to the right position of JTextField grid with JCheckBox
 
Similar Threads
Problem with 2D array of JButton
How to know which button being clicked?
Error message Lat1 isn't abstract method
automatically update score when user input in JTextField..
Limit the character in JTextField