• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic