Results 1 to 11 of 11
  1. #1
    Ashe is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default need help making hidden JButton

    Hello im new here and i just want to ask how to make an invisible jbutton where you still click it even though it cannot be seen

    i already tried
    button.setVisible(false);
    button.hide();
    it makes the button invisible although i cannot click it anymore

    hoping for help here and thanks in advanced
    cheers

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

    Default

    What functionality are you trying to achieve? If you just want a clickable location, why not just use a MouseListener?

  3. #3
    Ashe is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    basically i want to make like a hidden button wherein when its clicked another window will pop out

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

    Default

    Quote Originally Posted by Ashe View Post
    basically i want to make like a hidden button wherein when its clicked another window will pop out
    Again, why must this be a button? If the user doesn't see anything, why not just use a MouseListener? We'll be much better able to help you if you can provide the information we need or request.
    Last edited by Fubarable; 03-12-2011 at 03:50 PM.

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

    Default

    Do you understand what I mean about using a MouseListener? I assume that you don't want a button that can be seen or that even "clicks" but you want a region of the GUI that responds to mouse presses, and a MouseListener can fit that bill. It can be added to any component, it is not seen, it can be coded so that it only responds to clicks in a certain specified region, and in the mousePressed method of the MouseListener, you can put in the code that you otherwise would have placed in your JButton's ActionListener's actionPerformed method. You can learn more about these at the Oracle Swing tutorial: How to Write a Mouse Listener
    Last edited by Fubarable; 03-12-2011 at 04:05 PM.

  6. #6
    Ashe is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Oh ok thanks, one last question, how to do make a certain "region" , it would be too hard if it was just one certain point

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

    Default

    Quote Originally Posted by Ashe View Post
    Oh ok thanks, one last question, how to do make a certain "region" , it would be too hard if it was just one certain point
    You're welcome. The easiest way would be to add it to a single component that covers the region of interest. If you can't do this, then you would still add the listener to a single component, but in the mousePressed method, you would only respond if the mouse button coordinates are where you want them to be. So you would get the Mouse location from the MouseEvent object passed into the method's parameter (note that the location is relative to the component that has the listener -- i.e., 0, 0 is at the top left of the component) and use an if block to see if the mouse is where you want it. Give it a try and if you get stuck, come on back with your code, preferably an SSCCE.

  8. #8
    Ashe is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    I finally did it thanks a lot really appreciate your help in helping a beginner like me, I just used if statements to get the part like this one

    if(e.getX()>=230 && e.getX()<=580 && e.getY()>=310 && e.getY()<=380){

    again thanks for the reply, i can finally move on to the next part XD

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

  10. #10
    AndrewC is offline Member
    Join Date
    Apr 2012
    Location
    Ireland
    Posts
    5
    Rep Power
    0

    Default Re: need help making hidden JButton

    Hey,

    This seems to be the only post about this and although a mouse listener is a better option I thought I would answer this.
    I wanted to make a JButton hidden for my project instead of a mouse listener just to keep my code clean.
    If i remember you must implement 5 or 7 mouse listener methods.
    My solution is as follows:

    In paint I created an image the exact size of my JButton and color of the background JPanel, which for the windows look and feel was:

    Hue: 160
    Red: 238
    Green: 238
    Blue: 238
    Lum: 224
    Sat: 0

    This was judged but looks right.
    I then used the setIcon method to set this image and setBorder(null) to remove the black border.

    JButton is now completely invisible even when clicked and still has functionality.

    Hope this helps people I used this to have a sneaky way to enter admin mode in my program.

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: need help making hidden JButton

    Someone asked a similar question on StackOverflow a long time ago. One suggestion was to extend JButton and override paint() so it does nothing.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Android Run in Hidden mode
    By Blackberrylerner in forum New To Java
    Replies: 1
    Last Post: 03-04-2011, 07:14 PM
  2. password with hidden characters and 3 attempts
    By khay in forum New To Java
    Replies: 7
    Last Post: 12-05-2010, 02:44 PM
  3. hidden columns in jxtable
    By new2java2009 in forum New To Java
    Replies: 0
    Last Post: 09-27-2010, 10:13 PM
  4. [SELENIUM] Retrieve the value of a hidden input type
    By gecko753 in forum Web Frameworks
    Replies: 1
    Last Post: 11-04-2009, 03:32 PM
  5. Hidden Button after paint()
    By MuslimCoder in forum New To Java
    Replies: 5
    Last Post: 09-14-2009, 05:52 PM

Posting Permissions

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