Solved

Help with a method

Posted on 2004-03-24
10
240 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

728 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