Solved

Help with a method

Posted on 2004-03-24
10
209 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 68
HashMap Vs TreeMap 12 55
stackato and cloud 4 64
windows explorer path to command prompt 5 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

947 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

21 Experts available now in Live!

Get 1:1 Help Now