How to use a GradientPaint as background color in a JPopupMenu

I want to use a GradientPaint as background color in a JPopupMenu.
I try to use something like
GradientPaint p;
p = new GradientPaint(0, 12, Color.white, 0, 24, Color.pink);
UIManager.put("MenuItem.selectionBackground", p);but it doesn't work.
I also tried to extends BasicMenuItemUI in MenuItems
public class CMenuItem extends JMenuItem {
     public CMenuItem(String text) {
          super(text);
          setUI(BUI.createUI(this));
     static final class BUI extends BasicMenuItemUI {
         private final static BUI ui = new BUI();
         public static BUI createUI(JComponent c) {
             return ui;
         @Override
         public void update(Graphics g, JComponent c) {
               if (c.isOpaque()) {
                    Graphics2D g2 = (Graphics2D)g.create();
                    int w = c.getWidth();
                    int h = c.getHeight();
                    GradientPaint p;
                    p = new GradientPaint(0, 12, Color.white, 0, 24, Color.pink);
                    g2.setPaint(p);
                    g2.fillRect(0, 0, w, h);
                    g2.dispose();
               paint(g, c);
}I used this technique for JLabel and it works fine.
Please help

ok, here the SSCCE. I hope its helpful for an answer.
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicMenuItemUI;
public class Menu {
    public JMenuBar createMenuBar() {
         JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;
        //Create the menu bar.
        menuBar = new JMenuBar();
        //Build the first menu.
        menu = new JMenu("A Menu");
        menuBar.add(menu);
         GradientPaint p;
         p = new GradientPaint(0, 12, Color.white, 0, 24, Color.pink);
         UIManager.put("MenuItem.selectionBackground", p);
        menuItem = new JMenuItem("1st item: when selected its background is black: wrong");
        menu.add(menuItem);
         UIManager.put("MenuItem.selectionBackground", Color.pink);
        menuItem = new JMenuItem("2nd item: when selected its background is pink: ok");
        menu.add(menuItem);
        menuItem = new CMenuItem("3rd item: when selected its background is pink: WRONG!");
        menu.add(menuItem);
        return menuBar;
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Create and set up the content pane.
        Menu demo = new Menu();
        frame.setJMenuBar(demo.createMenuBar());
        //Display the window.
        frame.setSize(450, 260);
        frame.setVisible(true);
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
class CMenuItem extends JMenuItem {
     private static final long serialVersionUID = 1L;
     public CMenuItem(String text) {
          super(text);
          setUI(BUI.createUI(this));
     static final class BUI extends BasicMenuItemUI {
         private final static BUI ui = new BUI();
         public static BUI createUI(JComponent c) {
             return ui;
         @Override
         public void update(Graphics g, JComponent c) {
               if (true || c.isOpaque()) {
//                    System.out.println("update()");
                    Graphics2D g2 = (Graphics2D)g.create();
                    int w = c.getWidth();
                    int h = c.getHeight();
                    GradientPaint p;
                    p = new GradientPaint(0, 0, Color.white, 0, 10, Color.pink);
                    g2.setPaint(p);
                    g2.fillRect(0, 0, w, h);
                    g2.dispose();
               paint(g, c);
}Thanks for the help

Similar Messages

  • How do I apply a specific background color in InDesign?

    The background color is identified in Ps as "edf7e4", and it is a very pale green.  How do I make this the background color for my book cover in InDesign?

    You must add a frame to your cover page(s) at the bottom of the stack and fill it with the color that you wish to use (you can keep the RGB definiton and convert on export, too, if you need CMYK for press). Don't forget to bleed the edges where required.

  • How to change the particular column background  color in jTable?

    I am woking with a project, in which I am using a jTable. Then
    How to change the particular column background color in jTable?

    Use a custom Renderer. This is the JTable tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

  • How to get a layer's background color with AE SDK

    Hello,
    I am trying to get the color of the background for my current layer but I can't seem to find how to do it.
    By background color, I don't mean the application's background color, but the color that shows through if I set the alpha of my pixels to 0.
    For example, suppose the composition background is black, I put a green solid on top of it, then put an image on top of the solid and apply my pug-in to the image.
    If I set the alpha to zero in my plug-in, the solid's green will show up.
    So I want to be able to find out that my background is green.
    I already tried using PF_AppGetBgColor() but that returns the application's background color, which is not green in this example.
    If you want more details about what I want to do, I have plug-in P1 which sets some pixels to 0-alpha. On top of it I apply another plug-in P2 which reveals 0-alpha pixels by blending them with some color.
    What happens now is that the output is the original image's color blended with P2's color (because P1 only modified the pixels' alpha).
    What I want is to have the color showing through the transparent pixels of P1 blended with P2's color.
    To do that, I would like to be able to set the color of the 0-alpha pixels to the background's color in P1.
    Sorry for the long question.
    Thanks for your suggestions.

    hello nicolas.
    if i understood you correctly, what you're trying to get it the image buffer of the composited layers, beneath the layer with your effect.
    if that is correct, then i'm afraid it's next to impossible to do.
    when AE renders a composition it doesn't do it bottom to top. it tries to do the opposite actually.
    why? because if at a certain point the buffer is fully opaque, then it doesn't need to render what's beneath.
    further more, checked out layers change the order rendering, and many other things affect the rendering order of the layers.
    besides that, there's no API for accessing intermediate comp buffers. just the final result.
    the closest you can get to that without turning the world over, is to use AEGP_GetCompBGColor(),
    which would prove useless to you as the image doesn't get composited over that color, unless that's the comp being saved to file.
    so why did i say "next to impossible"?
    you can turn the world around.
    you can write an AEGP of type "artisan". (look at the "arti" sample)
    these plug-ins render comps (instead of the "advanced3D" renderer)
    an artisan does have intermediate comp results, as it is the one doing the rendering.
    so now you can have your artisan save the buffer you need, and deliver it to your effect.
    i must warn you that doing this is very very very difficult.
    another possible solution is to create a duplicate of the comp in which your effect resides,
    erase all layers who's image you don't need, and render that duplicated comp using AEGP_GetReceiptWorld().
    if you use this method, you have to supervise the original comp, and apply changes to the duplicate.
    yes, that too is difficult to do.
    you could make a change in strategy, and apply your effect on an adjustment layer instead.
    the input for the adjustment layer is the composited buffer of underlying layers.
    then you could get the original sources you wish to process using checked out layer params.
    the last solution i can think of is using the sampleImage() expression on a hidden param.
    this will give you pixel data from any layer in the comp, but not the composite of the underlying layers.
    maybe if you tried sampling an adjustment layers post effects... i don't know. you'll have to try.
    this method is very slow, and is also limited to time out, so you can't get a large number of pixels.
    the expression will simply fail.
    i hope it hasn't been all bad news for you.
    maybe of you told more about the plug-in you're developing i could come up with a better plan.

  • How can I create an alt background color for items in a jlistbox or jtree

    What I'm looking to do is have a white background for say items 1,3,5, etc. and a light yellow background for items 2,4,6, etc. I was wondering how can I set an alternative background color on items in a JTree or a JList

    Use a cell renderer:
    import java.awt.*;
    import javax.swing.*;
    public class ListAlternating extends JFrame
         public ListAlternating()
              String[] items = {"one", "two", "three", "four", "five", "six", "seven"};
              JList list = new JList( items );
              list.setVisibleRowCount(5);
              list.setCellRenderer( new MyCellRenderer() );
              JScrollPane scrollPane = new JScrollPane( list );
              getContentPane().add( scrollPane );
         public static void main(String[] args)
              ListAlternating frame = new ListAlternating();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible( true );
         class MyCellRenderer extends DefaultListCellRenderer
              public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                   super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                   if (!isSelected)
                        setBackground(index % 2 == 0 ? list.getBackground() : Color.LIGHT_GRAY);
                   return this;
    }

  • How do I change the tab background color in Firefox 4?

    I just upgraded to Firefox 4. Tabs are now fine black font on dark grey background -- very hard to read. How do I change the tab background color?
    I have searched and the closest I can come is some instructions for modifying .css files, and it is not clear that those instructions will impact the tab background color.
    Thanks in advance for your help.

    Similar question asked by TH received another response.
    Here is the address of that question and answer:
    https://support.mozilla.com/en-US/questions/804464?s=change+tab+color+firefox+4&as=s
    I like the solution recommended there.
    Added information - after several days experience, I notice that some tabs occasionally revert to the original dark grey. So I am still looking for a better solution, ideally within Firefox rather than via add on. This seems like a basic setting for the browser that should be easy to change, rather than something so complex that I need an add on to change it. Or just build it without such a dark grey background to begin with and I would not need to change it.

  • How do I change the white background color in a b/w line drawing jpg?

    How do I change the white background color in a b/w line drawing jpg?

    Just put a Solid Color Layer atop it and set it to Blend Mode Multiply or
    • load the Channel with the best contrast (cmd-click it in the Channels Panel)
    • invert Selection (cmd-I)
    • create a black Solid Color Layer
    • put whatever below that Layer

  • Change background color in a JPopupMenu?

    Hello. I've been trying to change the background color of a JPopupMenu, so far with no success.
    I've tried:
    + invoke setBackground() along with setOpaque()
    + subclass JPopupMenu and provide the following method:
         public void paintComponent(Graphics g) {
              g.setColor(new Color(0,0,0));
              g.fillRect(0, 0,100,100);
              super.paintComponent(g);
         (Ignore the numbers in fillRect() -- I'm just trying to see any effect at all!)
    + change the relevant(??) properties in the UIManager:
         UIManager.getDefaults().put("PopupMenu.background",Color.BLUE);
         UIManager.put("PopupMenu.background",Color.BLUE);
         SwingUtilities.updateComponentTreeUI(c);
         updateUI();
         (and various combinations of similar things).
    OK, none of these seems to have any effect whatsoever. In case it matters, I am using Java 1.4.1 on Mac OSX.
    Thanks heaps for any guidance here.

    JPopupMenu just handles its JMenuItems.
    A JMenuItem is what gets painted, you can
    directly set the background color. When the
    item is selected it gets the LAF color.
    For example, change PopupMenuDemo with
        public void createPopupMenu() {
            JMenuItem menuItem;
         // set selection color before creating the items
         UIManager.put("MenuItem.selectionBackground", Color.pink); // <=== add this
            //Create the popup menu.
            JPopupMenu popup = new JPopupMenu();
            menuItem = new JMenuItem("A popup menu item");
            menuItem.setBackground(Color.red); // <=== add this
            menuItem.addActionListener(this);
            popup.add(menuItem);
            menuItem = new JMenuItem("Another popup menu item");
            menuItem.setBackground(Color.red); // <=== add this
            menuItem.addActionListener(this);
            popup.add(menuItem);
            //Add listener to the text area so the popup menu can come up.
            MouseListener popupListener = new PopupListener(popup);
            output.addMouseListener(popupListener);
        }

  • How can I set the foreground/background color after using CSPickColor()?

    Hello all,
    Is there any one who has experience on changing foreground or background color using Photoshop Plug-in SDK?
    Currently I can get a returned color from CSPickColor(), but have no idea how to use it.
    Thanks!

    You can try a linear-gradient that starts at gray 0% and ends at white <whatever the percentage the thumb is at>.
            final Node track = slider.lookup(".track");
            slider.valueProperty().addListener(new ChangeListener<Number>() {
                public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                    double pct = (t1.doubleValue()-slider.getMin())/(slider.getMax()-slider.getMin())*100;
                    track.setStyle("-fx-background-color: linear-gradient(to right, gray, gray " + pct + "%, white" + pct + "%, white);");
            });Edited by: dgrieve on Apr 12, 2012 12:19 PM

  • The toolbar displays File, Edit, View, etc. print too small & barely able to read. How to enlarge print. Also background color of toolbars hard to read. It is black. make larger?

    The Menu Bar, Roboform Bar in general all toolbars the print is too small and barely able to read. Please tell me how to enlarge the print. Also, the background color is black and hard to read. Thanks for any help you can give me.

    Use this extension to adjust the font size for the user interface:
    *Theme Font & Size Changer: https://addons.mozilla.org/firefox/addon/theme-font-size-changer/
    Increase the String pref layout.css.devPixelsPerPx from 1.0 to 2.0 in 0.1 or 0.05 steps to adjust the size of all fonts and other elements in Firefox.<br />
    In current Firefox versions this pref is a String value parsed to a float and allows to fine tune the dimensions of all elements (user interface and web pages) more precisely.
    *http://kb.mozillazine.org/about:config
    You can use an extension to set a default font size and page zoom on web pages if the above steps made the text too large.
    *Default FullZoom Level: https://addons.mozilla.org/firefox/addon/default-fullzoom-level/

  • How to deal with transparency, variable background color and unsightly edges?

    I am a software developer, only know the Photoshop basics.
    A graphic professional designed for me an icon with transparent background. On some backgrounds it looks fine, but on other backgrounds the edges look bad. I read an Internet article that the problem is that the pixels should merge smoothly into the background. I understand how that workd provided that the background is a known constant (in math terms). My program may run on different versions of Windows, with different window styles (Classic Look, XP Look) and hence the backgrounds are variable.
    How can an icon be designed so it blends smoothly into a background whose color is not known at design time?
    TIA,
    -Ramon

    What version of Windows? If you are just using Vista and W7, you can use 24bit PNGs with proper Alpha channels as your icon resource. I think they prefer straight Alpha, but you may laso be able to use black or medium grey on the RGB channels hen it's premultiplied. (there's a guide somewhere on MS' developers' pages). For XP you are of course stuck with palette-based BMPs/ ICOs, which is no different than when creating GIFs or 8bit PNGs in PS. Generally, though, you may wish to work with a dedicated editor like Axialis Icon Workshop, especially when needing to create multiple versions of an icon. The rest is out of your hands. Some icons will always look frizzled, if the user applies an all too wild color scheme. It's just how it is....
    Mylenium

  • How do I get rid of background color in shortdesc

    Every time I remove the background color from the short.description paragraph tag, it comes back when I reopen FrameMaker. I don't want any background colors in my short descriptions.

    Ah .. FM10 and DITA 1.2 .. too bad, you're in for a bit of a chore.
    The 1.2 apps are here .. AdobeFrameMaker10\Structure\xml\DITA_1.2\
    Unfortunately, there are lots of them, and it's not immediately clear how to approach things. Opening an XML file in Frame requires a structured application in order to define the valid document model and to apply the proper formatting. The formatting is applied by an EDD, which defines the mapping from elements and associated styles which live in the template (the EDD itself is embedded in the template as well). The structured applications are defined in the structapps.fm file (of which there are two). One of these can be seen when you choose the Structure Tools > Edit Application Definitions file (this lives in the user's "app data" area), but another is at AdobeFrameMaker10\Structure\structapps.fm (this is the "core" structapps file). The two files define the available structure applications, any definitions in the user's file override those in the other file (I'm pretty sure).
    So .. when you open an XML file, Frame scans the structapps files for matching doctypes, if only one is found, that app is used. If multiple matching doctypes are found, you are presented with a dialog that lets you choose from the available apps. If you didn't select an app when opening the file, you'll need to look in the structapp files to see which one was used for your file. The root element in your file defines the doctype (as well as the Public ID). If this is a "task" topic, you'd look in the structapp files for matching apps that support the "task" doctype (if you're using DITA 1.2 you'll be looking for a DITA 1.2 task .. I'm not totally clear on how FM differentiates between the two). In my installation the DITA 1.2 definitions are in the core structapps file, but it could be in either place (or both).
    Once you locate the structure app definition, it will have nodes that define the Template, the DTD, the ReadWriteRules, and other things. You're just looking for the template though. If you're using a "task", for DITA 1.2 that's likely to  be the DITA_1.2_task structure application .. the template is located at ..
         $STRUCTDIR\xml\DITA_1.2\app\technicalContent\template\task.template.fm
    where "$STRUCTDIR" is the location of the FrameMaker\Structure folder (or possibly defined differently in the maker.ini .. but that's a different issue).
    Editing this one file will affect the formatting of future task topics you open. You'll have to do the same thing in the other corresponding topic type templates to affect all of the topics.
    In previous versions of Frame, it used the "ditabase" model for authoring and publishing. This encapsulates all of the models in one file making it much easier to maintain. This model is more parallel to the DITA model, but will be a real nightmare for people to customize. I'm not going to begin to try and explain how you'd clone these apps to make your own, although you can read a document written for DITA-FMx and DITA 1.1 that gives you the fundamental process ..
         http://docs.leximation.com/dita-fmx/1.1/?ditafmx_cloningtapps.html
    I'm hoping that I've explained this correctly .. I'm still trying to wrap my head around it myself.
    Cheers?
    ...scott

  • How to change the current item background color

    I m working in forms 6.0.
    I want to mak the background color of the current item (whatsoever the item name is ) as 'yellow'. How can i do that.How can i accoplish that? I've tried
    set_item_property(:system.cursor_item,background_color,'yellow');
    in Pre_text_item
    set_item_property(:system.cursor_item,background_color,'white');
    in post_item_item
    at block level
    But i failed to do that
    anyone to help me plz
    Riaz Shahid

    hi riaz,
    i think instead of pre-text-item put the code in when new item instance.or try creating a visual attribute with background color as yellow and attach that using set_item_property in when new item instance or check for help on DISPLAY_ITEM built in in forms help.I think one of these should definitely help u.
    bye
    Atul

  • HT201726 How to set again your windows background color

    I  used to have a pleasant blue color as a background for my files, documents, applications, but when I looked for a file and clicked on the screen the color was gone becoming a white background.
    I used to see the view options but now it has the point on one selection and cannot click on the color to change it back again. Any solutions and/or suggestions on how to do it? Thank U.

    I';ll try to make it as clear as I can to cover all bases. I set before the blue color I wanted, it changed once I update the OS System to Maverick.
    When I open the finder window on the left column appears 8 (eight) icons, another one for shared plus other for tags.
    All the first 8 used to have the blue color set. Now only iCloud, Desktop, and Ilabra (home). All my files, documents and applications the background used to be blue they turned White..
    ON the three options of White, Color or Picture, The selection appears on White and the other two are shaded. Before the View had the three option when I applied the blue color. Now I can not, and I do not know why this occurred.
    I hope I have been clear with the explanation. Any more suggestions? I will certainly appreciate them and your time and patience.

  • How do I change the workspace background color?

    Hi all,
    I need to change the workspace background color from the
    default mid grey. But I can't find how to do this!
    Any ideas!
    Thanks in advance, Pedro.

    Sorry, but there's no way to change it from the default gray.
    Linda Rathgeber [PVII] **Adobe Community Expert-Fireworks**
    http://www.projectseven.com
    Fireworks Newsgroup:
    news://forums.projectseven.com/fireworks/
    CSS Newsgroup: news://forums.projectseven.com/css/
    Design Aid Kits:
    http://www.webdevbiz.com/pwf/index.cfm

Maybe you are looking for