• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Gui Buttons

I have set up a gui interface and added some buttons like below

e.g.

Button programmeDataButton = new Button("Program Data");
When someone clicks on this buttton i want it to display the following
 if (buttonName.equals("List Programmes"))
    {

     add(progs);
     progs.setBounds(20, 140, 550, 400);

}

Which works fine.

The thing i wanted to know is how I can then say, the next time someone clicks on that button the "progs" is removed i.e. remove(progs);
At the moment I have added another button within the above code called "done" and when this is clicked it removes "progs" but I wanted to know whether there was a way do do it on the second click of the initial button!

Cheers
0
chocobogo
Asked:
chocobogo
  • 5
  • 4
  • 3
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
You can keep a boolean flag data-member in the class, initially set to false. Then, with every click of the button (in the actionPerformed () method), you should invert the value of flag, i.e., flag = ! flag ; And in case the value of flag is false after this, then the method has been invoked twice, or the button has been clicked twice.

Mayank.
0
 
chocobogoAuthor Commented:
Ok I have tried

     ProgrammeDataEntry progData = new ProgrammeDataEntry(this);    
     boolean flag = false;



(in the actionPerformed () method) :-



    if ((buttonName.equals("Program Data"))&& (flag = false))
    {
     add(progData);
     progData.setBounds(20, 140, 250, 200);
     flag = ! flag;
    }
   else
         if ((buttonName.equals("Program Data"))&& (flag = true))
    {
    remove(progData);
    flag = ! flag;
    }


It compiles fine but I cant seem to get it to work !!!
0
 
chocobogoAuthor Commented:
Ok I have tried

     ProgrammeDataEntry progData = new ProgrammeDataEntry(this);    
     boolean flag = false;



(in the actionPerformed () method) :-



    if ((buttonName.equals("Program Data"))&& (flag = false))
    {
     add(progData);
     progData.setBounds(20, 140, 250, 200);
     flag = ! flag;
    }
   else
         if ((buttonName.equals("Program Data"))&& (flag = true))
    {
    remove(progData);
    flag = ! flag;
    }


It compiles fine but I cant seem to get it to work !!!
0
Independent Software Vendors: 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!

 
chocobogoAuthor Commented:
Ok I have tried

     ProgrammeDataEntry progData = new ProgrammeDataEntry(this);    
     boolean flag = false;



(in the actionPerformed () method) :-



    if ((buttonName.equals("Program Data"))&& (flag = false))
    {
     add(progData);
     progData.setBounds(20, 140, 250, 200);
     flag = ! flag;
    }
   else
         if ((buttonName.equals("Program Data"))&& (flag = true))
    {
    remove(progData);
    flag = ! flag;
    }


It compiles fine but I cant seem to get it to work !!!
0
 
chocobogoAuthor Commented:
sorry didnt mean todo multiple posts!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try:

flag = ! flag ;

if ( buttonName.equals ( "Program Data" ) )
{
  if ( flag )
  {
    progData.setBounds ( 20, 140, 250, 200 ) ;
    add ( progData ) ;

  } // end nested if

  else
    remove ( progData ) ; // end else

} // end outer if


Mayank.
0
 
chocobogoAuthor Commented:
cheers mate

very clear help
0
 
OviCommented:
More aqurate will be:

if(buttonName.equals("Program Data")) {
 if(flag) {
   progData.setBounds(20, 140, 250, 200);
   add(progData);
 } else
   remove(progData);
 flag = ! flag ;
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ovi,

For your code, flag will initially have to be initalized to true as per his requirements. Anyways, hardly makes a difference....

Cheers :-)

Mayank.
0
 
OviCommented:
Yes, indead, but elseware. In your code the flag is reset prior to tests, so will not aqurate reflect the state of the button. In the rest the code is the same (copy - pasted from yours).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yeah, agreed. We don't know the details of his actionPerformed () method as yet. Of course, its better to invert the value of flag after the 'if' statements. Anyways, as long as he has managed to put it to work, all's well that end's well :-)

Mayank.
0
 
OviCommented:
yep, see you arround :-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now