Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Button Logic - Part 2

This is similar to the open question, but I feel like this question is more clear.

just like the last question with the removeButton.  When something is added to the ArrayList the table is updated and the removeButton is enabled or disabled.  This works great.

I have a similar issue with the submit button on my base form.  The button needs to know if there is something in the table like the removeButton, but it also has to check and see if  a project is assigned.

so the button is not enabled unless there is a project assigned and the table has something in it.

Like the removeButton if the table is empty and the user adds something the submitButton needs to check and see if the project is assigned, if it is then enables the button.

How would you go about the logic on this one?
Avatar of for_yan
for_yan
Flag of United States of America image

All these thisng are the same - you make a method in the original class which contain button to change the button - and
then in both places where you handle the evnet of changing the project or adding stuff to the table you check - in the first case (when dealing with assigniong project) you check if the table contains something and then enable the button, in the secodn caase (when adding ti the table) you check
if project is assigned and then enable the button.
It is just the matter to have accessible reference to the instance of the class with the button in all these places -0 but that should not be a problem
Avatar of jkteater

ASKER

With the removeButton - I created the button in the Singleton Class and then had a method to check the size of the ArrayList.  If there was something the button was true - if not the button was false.  I called the method from the add method - then in the base class I just added the button

p.add( myModel.getButton());

Should I do something similar with the submitButton?  write a method in the Singleton class?
Yes, if it worked for you with one button, then why not to do the same with the next button
In general I usually do it in such a way - does not mean that it is the only possible way - but that seesm logical to me:

Suppose I have some element on soem class/window which depends on some status of data.
So in that class/window I creatte a public method that will change the propertuy of that element - change the color,
enable diasble the buttion, etc.
Then everywhere where the underalying data change in the way that it could affect the status of the elemnt I have a
reference to that class which contains the element and as soon as the data change I check if the element visual status should be
affected - and call this method right form this point when I'm changing the data.
This system in normal cases works fine and in my mind rather easy to use.

I aslo prefer if possible to keep the set of elements in the winodw all the time the same and just change theire properties,
rather than on the fly adding/removing labels/buttons/panels, etc.
For most of the workflows such reqyuirement does not craete problems, and it is easy to program this way.
what confuses me is checking both values.

dialog opens -----

I have set the button to false, because there may not be anything in the table and I know that project is not set yet
the user adds something to the list - I know that there is something in the table now - so I enable the button
the button is now enabled and I have not checked the project.

the button is updated on the table which is part of, but how do I incorperate the project?
But when they set the project - again you can check perhaps getText() form some textbox - I guess - so whenever any of the two
cahnges happen - you need to check for the status of the second piece of data and enable only if both of them are persent (porject set and data in the table exist).
Aagain just the metter of having both pieces of data accessible at both places - and eventually you call the same method to modify yourt button.
>the user adds something to the list - I know that there is something in the table now - so I enable the button
so when the user adds something tio the list - check the status of the project and  enable the button only when it is set
so would I need to write a method in my base class that checks and see if project is set
then make a instance of my base class in my singleton class - then in my button method I can check both values there and update the button accordly
Yes, this is one possible way of doing it.
Here is what I did - Not working by the way

In my base class I added this method


public int getProjectStatus() {
         if (currentProject != null) {
          projectFlag = 1;
         }
         return projectFlag;
   }

Then in the singleton class - I tried to create a instance of the base class

EdiBaseDialog ebd;
   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                              Constructor                             //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////  
   private SingletonSelectTable() {      
   ebd = new EdiBaseDialog(null, null);
   }


Then in the button method I did this

public JButton getOKButton() {
         if(rds.size() > 0 && ebd.getProjectStatus()== 1){
               //okButton.setVisible(true);
               okButton.setEnabled(true);    
         }
         else {
               //okButton.setVisible(false);
               okButton.setEnabled(false);
         }
        return okButton;
   }

when I open the dialog it - it dont open throwing complaining about

ebd = new EdiBaseDialog(null, null);
this is the constructor of the base class

public EdiBaseDialog(Frame parent, TCSession theSession){

but I don't care about frame and session
so this is not the best way to do it
this is not very good methosd ; and what will it return if currentProject == null
I think compiler should complain - there is no return value
unless projectFlag is instance variable - btter to write it - if ... else .. and return for each case

public int getProjectStatus() {
         if (currentProject != null) {
          projectFlag = 1;
         }
         return projectFlag;
   }
You see, without having your code in self-standing piece so that I could compile and execute it,
it is difficult to follow.
I cant understand why this change leads you to this
ebd = new EdiBaseDialog(null, null);

and waht it has to do with chnging project and table and button accordingly


I would make it the way I was suggesting from the beggining
folowing the general idea of http:#37001883

So if you answer in simple way these three questions

So what is the name of the class which contains your button ?


What is the name of the class that knows about status of the project?

And where is that piece of code which makes the change in the table?

If you answer these three questions I can try to
show  some skeleton for you.




So what is the name of the class which contains your button ?

SingletonSelectTable Class

What is the name of the class that knows about status of the project?

EdiBaseDialog Class

And where is that piece of code which makes the change in the table?

SingletonSelectTable Class
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So the point is to have references to access the classess form each other and
on every change of data in table and change of project add one of the two "if" lines
depending in which class you are adding this line
wow - let me see if I get that working!!!
Null Pointer on edb.getProjectStatus()

Not sure why - should at least return false

is this the right place to put that code

public JButton getOKButton() {
        
         System.out.println("Status : " + edb.getProjectStatus() + "\n");
         System.out.println("Do I  : " + doIHaveDataInTable() + "\n");
         if(edb.getProjectStatus() && doIHaveDataInTable())enableOKButton(true);
         else enableOKButton(false);

        return okButton;
   }
I think null pointer mans that you ddid not communicate
correctly ten reference to edb.
At the time you do edb.getProjectStatus() edb is still null
That will cause null pointer

I think this code can or cannot be inside speciial method or just thei if near the place where
the event happens or beieng proceesed - it deso not matter
I put a print statement at the beginning of the getProjectStatus()  - it is not getting in the method
I think null pointer mans that you ddid not communicate
correctly ten reference to edb.
At the time you do edb.getProjectStatus() edb is still null
That will cause null pointer


How I do address that?
In your instance of SingletonSelectTable you need to make sure that your variable edb is pointing at your instance of EdiBaseDialog
(the same instance here user will change the project).
How and wnhen you crearte EdiBaseDialog?
If you create it in sst then keep this variable edb instance and if you alreday created it it should not be null.
If your sst is creted later than you can have a method in sst like
setEdb(EdiBaseDialog edb){
this.edb = edb;

}

then somehwre in EdibaseDialog you would
say  
stt.setEdb(this);

But understand that you dont need to create another instance of edb, yyou need to point to the same very instance where your project
will be set.

And by the time you use this method  edb.getProjectStatus()
your edb should already be piointing at your instance of EdibaseDialog


I am not 100% of what you are referring to.  I will try to show what I have

SingletonSelectTable class

public class SingletonSelectTable extends AbstractTableModel{

   
   EdiBaseDialog edb;

Open in new window


EdiBaseDialog class

public class EdiBaseDialog extends AbstractAIFDialog {

SingletonSelectTable myModel;

public EdiBaseDialog(Frame parent, TCSession theSession){
	   super(parent, false);
	   session = theSession;
	   myModel = SingletonSelectTable.getInstance();
	   createDialog();  
   } //end Construct

Open in new window


I am using myModel in the class ex:  myModel.getOKButton().setEnabled(false);

How and wnhen you crearte EdiBaseDialog?

If the user gets objects from the shortcut menu then SingletonSelectTable runs before  EdiBaseDialog but if the user clicks the menu button then  EdiBaseDialog runs before SingletonSelectTable
I have it working :)   Thank you so much for staying with me.  But I am learning as we go.  You have open my eyes to some new ways to do things better


public class SingletonSelectTable extends AbstractTableModel{

   
   EdiBaseDialog edb;

This is not enough - at this point edb is still null

OK, so when you write this code if in SST:

   if(edb.getProjectStatus() && doIHaveDataInTable())enableOKButton(true);
         else enableOKButton(false);

make it like that:

   if(edb != null   && edb.getProjectStatus() && doIHaveDataInTable())enableOKButton(true);
         else enableOKButton(false);

then in SST add method, as I already methdioned

public void setEdb(EdibaseDialog edb) {
this.edb = edb;
}

and in the cosndtructor o f EdiBaseDialog have following:

myModel = SingletonSelectTable.getInstance();
myModel.setEdb(this);

then it should not have nullpointer







Great!

I'd suggest that you look through this again and have this thing 100% clear.
Beleieve me, with all many words around it,  this is in fact is the heart and soul of object oriented programming
You may not know what is singleton pattern, or even polymorphism (which in my practical programming I frankly used really rarely,
notwithstadning the ammount of questions in various java drills about it)
or hundreds other fancy things,
and in most cases it is not that important if you run this pierce of code in this place or another place,
but you need 100% understand how these  interactions between objects happen - because that is where
object oriented programming really starts. This was one example. In fact in any program which has more
than one class you'll have something similar, and this is really very important very basic stuff.
 
Get some good rest - I have two questions coming tomorrow
OK. You are always welcome.
One question to be thinking about and I will ask it before I leave work for the day