Results 1 to 8 of 8

Thread: ActionListener

  1. #1
    javalad90 is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default ActionListener

    Hi guys, I have created a sample GUI for an estate agency.

    If for example a user presses a Radio Button ("More Than One House") it displays a text box in which they can enter a number. The issue I is have, if they press it it shows up the text box, but if I un-click it the text box still shows.

    public void actionPerformed(ActionEvent e) {

    if(e.getSource()==rdbtnMoreThenOne){
    textField.setVisible(true);

    }

    My code above.^ I would appreciate it if someone could tell me how to fix the issue or if I have gone wrong somewhere. Thanks for advance :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: ActionListener

    You need to submit an Short, Self Contained, Correct Example to demonstrate the problem. Otherwise, we are only guessing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    javalad90 is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: ActionListener

    Hey Jim, thanks for the reply, this will sound silly but what do you want me to include?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: ActionListener

    The smallest possible bit of code which demonstrates the problem and can be compiled and run.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    javalad90 is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: ActionListener

    rdbtnMoreThenOne = new JRadioButton("More Then One House?");
    rdbtnMoreThenOne.setBounds(6, 7, 137, 23);
    contentPane.add(rdbtnMoreThenOne);
    rdbtnMoreThenOne.addActionListener(this);

    textField = new JTextField();
    textField.setBounds(6, 42, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);
    textField.setVisible(false);

    }

    public void actionPerformed(ActionEvent e) {

    if(e.getSource()==rdbtnMoreThenOne){
    textField.setVisible(true);

    }


    I have a textfield in which a user can enter an number. I have set the visibility as false. When they click on the radio button its should appear which is does. However of you unclick it it remains visable.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: ActionListener

    This is not compilable. You need the class def(s), the main method, the JFrame, JPanel, etc, etc. Everything which demonstrates the problem and compiles. Probably shouldn't take more than 30 lines of code. If I can't paste it into a file and compile it with no errors, then it does not qualify as an SSCCE.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    javalad90 is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: ActionListener

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;


    public class House1 extends JFrame implements ActionListener{

    private JPanel contentPane;
    private JTextField textField;
    private JRadioButton rdbtnMoreThenOne;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    House1 frame = new House1();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public House1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    rdbtnMoreThenOne = new JRadioButton("More Then One House?");
    rdbtnMoreThenOne.setBounds(6, 7, 137, 23);
    contentPane.add(rdbtnMoreThenOne);
    rdbtnMoreThenOne.addActionListener(this);

    textField = new JTextField();
    textField.setBounds(6, 42, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);
    textField.setVisible(false);

    }

    public void actionPerformed(ActionEvent e) {

    if(e.getSource()==rdbtnMoreThenOne){
    textField.setVisible(true);

    }


    }
    }

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: ActionListener

    Perfect SSCCE. You should probably add code tags in the future to make it more presentable. Got to advanced editor. Highlight code, and click on #.

    Also, it is better to extend JPanel and add to an instance of JFrame.

    This should help with your problem.

    Java Code:
    if (e.getSource() == rdbtnMoreThenOne) {
             JRadioButton rb = (JRadioButton) rdbtnMoreThenOne;
             if (rb.isSelected()) {
             textField.setVisible(true);
             } else {
                textField.setVisible(false);
             }
          }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. add an actionListener
    By mixpro in forum AWT / Swing
    Replies: 4
    Last Post: 12-21-2013, 03:02 PM
  2. Actionlistener q
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 09:17 PM
  3. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 04:59 PM
  4. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 07:05 PM
  5. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 08:43 AM

Posting Permissions

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