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 6 of 6

Thread: How to change color of JMenuItem when mouse is over it

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to change color of JMenuItem when mouse is over it

    Title says it all, I want to change background and foreground color's of JMenuItem so that normal background is blue and when I put my mouse over it, it turns to green. once I move my mouse away it moves back to blue. I tried with Mouse Listener and with MouseDraggedListener and neither have worked.

    If there is a solution please let me know.

    Also another question is a similar one involving tabs. How do I change background and foreground of selected tab. I managed to change background of selected tab with this command

    UIManager.put("TabbedPane.selected", Color.Green);

    but can't seem to change the Foreground.

    Any help is appreciated

    Regards,


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to change color of JMenuItem when mouse is over it

    I'm just thinking out loud here, but does any generic JMenuItem react in any way when you just hover your mouse over it (highlights or something)? If so, that would be an indicator that JMenuItem has a built in listener to process a mouse hovering over it. If it has a built in listener by default, we just need to figure out what type of listener it is an override it to do what you want.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to change color of JMenuItem when mouse is over it

    When mouse hovers over background color and foreground color change. Text becomes black, background becomes very light_blueish. This is the default and I have no idea how to overwrite that. With mouse listeners I tried using print statements but nothing happened.


    Edit: Ok using mouse listener I managed to get color to change, I added so that color changes when mouse enters. While mouse hovers color is the same old but when mouse moves away it goes to green. So i'm assuming I need to over-wright hover over mouse function somehow, any ideas?
    Last edited by Bagzli; April 2nd, 2012 at 12:16 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to change color of JMenuItem when mouse is over it

    If you wish this to apply to all JMenuItems across your application, use the UIManager to set the look and feel defaults for MenuItem.selectionBackground and MenuItem.background.
    ...otherwise, JMenuItem extends JComponent so anything applies there (such as add listeners and setting its background/foreground)

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to change color of JMenuItem when mouse is over it

    Solution:

    UIManager.put("TabbedPane.selected", Color.GREEN);
    		UIManager.put("MenuItem.selectionBackground", Color.GREEN);
    		UIManager.put("MenuItem.selectionForeground", Color.BLUE);
    		UIManager.put("Menu.selectionBackground", Color.GREEN);
    		UIManager.put("Menu.selectionForeground", Color.BLUE);
    		UIManager.put("MenuBar.selectionBackground", Color.GREEN);
    		UIManager.put("MenuBar.selectionForeground", Color.BLUE);


    Hope this helps people who search for same problem, just note that you have to implement UIManager before you initialize your JMenu and its components.

    On final note what would the command be to change TabbedPane selected Foreground? I couldn't figure that one out.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to change color of JMenuItem when mouse is over it

    Quote Originally Posted by Bagzli View Post
    On final note what would the command be to change TabbedPane selected Foreground? I couldn't figure that one out.
    For future reference, you can iterate over the UIDefaults to check the UI options available and search for the specific key you need to set the UI default value

    UIDefaults ui = UIManager.getLookAndFeel().getDefaults();
     
    for ( Object o : ui.keySet() ){
    	System.out.println(o.toString());
    }

    The above code will print the keys available, which you can then search for the appropriate syntax to use for the value you need

Similar Threads

  1. Re: How to Change JTextArea font, font size and color
    By binokyo10 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 5th, 2012, 12:12 PM
  2. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  3. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 10:57 AM