Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enabling a JButton Based on an Associated Pair of JFormattedTextFields  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I have a basic design flaw but I am scratching my head on how to accomplish the following. I have a variable number of pairs of JFormattedTextFields on a JDialog. Each pair is associated with a disabled JButton. When the pair both contain data, I want to enable the associated JButton. Can someone help me think this through? TIA.
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically all you have to do is keep track of the TextFields and when they contain something. You can do this with a boolean 2D array, by using an EventHandler or polling. So something along the line of:

Dennis Putnam wrote:Perhaps I have a basic design flaw but I am scratching my head on how to accomplish the following. I have a variable number of pairs of JFormattedTextFields on a JDialog. Each pair is associated with a disabled JButton. When the pair both contain data, I want to enable the associated JButton. Can someone help me think this through? TIA.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but my problem is when and how to do that. I tried using a single listener for all the fields but I could not figure out how to identify which set had the focus. My other thought was to create a new listener for each set and put some kind of identifier in the constructor. I haven't tried that because I'm leery of that approach as there could be a dozen or more listeners at any given time.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here one solution that is flexible and reusable:


Note how it is presented in the form of a SSCCE so it is easy to copy/paste/compile and test, unlike code you post with your questions.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Here one solution that is flexible and reusable:


You would need to create one instance of the class for each button/textfield group.

Note how it is presented in the form of a SSCCE so it is easy to copy/paste/compile and test, unlike code you post with your questions.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I read a little about DocumentListeners but I could not find an example that helped me understand how it applies to my situation. I'll take a harder look now at your code.

P.S. There is no point in posting an SSCCE that does not reproduce the problem stand-alone, is there?
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no point in posting an SSCCE that does not reproduce the problem stand-alone, is there?


The idea is to demonstrate the problem with minimal code. The majority of time if you truly understand what you application is doing you can create a SSCCE with a few lines of code.

If you can't reproduce the problem, then you don't understand your problem or you haven't narrowed down the problem, so you need to do more work.


That is you start with your original application and isolate the problem.

For example in the code you posted you have a menubar with multiple menu items. However only one is relevant to the problem as you stated the question. So you get read of all the others and see if the problem still exists.

If so then you keep cutting out more code.

If not, then your problem is related to the order in which you invoke menu items. So you need to isolate that.

The point is you need to understand your question BEFORE you post the question. This is done by eliminating all excess code not related to the problem.

 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no point in posting an SSCCE that does not reproduce the problem stand-alone, is there?


The idea is to demonstrate the problem with minimal code. The majority of time if you truly understand what you application is doing you can create a SSCCE with a few lines of code.

If you can't reproduce the problem, then you don't understand your problem or you haven't narrowed down the problem, so you need to do more work.


That is you start with your original application and isolate the problem.

For example in the code you posted you have a menubar with multiple menu items. However only one is relevant to the problem as you stated the question. So you get read of all the others and see if the problem still exists.

If so then you keep cutting out more code.

If not, then your problem is related to the order in which you invoke menu items. So you need to isolate that.

By creating a working SSCCE you have proved that all you need is the dispose() method. You have been given information and links to the API that will explain when the dispose() method might not work. We don't have access to your code so we can't tell you which condition you are breaking. It is up to you to isolate the problem, then if you can't find a solution we might be able to help.

The point is you need to understand your question BEFORE you post the question. This is done by eliminating all excess code not related to the problem.
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The event knows which object, then you can just make a comparison to an array to find the right group, and thus; the right index.
Dennis Putnam wrote:Thanks but my problem is when and how to do that. I tried using a single listener for all the fields but I could not figure out how to identify which set had the focus. My other thought was to create a new listener for each set and put some kind of identifier in the constructor. I haven't tried that because I'm leery of that approach as there could be a dozen or more listeners at any given time.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to do better but creating an SSCCE that demonstrates the problems I've come here for has been a time consuming and fruitless struggle. It was my hope that the problem was something stupid I did in the posted code segment and easy for an expert to spot. However, in one case it turned out my IDE went out to lunch giving false syntax errors as opposed to something really wrong with my code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!