Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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?
0
jkteater
Asked:
jkteater
  • 16
  • 15
1 Solution
 
for_yanCommented:
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.
0
 
for_yanCommented:
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
0
 
jkteaterAuthor Commented:
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
Yes, if it worked for you with one button, then why not to do the same with the next button
0
 
for_yanCommented:
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.
0
 
jkteaterAuthor Commented:
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?
0
 
for_yanCommented:
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.
0
 
for_yanCommented:
>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
0
 
jkteaterAuthor Commented:
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
0
 
for_yanCommented:
Yes, this is one possible way of doing it.
0
 
jkteaterAuthor Commented:
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);
0
 
jkteaterAuthor Commented:
this is the constructor of the base class

public EdiBaseDialog(Frame parent, TCSession theSession){

but I don't care about frame and session
0
 
jkteaterAuthor Commented:
so this is not the best way to do it
0
 
for_yanCommented:
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;
   }
0
 
for_yanCommented:
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.




0
 
jkteaterAuthor Commented:
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
0
 
for_yanCommented:


OK. You make OKButton instance vraibel in SingletonSelectTable

when you start in the very beginning - you have no data and no project - make sure to disabke the button

in SingletonselectTable you want to have the method:

public void enableButton(boolean b) {
OKButton.setEnabled(b);

}

and another method

public doIHaveDataInTable() {
if(data) return true; //somehow you should know I just wrtte if(data) schematically
else retrn false;

}

in
EdibaseDialog you make method which would
return statuds of the project

public boolean getProjectStatus() {
if(currentProject != null) return true;
else return false;

}

Now you make sure that in the class SingletonSelectTable you have access to
the instance of your class EdibaseDialog

so say

EdibaseDialog edb; <-- this in class SingletonSelectTable Class  and it should be assigned non-null to instance of your EdibaseDialog before  all these events are happneing

the samnme in oppsite way - so in EdiBaseDialog you have acces to Singleton... isntance - that one yiou can alwasy get by calling static getInstance()


anyway, now  in  EdiBaseDialog
you have

SingletonSelectTable sst;

then when you change project in EdiBaseDialog (or associate event with chaniging the project - I hope you have it as event)
you say this

if(getProjectStatus() && stt.doIHaveDataInTable() )sst.enableButton(true);
else sst.enableButton(false);

then when you get data in tabe in sst immediately after that you say:

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

Taht should work




 








0
 
for_yanCommented:
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
0
 
jkteaterAuthor Commented:
wow - let me see if I get that working!!!
0
 
jkteaterAuthor Commented:
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;
   }
0
 
for_yanCommented:
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
0
 
jkteaterAuthor Commented:
I put a print statement at the beginning of the getProjectStatus()  - it is not getting in the method
0
 
jkteaterAuthor Commented:
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?
0
 
for_yanCommented:
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


0
 
jkteaterAuthor Commented:
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
0
 
jkteaterAuthor Commented:
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
0
 
for_yanCommented:


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







0
 
for_yanCommented:
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.
 
0
 
jkteaterAuthor Commented:
Get some good rest - I have two questions coming tomorrow
0
 
for_yanCommented:
OK. You are always welcome.
0
 
jkteaterAuthor Commented:
One question to be thinking about and I will ask it before I leave work for the day
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 16
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now