Results 1 to 15 of 15

Thread: resetting components ...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    resetting components ...

    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(" ");		
    			
    		}

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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
    }
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    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?

  4. #4
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    What jdk are you using?
    Can you post your declarations for cbMale and cbFemale

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    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");
    	
    		}
    ...
                     }
              }

  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Sorry thought you were using JCheckbox
    I think its setState(false) then

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    Unhappy

    that didn't work either

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    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);		
    		}

  11. #11
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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 :/

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    Thanks for your help with this, I appreciate it.
    Cheers, Sarah

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    Originally posted by CornedBee

    mbonfyre: I think you need a JButtWipe variable somewhere. [/B]
    Whats the prob with appreciation?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Never mind...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width