Results 1 to 4 of 4
  1. #1
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default Removing Items In a GUI

    So I know that if you remove something, you would remove('obj'); revalidate(); repaint();

    What I'm doing wrong is this:

    Java Code:
    JButton button = new JButton("Remove Me");
    JButton.setBounds(200, 200, 100, 20); // Layout set to null, this is required
    add(button);
    button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
               remove(this);
               revalidate();
               repaint();
         }
    });
    Now this doesn't work. If I do something like

    Java Code:
    private JButton button;
    and use

    Java Code:
    remove(button);
    This works fine. Can you explain to me why the remove(this); doesn't work. I know I'm not using this properly, I just don't know exactly what about it is wrong and how I can get it to work correctly.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Since "this" refers to the class you are currently in when you are calling
    Java Code:
    remove(this)
    you are trying to remove the ActionListener anonymous inner class, not the JButton.


    To remove the JButton, you must call
    Java Code:
    remove((JButton)event.getSource());

    edit: please note that you never replied to answer given to you in your previous thread. Since none of us are paid to help out here, our payment is your thanks, and we always appreciate replies to our posts, even if it's nothing more than a simple acknowledgment that you've read our posts. I'm sure you understand why this is important. Thanks for your cooperation.
    Last edited by Fubarable; 08-01-2010 at 04:58 AM.

  3. #3
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default

    Alright, that worked fine. I think my problem was that I was thinking jQuery and not Java.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by 67726e View Post
    Alright, that worked fine. I think my problem was that I was thinking jQuery and not Java.
    You're welcome and glad it helped. I'm not familiar with jQuery, so I'll have to take a look at it sometime soon. Good luck.

Similar Threads

  1. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 12:48 PM
  2. Removing Buttons?
    By Mass Zero in forum Java Applets
    Replies: 10
    Last Post: 10-28-2009, 03:16 PM
  3. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM
  4. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 05:56 PM
  5. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 11:46 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
  •