Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


How to customize the JMenu or any component filter_list
Author
Message
How to customize the JMenu or any component #1
Hello [username],

I want to know how to customize a component in java. I know that I need to extend it and then paint in in the way I want.

I want to have a JMenu whose hackground is a different color and the font text is different color.

I have already done something like this with JMenuBar :-

Code:
package com.psychocoder.ide;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JMenuBar;

public class MenuBar extends JMenuBar{
    
    private static final long serialVersionUID = -1251333663922218277L;
    
    public MenuBar(){
        setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);    
    }    
    
    @Override
    protected void paintComponent(Graphics g){        
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(146, 0, 0));
        g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);        
    }
    
}

and JMenu

Code:
package com.psychocoder.ide;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JMenu;

public final class Menu extends JMenu{

    private static final long serialVersionUID = -5687538740176278239L;
    
    public Menu(String name){
        super(name);
    }    
    
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setBackground(new Color(52,52,52));
        setForeground(Color.WHITE);
        setIconTextGap(8);
        setToolTipText("<html>" +
                "<body bgcolor=black>" +
                "<font color=#ffffff>Click to Open</font>" +
                "</body>"+
                "</html>");
    }    
}


you can use the above code and see the simple effect.

I want to know how to customize them further like changing the action listeners property or change the colour of foreground onMouseHover or setting the gradients and setting some kind of animations. I want to know if I want to create my own components and different shapes and events then how can this be done. I haven't worked with customizing components much so I don't have good idea.

One way to do the customization is this but I doubt whether this is efficient or not :-

Code:
UIManager.getLookAndFeelDefaults().put("Menu.foreground", Color.WHITE);


EDIT : I want to set the background of the tool tip to black. The edited code above does show the tool-tip with black ground but a think border is shown as well, how to get rid of that. Also is there a way to make it dynamic like here I have written from before hand "Click to open", what if I want to show the text on the component in the tooltip.

To do this I think I need to inherit the JTooltip and then paint it in my way, but I haven't tried it yet.
[Image: OilyCostlyEwe.gif]

Reply

RE: How to customize the JMenu or any component #2
I usually use the Nimbus Look and Feel and change it to my likings. I.e. these are the settings for the HC theme:

Code:
private static void setLookAndFeel() {
        UIManager.put("nimbusBase", new Color(15, 0, 0));
        UIManager.put("nimbusBlueGrey", new Color(170, 0, 0));
        UIManager.put("control", Color.black);
        UIManager.put("text", Color.white);

        UIManager.put("nimbusSelectionBackground", Color.gray);
        UIManager.put("nimbusSelectedText", Color.white);
        UIManager.put("textHighlight", Color.lightGray);
        UIManager.put("nimbusFocus", new Color(170, 0, 0));
        UIManager.put("nimbusSelection", new Color(170, 0, 0));

        UIManager.put("textBackground", Color.black);
        UIManager.put("nimbusLightBackground", Color.black);

        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e) {
                    System.err.println(e.getMessage());
                }
                break;
            }
        }
    }

Read more about it here: http://docs.oracle.com/javase/tutorial/u...ustom.html
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: How to customize the JMenu or any component #3
(10-11-2013, 08:08 PM)Deque Wrote: I usually use the Nimbus Look and Feel and change it to my likings. I.e. these are the settings for the HC theme:

Code:
private static void setLookAndFeel() {
        UIManager.put("nimbusBase", new Color(15, 0, 0));
        UIManager.put("nimbusBlueGrey", new Color(170, 0, 0));
        UIManager.put("control", Color.black);
        UIManager.put("text", Color.white);

        UIManager.put("nimbusSelectionBackground", Color.gray);
        UIManager.put("nimbusSelectedText", Color.white);
        UIManager.put("textHighlight", Color.lightGray);
        UIManager.put("nimbusFocus", new Color(170, 0, 0));
        UIManager.put("nimbusSelection", new Color(170, 0, 0));

        UIManager.put("textBackground", Color.black);
        UIManager.put("nimbusLightBackground", Color.black);

        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e) {
                    System.err.println(e.getMessage());
                }
                break;
            }
        }
    }

Read more about it here: http://docs.oracle.com/javase/tutorial/u...ustom.html

I know about these and I have read the theory on that page earlier. http://docs.oracle.com/javase/tutorial/u...ml#primary

The above link has almost all the details of the default settings that can be made. I want to know if the event can be tweeked for these components.

Okay. I understand.
[Image: OilyCostlyEwe.gif]

Reply

RE: How to customize the JMenu or any component #4
(10-11-2013, 09:07 PM)Psycho_Coder Wrote: I want to know if the event can be tweeked for these components.

Can you rephrase your question?
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply







Users browsing this thread: 1 Guest(s)