Solved

Help with a method

Posted on 2004-03-24
10
219 Views
Last Modified: 2010-03-31
Hi everyone thanks for all your help. How would I get the method below into:
 else if (command.equals("fnew")) { Code below)


private void clear ()
{
int count = getComponentCount () ;
Component temp ;

for ( int i = 0 ; i < count ; i ++ )
{
temp = getComponent ( i ) ;

if ( temp instanceof JTextField )
( ( JTextField ) temp ).setText ( "" ) ;

else if ( temp instanceof JCheckBox )
( ( JCheckBox ) temp ).setSelected ( false ) ;

} // end for

} // end of clear ()

Call this clear () method wherever you want to.
}


public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("quit")) {
      YesNoDialog d = new YesNoDialog(this, "Really Quit?",
                                      "Are you sure you want to quit?",
                                      "Yes", "No", null);
      d.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          if (e.getActionCommand().equals("yes")) System.exit(0);
          }
      });
      d.show();
    }
    else if (command.equals("fnew")) {
     
    }
    else if (command.equals("open")) {
      FileDialog d = new FileDialog(this, "Open File", FileDialog.LOAD);
      d.show();  // display the dialog and block until answered
            d.dispose();
    }
    else if (command.equals("save")) {
      FileDialog d = new FileDialog(this, "Save File", FileDialog.LOAD);
      d.show();  // display the dialog and block until answered
            d.dispose();
    }
    else if (command.equals("about")) {
      InfoDialog d = new InfoDialog(this, "About",
                                "This is Training Arts Program\n" +
                                "Copyright (c) 2004 Minor & Associates");
      d.show();
    }
  }
0
Comment
Question by:Drop_of_Rain
  • 4
  • 3
  • 3
10 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10665287
Directly start off by opening the TextArea.

When closing the window and/or when save is called....

      FileDialog d = new FileDialog(this, "Save File", FileDialog.LOAD);
      d.show();  // display the dialog and block until answered
 

Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
ID: 10665315
You missed what I was trying to say. I need to get the clear method into the (command.equals("fnew")) { so I can clear all of the textlabels and ckeckboxs.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10665328
           List list = new ArrayList();
            Component temp;
            Container container = null;
            
            int count = getComponentCount();

            do
            {
                  if (list.size() > 0)
                  {
                        container = (Container)list.remove(0);
                        count = container.getComponentCount();
                  }
                  
                  for (int i = 0; i < count; i++)
                  {
                        if (container != null)
                        {
                              temp = container.getComponent(i);
                        }
                        else
                        {
                              temp = getComponent(i);
                        }

                        if (temp instanceof JTextField)
                        {
                              ((JTextField) temp).setText("");
                        }
                        else if (temp instanceof JCheckBox)
                        {
                              ((JCheckBox) temp).setSelected(false);
                        }
                        else if (temp instanceof Container)
                        {
                              list.add(temp);
                              continue;
                        }                        
                  }
            }
            while (list.size() > 0);



What is the main wrapper of your class
Is it is frames or panel.  

Then it will work
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10665378
Drop_of_Rain, you need to just call the method:

public class .... // the same class which has the actionPerformed () method
{
private void clear ()
{
int count = getComponentCount () ;
Component temp ;

for ( int i = 0 ; i < count ; i ++ )
{
temp = getComponent ( i ) ;

if ( temp instanceof JTextField )
( ( JTextField ) temp ).setText ( "" ) ;

else if ( temp instanceof JCheckBox )
( ( JCheckBox ) temp ).setSelected ( false ) ;

} // end for

} // end of clear ()

public void actionPerformed ( ActionEvent ae )
{
  .... // your existing code

  else if (command.equals("fnew")) {
     clear () ;      
    }

  .... // your existing code

} // end of actionPerformed ()

} // end of class
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 10665388
else if (command.equals("fnew")) {
     clear () ;      // ----> JUST CALL THE clear () METHOD - KEEP THE clear () METHOD INSIDE YOUR CLASS JUST LIKE ANY METHOD
    }
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10665406
That's enough.

Keep the clear() method as private in your class.  Then call that method in this conditional check.
0
 

Author Comment

by:Drop_of_Rain
ID: 10665484
Thanks I thought that,  but I am just so unsure of myself at this point, I feel intimidated. This is the first program I have every writen. It is way over my head for a first.  I learn best by example, so I just went for it. I emailed you something.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10665586
>> I learn best by example, so I just went for it

Good. You should do that.

>> I emailed you something.

To me? I didn't get anything? Moreover, where did you get my e-mail ID from?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10665597
As a word of advice, generally, it is better to compare the source of the event rather than the action-command. Instead of doing:

>> String command = e.getActionCommand();
>> if (command.equals("quit")) {

You should do:

Object source = e.getSource () ;

if ( source == quitMenuItem ) {

- and so on....
0
 

Author Comment

by:Drop_of_Rain
ID: 10665880
mayankeagle  

It wasn't your email ID sorry. Thanks for the advice.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

810 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