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

asked on

Logic Question

On my dialog I have a textfield named curProject, when the dialog is created that field is set to

curProject.setText("NO PROJECT SELECTED");

I also have a jtable

The problem I am having is I only want the OK button to be enabled if there is a project selected and there is a value in the jtable

So I have create a checkProject variable and listSize varaible.  
The listSize is getting a value from

int listSize = checkList.size(); <-  checkList is the arraylist

when the method is executed to select a project I am assigning checkProject to 1

private void updateCurrentProjectLabel() {
      if (currentProject != null) {
    	  curProject.setText(currentProject.getName());
    	  threeDButton.setEnabled(true);
    	  emailButton.setEnabled(true);
    	  checkProject = 1;
    	  OKCancelButtons();
      } // end if
      else {
         curProject.setText("NO PROJECT SELECTED");
      }
    	   
   } // end updateCurrentProjectLabel()  

Open in new window


and I am running the method for my buttons OKCancelButtons() after I change the value

 
private JPanel OKCancelButtons() {
      JButton okButton = new JButton(appReg.getString("edi.transButton"));
      //System.out.println("Check Project : " + checkProject + "\n");
      int listSize = checkList.size();
      //System.out.println("Check Size : " + listSize + "\n");
      if (listSize > 0 && checkProject == 1) {
          okButton.setEnabled(true);
      }
      else {
    	   okButton.setEnabled(false);
      }
      
      okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        	 startGetReqInfoOperation(); 
        	 EdiSelection.clearList();
        	 
         }
      });

Open in new window


I assume that when the button method is being called from updateCurrentProjectLabel() method, it should see the new value and the if statement should work.

But it don't, I can not get it to work.  I only want the OKButton to be enabled when there is something in checkList and chcekProject = 1  
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Place debugging System.out.println calls in there to see what's actually being called
Avatar of jkteater

ASKER

It is getting in to the IF like it is suppose to.  So the logic of the IF statement seems to be working - but the button even though it is now set to be enabled

 okButton.setEnabled(true);

It is not refreshing the button
How, and on what thread is updateCurrentProjectLabel being called?

BUt when create it it is enabled by deafualy:
 JButton okButton = new JButton(appReg.getString("edi.transButton"));

How do you know the difference?

Please, muind that enable desiable the button is only efelected iun the button appearsnce.
Its action does not dfpend on that - it depends on wherther ctionListenre is added or not
updateCurrentProjectLabel is being called after the user selects a project - the project dialog is called when the user clicks a select button
Please, explain how do you know it does not enable the button.
At this point button is created enabled
JButton okButton = new JButton(appReg.getString("edi.transButton"))

If it goes into this part:

 if (listSize > 0 && checkProject == 1) {
          okButton.setEnabled(true);
      }

so nothing shouild be cahnged.

You don't need the first part of this if, as it is created enabled
You only may want to check whwen you want to disable it.

And then you show here only how you create the button -
but you need to add it to the panel and then somewhow replace
the panle, or how does it work?

This is in general not a good idea to replace these panels in the GUI in the process,
why do you need it?

Craete all eleemnts form the bginnijing and then you may only change steEnabled(false/true)
but don't create your buttons and panels in the process.
It is always tricky.



SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
both are true
OK. So we know that

a. it's being called
b. it was enabled before being called

So - what's the current state of the problem?
Here is the new code

int checkList = myModel.getRowCount(); <-  getting my list size
JButton okButton = new JButton("Send Transmittal"); <- creating the button
okButton.setEnabled(false);                                         <- setting to false - want it grayed out now  
if (checkList > 0 && checkProject == 1) {                      <- if my list has something and checkProject = 1
       okButton.setEnabled(true);                                   <- the button is not grayed out
  }

Open in new window


When the dialog is created checkProject is always 0, because a project has not been selected.  So the first IF should be (true && false)

The user clicks the select project button - the button is in a differnt method
      projectButton.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent event )
         {
        	 showProjectSelectDialog(); 
         }           
      });

Open in new window


   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                   showProjectSelectDialog()                          //
   //                                                                      //
   ////////////////////////////////////////////////////////////////////////// 
   private void showProjectSelectDialog() {
      EdiSelectProjectDialog ar = new EdiSelectProjectDialog(this, session, appReg); <- calling a project dialog
	  ar.setModal(true);
	  ar.setVisible(true);
	  EdiProject newProject = ar.getSelectedProject(); <- getting the project selected
	  if (newProject != null) {
		  currentProject = newProject;                         <- passing the project
		  updateCurrentProjectLabel();                        <- calling the update method
		  updateCurrentTranslators();
		  updateCurrent2dTranslators();
	      updateCurrentEmails();
	  }
   } 

Open in new window


 private void updateCurrentProjectLabel() {
      if (currentProject != null) {
    	  curProject.setText(currentProject.getName());  <-  setting my textfield
    	  threeDButton.setEnabled(true);                         
    	  emailButton.setEnabled(true);
    	  checkProject = 1;                                               <- this is where I am changing the value of checkProject
    	  
      } // end if
      else {
         curProject.setText("NO PROJECT SELECTED");
      }
    	   
   } // end updateCurrentProjectLabel()  

Open in new window


so now have both values I need to ungray the OK button.  But I am not sure how to get back to the IF statement .

Do I call the OKCancelButton method under the checkProject = 1?  
Should the code pick up the changes?
ASKER CERTIFIED SOLUTION
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
That button is being created in my OKCancelButtons method which is a jpanel then in my createDialog method I am adding it

public void createDialog() {
        
        Component selectionsPanel = selectedTable();
        Component currentPanel = currentItems();
        Component buttonPanel = OKCancelButtons();
        centerPanel = new JPanel();


so you would create all your buttons in the constructor?
Yes, I would create all buttons, labels, etc. in the constructor and make themn static.
You can imagine some cases when it is not possivble, - but for most tasks it is usuallay OK.
and then whlile you are handling events - you can change colors, background, texts,etc.
This desind would be much simpler.

>make themn static. - I meant static in the everyday sense - all the time sitting in the interface - not in java senese
Think of it - perhaps you ca desing it so that you don;t need to create buttons on the fly
If you do - that really requiers understading - so you should not expect see change in the button which you haven't really yet expoiosed but just constructed, etc.
Better jkeep the elements all the tim sitting and aonly chnage theior properties - it will make youir life simpler.
I am closing this one and starting a new question