?
Solved

Gui Buttons

Posted on 2003-03-21
12
Medium Priority
?
153 Views
Last Modified: 2012-05-04
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
Comment
Question by:chocobogo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179884
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
 

Author Comment

by:chocobogo
ID: 8179967
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
 

Author Comment

by:chocobogo
ID: 8179989
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:chocobogo
ID: 8179991
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
 

Author Comment

by:chocobogo
ID: 8179995
sorry didnt mean todo multiple posts!
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 8180002
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
 

Author Comment

by:chocobogo
ID: 8180014
cheers mate

very clear help
0
 
LVL 9

Expert Comment

by:Ovi
ID: 8180038
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8180047
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
 
LVL 9

Expert Comment

by:Ovi
ID: 8180095
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8180165
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
 
LVL 9

Expert Comment

by:Ovi
ID: 8180409
yep, see you arround :-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

762 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