Click to See Complete Forum and Search --> : resetting components ...
mbonfyre
Apr 15th, 2003, 12:15 PM
how would I go about resetting combo boxes/radio buttons or checkboxes at a button click?
if (e.getSource() == JButClear)
{
name.setText(" ");
age.setText(" ");
}
marnitzg
Apr 15th, 2003, 02:34 PM
if (e.getSource() == JButClear)
{
name.setText("");
age.setText("");
myCheckBox.setSelected(false); //Deselects the checkbox
myRadioButton.setSelected(false); //Deselects the radio button
myComboBox.setSelectedIndex(0); //Sets to the first object
myComboBox.removeAll(); //Removes all items
}
Don't use a space to reset the textfields. rather send a blank string like above. Sending a space puts a space in the textbox and it can get rather irritating. Not sure if you wanted to set the combo box to the first item or to clear it, so I added code for both.
Hope that helps
Marnitz
mbonfyre
Apr 16th, 2003, 01:51 AM
Tried as you suggested, but it would not compile
error:
---------- Javac ----------
System2.java:525: cannot resolve symbol
symbol : method setSelected (boolean)
location: class java.awt.Checkbox
cbMale.setSelected(false); //Deselects the radio buttons
^
System2.java:526: cannot resolve symbol
symbol : method setSelected (boolean)
location: class java.awt.Checkbox
cbFemale.setSelected(false);
^
2 errors
Normal Termination
Output completed (0 sec consumed).
any suggestions?
marnitzg
Apr 16th, 2003, 01:15 PM
What jdk are you using?
Can you post your declarations for cbMale and cbFemale
mbonfyre
Apr 16th, 2003, 02:18 PM
jdk 1.3.1
public class System2 extends JFrame implements ActionListener, ItemListener{
....
String vgender;
CheckboxGroup oneCBG;
Checkbox cbMale, cbFemale;
.....
public System2() {
...
myOpts = new JPanel();
EtchedBorder opts = new EtchedBorder();
myOpts.setBorder(new TitledBorder(opts, " Gender: "));
c.ipadx = 12;
c.ipady = 12;
c.gridx=3;
c.gridy=6;
c.gridheight = 2;
gridbag.setConstraints(myOpts, c);
contentPane.add(myOpts);
oneCBG = new CheckboxGroup();
cbMale = new Checkbox("Male", false, oneCBG);
myOpts.add(cbMale);
cbMale.addItemListener(this);
cbFemale = new Checkbox("Female", false, oneCBG );
myOpts.add(cbFemale);
cbFemale.addItemListener(this);//additemListeners
...
public void actionPerformed(ActionEvent e){
if (e.getSource() == JButSubmit)
{
TADisplay.setText ((vgender);
}
public void itemStateChanged(ItemEvent e) {
if (e.getItemSelectable() == cbMale) {
vgender =("Male");
}
if (e.getItemSelectable() == cbFemale) {
vgender = ("Female");
}
...
}
}
marnitzg
Apr 16th, 2003, 02:56 PM
Sorry thought you were using JCheckbox
I think its setState(false) then
mbonfyre
Apr 16th, 2003, 04:05 PM
that didn't work either
CornedBee
Apr 17th, 2003, 06:47 AM
setState
public void setState(boolean state)
Sets the state of this check box to the specified state. The boolean value true indicates the "on" state, and false indicates the "off" state.
Note that this method should be primarily used to initialize the state of the checkbox. Programmatically setting the state of the checkbox will not trigger an ItemEvent. The only way to trigger an ItemEvent is by user interaction.
Parameters:
state - the boolean state of the check box
See Also:
getState()
From the documentation of java.awt.Checkbox
marnitzg
Apr 18th, 2003, 01:43 PM
Ah so my memory hasn't failed me yet ;)
I don't see your actionlistener for jbutclear. Can you post it the way you have implemented it. You might also need to refresh refresh the control by calling myCheckBox.repaint(); straight after you change the state
mbonfyre
Apr 19th, 2003, 03:21 AM
this is what I have that relates to JbutClear:
...
JButton JButSubmit, JButClear, JButDisplay;
...
Icon clear = new ImageIcon("clear.gif");
JButClear = new JButton(clear);
c.ipadx = 12;
c.ipady = 13;
c.gridx = 0;
c.gridy = 14;
c.gridwidth = 1;
c.weighty = 3.0;
gridbag.setConstraints(JButClear, c);
contentPane.add(JButClear);
JButClear.addActionListener(this);
....
public void actionPerformed(ActionEvent e){
if (e.getSource() == JButClear)
{
jtffName.setText("");
jtfstreet.setText("");
jtfsName.setText("");
jtfstreet.setText("");
jtfcity.setText("");
jtfcounty.setText("");
jtfpcode.setText("");
jtfphone.setText("");
jtfurl.setText("");
TADisplay.setText("");
TADisplay2.setText("");
checkFB.setSelected(false); //Deselects checkboxes
checkAer.setSelected(false);
checkTen.setSelected(false);
checkGym.setSelected(false);
checkKar.setSelected(false);
checkHoc.setSelected(false);
cboMonth.setSelectedIndex(0); //Sets to the first object
cboDate.setSelectedIndex(0);
cboYear.setSelectedIndex(0);
}
marnitzg
Apr 19th, 2003, 08:15 AM
Everything looks in order. What you can try is to call checkHoc.repaint(); after you clear it. I don't have java installed anymore so I can't test it for you. It gave way to .net :/
mbonfyre
Apr 19th, 2003, 08:20 AM
Thanks for your help with this, I appreciate it.
Cheers, Sarah
mbonfyre
Apr 19th, 2003, 09:46 AM
Originally posted by CornedBee
mbonfyre: I think you need a JButtWipe variable somewhere. [/B]
Whats the prob with appreciation?
mbonfyre
Apr 20th, 2003, 11:57 PM
:confused:
CornedBee
Apr 21st, 2003, 11:49 AM
Never mind...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.