how would I go about resetting combo boxes/radio buttons or checkboxes at a button click?
Code:if (e.getSource() == JButClear)
{
name.setText(" ");
age.setText(" ");
}
Printable View
how would I go about resetting combo boxes/radio buttons or checkboxes at a button click?
Code:if (e.getSource() == JButClear)
{
name.setText(" ");
age.setText(" ");
}
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.Code: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
}
Hope that helps
Marnitz
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?
What jdk are you using?
Can you post your declarations for cbMale and cbFemale
jdk 1.3.1
Code: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");
}
...
}
}
Sorry thought you were using JCheckbox
I think its setState(false) then
that didn't work either
From the documentation of java.awt.CheckboxQuote:
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()
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
this is what I have that relates to JbutClear:
Code:
...
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);
}
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 :/
Thanks for your help with this, I appreciate it.
Cheers, Sarah
Whats the prob with appreciation?Quote:
Originally posted by CornedBee
mbonfyre: I think you need a JButtWipe variable somewhere. [/B]
:confused:
Never mind...