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

x
?
Solved

Logic Question

Posted on 2011-10-19
16
Medium Priority
?
235 Views
Last Modified: 2012-05-12
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  
0
Comment
Question by:jkteater
  • 6
  • 6
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36993549
Place debugging System.out.println calls in there to see what's actually being called
0
 

Author Comment

by:jkteater
ID: 36993609
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36993703
How, and on what thread is updateCurrentProjectLabel being called?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 47

Expert Comment

by:for_yan
ID: 36993737

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
0
 

Author Comment

by:jkteater
ID: 36993813
updateCurrentProjectLabel is being called after the user selects a project - the project dialog is called when the user clicks a select button
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36993989
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.



0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 36994122
Please say what the following prints
System.out.printf("Is threeDButton enabled? %b\n", threeDButton.getEnabled());
threeDButton.setEnabled(true);
System.out.printf("Is threeDButton enabled now? %b\n", threeDButton.getEnabled());

Open in new window

0
 

Author Comment

by:jkteater
ID: 36994412
both are true
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36994451
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?
0
 

Author Comment

by:jkteater
ID: 36994594
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?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 100 total points
ID: 36994675
>JButton okButton = new JButton("Send Transmittal"); <- creating the button
>okButton.setEnabled(false);                                         <- setting to false - want it grayed out now  

Look, you just createdd the button, and you cannot see it anywhere - you need to add it to some panel and window and
then refrehs the widnow or something - but at the point when yiou create the button you never see it,
so you cannot see how it vbecomes not enabled at this point

That's why I'm saying - try to avoid this dsign wher you need to cretae buttons on the fly
Crteate all necerssary buttons (and lables and panels) befores hand show them in the constructor and then
you can chnge theore befhavioprs and properies and see it.

0
 

Author Comment

by:jkteater
ID: 36994811
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36994828
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.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36994838
>make themn static. - I meant static in the everyday sense - all the time sitting in the interface - not in java senese
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36994854
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.
0
 

Author Comment

by:jkteater
ID: 37001636
I am closing this one and starting a new question
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question