Solved

Help with a method

Posted on 2004-03-24
10
196 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
Comment Utility
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
Comment Utility
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
Comment Utility
           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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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:
mayankeagle earned 100 total points
Comment Utility
else if (command.equals("fnew")) {
     clear () ;      // ----> JUST CALL THE clear () METHOD - KEEP THE clear () METHOD INSIDE YOUR CLASS JUST LIKE ANY METHOD
    }
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
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
Comment Utility
mayankeagle  

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now