?
Solved

Help with a method

Posted on 2004-03-24
10
Medium Priority
?
241 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
[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
  • 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
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!

 
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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

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