Results 1 to 16 of 16
  1. #1
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default Removing a JButton

    How would I remove a JButton from the screen after it's clicked? I've tried:

    Java Code:
    	stButton = null;
    		remove(stButton);

    But it seems all that does is stop it listening, as the button stays in place but no longer reacts when clicked. I'd like to remove the button completely if possible.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Try removing it from it's parent container e.g
    Java Code:
    jPanel.remove(stButton);

  3. #3
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Removing it that way gives:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Are you setting the JButton to null before removing it? That would explain the null pointer... Remove the JButton first, then set it to null if you like

  5. #5
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    I've tried various ways, all of which give me that exception. Each time it says it's the remove(stButton) that's causing it, but I can't see how. :/

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    It would help to see more of the code

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't call it on a null JPanel.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by StormyWaters View Post
    It would help to see more of the code
    Indeed. There's no way we can help given the information provided.

  9. #9
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    What more do I need to provide?

  10. #10
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    How about the classes involved, unless they are too big.

    Try creating a small example application which demonstrates your problem

  11. #11
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Make sure the JPanel isn't null.
    Make sure the button isn't null
    Make sure the button exists on the panel.
    I die a little on the inside...
    Every time I get shot.

  12. #12
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    I'm sure the JPanel isn't null, nor is the button, and as far as I can tell the button exists on the panel. Is there any way to check this?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sure the JPanel isn't null, nor is the button
    The JVM isn't lying. Something is null. Period.


    Is there any way to check this?
    Sure, just call something like:
    Java Code:
    System.out.println("Is myJPanel null: " + (myJPanel == null));

    Better still: show us compilable code that demonstrates the problem.

  14. #14
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Hmm, it's fixed now. Calling remove from JFrame successfully removes it.

    Thanks for providing assistance with this everyone. :)

  15. #15
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Now tell us what the problem was ;) And the solution. So other people can read it and help theirselfes. :)
    I die a little on the inside...
    Every time I get shot.

  16. #16
    Lingerz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    The problem was I was attempting to remove the JButton from the JPanel alone. I solved it by removing it from the JFrame.

    (JFrame f)

    Java Code:
    	f.remove(stButton);

    Thanks again for your help guys. :D If you have anything to add, please do. :)

Similar Threads

  1. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 02:03 PM
  2. Removing Buttons?
    By Mass Zero in forum Java Applets
    Replies: 10
    Last Post: 10-28-2009, 04:16 PM
  3. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 08:23 AM
  4. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 06:56 PM
  5. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 12:46 PM

Tags for this Thread

Posting Permissions

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