Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JPanel Ontop for JButton

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel Ontop for JButton

    I want to position a JPanel over a bunch of JButtons in such a way that the JButtons would not be able to be clicked on. Is there a way without having to manually disable each of the JButtons using layers or something? Currently any JPanel I put over the top still allows me to click the buttons underneath.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel Ontop for JButton

    If the JButtons were in an array or a collection that could be easily and quickly iterated, disabling each button is not a big deal. The design you describe - causing the buttons to be hidden or unreachable, covered - is an odd approach that I've never seen implemented.

    If a single action listener is used to process all of the buttons, another approach would be to use an on/off flag in the action listener to enable/disable the action for all buttons as needed.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JPanel Ontop for JButton

    May it be that what you really want is a dialog window popping up? If that is the case look at the JDialog class or the JOptionPane for simple dialogs.

    Other then that you could add your JPanel as the glass pane of your frame and catch any incoming mouse events without redispatching them. In this way all your mouse input would be caught by your glass pane and nothing will get through to the buttons.

  4. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (October 8th, 2014)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel Ontop for JButton

    Ah, good thinking . . . a modal dialog. Check out this page and see if this is what you had in mind or if it helps.

Similar Threads

  1. JPanel within JPanel causing odd stretching behavior.
    By mstabosz in forum AWT / Java Swing
    Replies: 7
    Last Post: September 2nd, 2013, 06:32 AM
  2. JButton Current Location on a JPanel
    By sam91wvu in forum AWT / Java Swing
    Replies: 1
    Last Post: July 9th, 2013, 04:21 PM
  3. Help with NetBeans App using JPanel/JFrame with JButton (if statements)
    By deanbyrne95 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 3rd, 2013, 07:48 PM
  4. Adding JPanel using Jbutton, Expanding JFrame as needed.
    By ShadowMystic in forum AWT / Java Swing
    Replies: 1
    Last Post: February 24th, 2013, 09:13 AM
  5. JButton on JPanel
    By JavaLearner in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:46 AM