Drop_of_Rain
asked on
Help with a method
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(ActionEven t 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(ActionEven t e) {
if (e.getActionCommand().equa ls("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();
}
}
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(ActionEven
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(ActionEven
if (e.getActionCommand().equa
}
});
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();
}
}
ASKER
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.
List list = new ArrayList();
Component temp;
Container container = null;
int count = getComponentCount();
do
{
if (list.size() > 0)
{
container = (Container)list.remove(0);
count = container.getComponentCoun t();
}
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
Component temp;
Container container = null;
int count = getComponentCount();
do
{
if (list.size() > 0)
{
container = (Container)list.remove(0);
count = container.getComponentCoun
}
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's enough.
Keep the clear() method as private in your class. Then call that method in this conditional check.
Keep the clear() method as private in your class. Then call that method in this conditional check.
ASKER
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.
>> 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?
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?
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....
>> String command = e.getActionCommand();
>> if (command.equals("quit")) {
You should do:
Object source = e.getSource () ;
if ( source == quitMenuItem ) {
- and so on....
ASKER
mayankeagle
It wasn't your email ID sorry. Thanks for the advice.
It wasn't your email ID sorry. Thanks for the advice.
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