1

as I said in other posts I'm new in Java and I'm having some dumb problems, here's the deal:

I have a radioButton (radioStock) and a textField (stockField). I want stockField to be setEnabled(false) by default, no problem with that, and whenever the radioStock is checked, set the stockField enabled on true. I wrote this code, but it doesn't work.

if (radioStock.isSelected()) {
    stockField.setEnabled(true);
}else{
    stockField.setEnabled(false);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Agustín
  • 1,546
  • 7
  • 22
  • 41

2 Answers2

5

That code needs to be in a listener that is attached to the JRadioButton such as an ActionListener or ItemListener. And you don't even need the if blocks since all you'd need is one line of code inside of the listener:

  radioStock.addItemListener(new ItemListener() {

     @Override
     public void itemStateChanged(ItemEvent itemEvent) {
        stockField.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
     }
  });

For more on use of JRadioButtons, please check out the tutorial: button tutorial.


Edit my SSCCE

import java.awt.event.*;
import javax.swing.*;

public class ItemListenereg {
   private static void createAndShowGui() {
      final JRadioButton radioStock = new JRadioButton("Stock", true);
      final JTextField stockField = new JTextField(10);
      JPanel panel = new JPanel();
      panel.add(radioStock);
      panel.add(stockField);

      radioStock.addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent itemEvent) {
            stockField.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
         }
      });

      JOptionPane.showMessageDialog(null, panel);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I pasted that but still not working! Do I need to change anything else? – Agustín Jul 26 '13 at 20:53
  • 3
    @Agustín: never cut and paste code blindly. Use the ideas that it contains to augment your own program. Read the tutorial that I've provided a link for. Think, then code. Then if you're still stuck, create and post a minimal program that shows just a JRadioButton and a JTextField and nothing else, and shows your problem, an [sscce](http://sscce.org). I'll post an sscce example above to show you what I mean. – Hovercraft Full Of Eels Jul 26 '13 at 20:56
0

This should work

    radioStock.addActionListener(new ActionListener() 
    {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
         if(radioStock == e.getSource()) 
         {
            stockField.setEnabled(radioStock.isSelected());
         }
       }
    });
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
  • 1
    why the `if (radioStock...` if this is an anonymous inner class? How would the source be anything else? – Hovercraft Full Of Eels Jul 26 '13 at 20:58
  • Why should it work? What principles should the OP paying attention to? Blindly throwing code at the OP is why they're having problems. Sometimes we need to teach how to to fish over just forcing fish upon them ;) – MadProgrammer Jul 26 '13 at 21:18