Background Changing in JPanels

Hi ! I really need fast help on this //Its my first post BTW.
Anyway, i have a window and i want to enable the user to switch its background at will.
The default background is "Black". The problem is, after choosing the new background, the window stays black and only changes after i resize the window, after that, it doesn't change anymore (even resizing). I wont paste the whole code, just the part that matters!
//previous code...
//this is programmed on my constructor
JMenu back  = new JMenu("Background         "); viewMenu.add(back);        viewMenu.addSeparator();
back1 = new JMenuItem("Background 1"); back1.addActionListener(g); back.add(back1);
back2 = new JMenuItem("Background 2"); back2.addActionListener(g); back.add(back2);
back3 = new JMenuItem("Background 3"); back3.addActionListener(g); back.add(back3);
back4 = new JMenuItem("Background 4"); back4.addActionListener(g); back.add(back4);
back5 = new JMenuItem("Background 5"); back5.addActionListener(g); back.add(back5);
//more code....
private class EventManager implements ActionListener
{ public void actionPerformed(ActionEvent e)
JLabel gish = new JLabel("");
if(e.getSource() == back1)
{ gish.setIcon(new ImageIcon("1.jpg"));
center.add(gish); center.repaint();}  //center is a JPanel, its programmed like this:
//                            center = new JPanel(); center.setBackground(Color.BLACK);
if(e.getSource() == back2)  //& then added on my frame like this:
{ gish.setIcon(new ImageIcon("2.jpg"));  //frame.add(center, BorderLayout.CENTER);
center.add(gish); center.repaint();}
if(e.getSource() == back3)
{ gish.setIcon(new ImageIcon("3.jpg"));
center.add(gish); center.repaint();}
if(e.getSource() == back4)
{ gish.setIcon(new ImageIcon("4.jpg"));
center.add(gish); center.repaint();}
if(e.getSource() == back5)
{ gish.setIcon(new ImageIcon("5.jpg"));
center.add(gish); center.repaint();}
}Tkx in advance!!!

mpmarrone wrote:
Where's the part where you set the background? To quote the OP "I wont paste the whole code, just the part that matters!"
1) Nobody wants your 'whole code'. Especially if it is as poorly indented and formatted as what you posted. What is generally recommended is for you to prepare and post an SSCCE *(<- link)* that demonstrates the problem.
2) If you don't know the solution, how can you possibly know what 'matters' and what does not?
You might get the effect you are after by calling repaint().
If not, I suggest you prepare an SSCCE that has only one code statement per line, and uses one of the two standard forms of bracketing and indenting. Then post that SSCCE to the forum.

Similar Messages

  • My Mac's background changed to white and i can't change it back

    Dear anybody,
    Yesterday I plugged in my external harddisk, and I just moving some folders from one partition to another. then in the middle of progress, it opened an error window and the external hd disappeared from my desktop icon and finder. I tried to plugged out and plugged in again but it's not recognized until I restarted it. and the process of restarting takes about 5minutes (it's not usual since I just need about 30-60secs to reboot my mac). then I logged in and my background changed to white. I thought it was missing my bg pict, then I searched for the picture and set it again as desktop background but nothing happened. it's still white. but the strange is when I press f9 or f10 button (expose), I saw the picture on my background and a lot of windows exposed, then when I click one of the window, it disappeared again, changed to white.
    so, please anybody who had ever gotten this kinda problem, help me out. Thanks for your attention!
    *Leopard-10.5

    ''david.abel wrote:''
    On the Firefox home page, the Arabic language has suddenly started to appear
    Is the language for the menus and other user interface elements still English? If so,
    # Click the ≡ Menu Button and choose Preferences.
    # Click the Content icon.
    # Below Languages, click the Choose button.
    # Make sure a specific variant of English like ''en-us'' is at the top, followed by ''en''. Remove any unwanted languages as well.
    [[Settings for fonts, languages, pop-ups, images and JavaScript|Settings for fonts, languages and pop-ups]]
    ''david.abel wrote:''
    Also, my browser history has started to take up part of the screen.
    I don't know what you mean by that. Please attach a screenshot.
    * [[How do I create a screenshot of my problem?]]

  • Background Change Picture Feature Not Working

    I have had my macbook pro for about 6 months now and my change background feature worked fine when I first got it. I could add folders of pictures and tell it change ever 5 seconds and it worked. Over time, I messed with my dock settings and background settings with other third party programs and stuff and decided I didn't like them so I would just go back to the features built into Leopard. I tried to do a random, change every 5 seconds background change folder deal the other day and it never would change. I tried doing it on the default folders like the apple images and the nature ones and stuff but it would never change. I can still add folders. I can click the option of random order and change every x times. When I close out my system preferences, it resets what I did. I go back to my background panel and they are unchecked. How can I fix this and is anyone else having this problem?

    delete the file com.apple.desktop.plist from homedirectory/Library/Preferences and log out/in. You background settings should be set to defaults and you should be able to change them as you like.

  • Background image for JPanel

    I was to come here and "search" for examples on this but i havnt found any so im jsut going to post it:
    How do i set an image to be the background of a JPanel? setBackground(image); doesnt work... i was toal something about paintComponent() but i tried it with that too and it didnt work... anyhelp would be greatly appreciated

    Ive tried many of the things that search suggested but none of them pertained enough to what i am doing for it to work. Here is my code:package gameFunctions;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class loginScreen extends JFrame implements ActionListener
         JTextField USERNAME, Username, PASSWORD, Password; // yeah i know, bad practice but there is some logic to the names relative to capitalization
         String username, password;
         JButton LOGIN;
         Dimension res = Toolkit.getDefaultToolkit().getScreenSize();
         ImageIcon background = new ImageIcon("C:\\FileTesting\\testImage.jpg");;
         JPanel pane;
    public static void main(String[] args)
              loginScreen frame = new loginScreen();
              frame.setVisible(true);
    public loginScreen()
              Container c = getContentPane();
              setSize(400, 300);
              setLocation((int)res.getWidth()/2-getWidth()/2, (int)res.getHeight()/2-getHeight()/2);
              setResizable(false);
              setLayout(null);
              pane = new JPanel();
              pane.setSize(getWidth(), getHeight());
              pane.setLayout(null);
              c.add(pane);
              USERNAME = new JTextField("Username:");
              USERNAME.setSize(68,25);
              USERNAME.setLocation(getWidth()/2-USERNAME.getWidth()/2-80, getHeight()/2-USERNAME.getHeight()/2-50);
              USERNAME.setEditable(false);
              pane.add(USERNAME);
              PASSWORD = new JTextField("Password:");
              PASSWORD.setSize(68,25);
              PASSWORD.setLocation(USERNAME.getX(), USERNAME.getY()+30);
              PASSWORD.setEditable(false);
              pane.add(PASSWORD);
              Username = new JTextField();
              Username.setSize(150,25);
              Username.setLocation(USERNAME.getX()+USERNAME.getWidth()+5, USERNAME.getY());
              Username.setEditable(true);
              pane.add(Username);
              Password = new JTextField();
              Password.setSize(150,25);
              Password.setLocation(Username.getX(), Username.getY()+30);
              Password.setEditable(false); // because im not working with PW's yet
              pane.add(Password);
              LOGIN = new JButton("LOGIN");
              LOGIN.setSize(80,30);
              LOGIN.setLocation(getWidth()/2-LOGIN.getWidth()/2, PASSWORD.getY()+30);
              pane.add(LOGIN);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
    public void actionPerformed(ActionEvent event)
    }i want "pane" to be the image of "background" imageIcon is the only thing that works like this because i dont know how to use straight up "Image". everything works if i set the ImageIcon "background" to be the background of JButton "LOGIN"
    any help would be greatly appreciated

  • Background image  for JPanel using UI Properties

    Is there any way to add background image for JPanel using UI Properties,
    code is
    if (property.equals("img")) {
    System.out.println("call image file in css"+comp);
    //set the background color for Jpanel
    comp.setBackground(Color.decode("#db7093"));
    here the comp is JPanel and we are setting the background color,
    Is there any way to put the Background image for the JPanel ????

    KrishnaveniB wrote:
    Is there any way to put the Background image for the JPanel ????Override the paintComponent(...) method of JPanel.
    e.g.
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class ImagePanel {
        public void createAndShowUI() {
            try {
                JFrame frame = new JFrame("Background Image Demo");
                final Image image = ImageIO.read(new File("/home/oje/Desktop/icons/yannix.gif"));
                JPanel panel = new JPanel() {
                    protected void paintComponent(Graphics g) {
                        g.drawImage(image, 0, 0, null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(new Dimension(400, 400));
                frame.setContentPane(panel);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            } catch (IOException ex) {
                ex.printStackTrace();
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ImagePanel().createAndShowUI();
    }

  • Background changes for every button?

    Hello, I am using encore 5.1 and I was wondering if it's possible to have the background change every time I go from one button to the next withtout actually changing menus?
    I would like to do something like in the video I'm linking (watch only the first 10 seconds to get an idea of what I'm talking about).  Granted, in the video, what's running is a program and not a dvd menu, but is something similar possible in encore?
    http://www.youtube.com/watch?v=JKMu-bybpI0
    I know I can make it so I go on a button, click enter, this brings me to a new menu with a new background, but I want to cut on the amount of menus.  I like the idea of the background changing automatically without having to go to a new menu.
    Thanks for your time and help!

    When using dupe Menus and Auto Activate, I strongly recommend that one sketch out the flowchart first, and also think about using Button and Menu names, that relate directly to where they need to Link. With a bunch of Buttons, it can get involved pretty quickly. With a bit of planning, things will be much easier. I usually will layout my flowchart in Adobe Illustrator, and use the images of my Menus, in that image. It really helps me plan things. However, using AI might be overkill for some Projects, and a pencil and paper would do the job.
    Good luck, and remember, if played on a computer, the effect will not be so fluid, as the user must click on the "original" Menu to get to the "dupe" Menu with the different background - just a limitation of Auto Activate on a computer. In a DVD player, it will be pretty seamless, and can look beautiful. I use this general technique to create what appear to be Sub-picture Highlights, that are really graphics.
    Hunt

  • Desktop background changes when I change the user

    The background changes from solid dark grey to solid dark aqua blue, when I make a quick user change. It happens automatically. Even when both desktops have the same (dark grey) background. This is a mess. Is it a bug?
    Thomas

    that usuall happens when you are not connected to the internet. if you were running wifi your connection could have been lost for a moment and then recovered.

  • Why does my launchpad background change automatically??

    i just fingered my launchpad background chagnes automatically. and also i found out that people say if u wanna change your background of laucnpad, you can just press option+command+control+b, i tried it several times and it works. but when the launchpad background changes itself, i cannot change it back!!i pressed those keys and they did not work....why??

    I am a new to macs and I am having the same problem. After downloading gogle chrome then installed I decided I did not want to use it. My mistake I decided to uninstall but simply dragged tha app from the finder widow to the trash. After that launch pad started to do the same as above.
    Plaese can anyone help?

  • Every time I turn my mac off, the desktop background changes. It always returns to the same picture, which was the second or third thing I changed my desktop to after getting my mac.

    Every time I turn my mac off, the desktop background changes. It always returns to the same picture, which was the second or third thing I changed my desktop to after getting my mac. I have background switching unchecked in my preferences, and I have never selected it as an option. Also, when I go to my preferences, it will show the icon of the picture I changed it to, even though the desktop itself has switched back.

    I would start with some basic troubleshooting.
    REPAIRING DISK PERMISSIONS -
    NOTE: refer to article "About Disk Utility's Repair Disk Permissions feature" found here http://support.apple.com/kb/ht1452 - for the actual apple article on these step. ----
    - click finder in the bottom dock, and in the window that opens, select applications in the left column - scroll through until you see the 'Utilities' folder and double click to open it.  Then double click on 'Disk Utility'
    - When prompted to select a disk volume or image, select the 'Macintosh HD' in the left column
    - ensure the the "first aid" tab is highlighted blue in the middle of the screen
    - click the button at the bottom that says 'Repair Disk Permissions'
    - this process will take a few minutes to run.  It will indicate it's done, by scrolling to the bottom of the "details area" displaying 'Permissions repair complete'
    Dan Frankes has written an excellent article for Macworld on "Permissions" and the need to repair them - see - http://www.macworld.com/article/52220/2006/08/repairpermissions.html.    Another good web site is - http://www.thexlab.com/faqs/durepairfns.html.
    REPAIRING DISK - About OS X Recovery - http://support.apple.com/kb/HT4718.
    - restart your computer hold down command + R keys on the keyboard to boot you into Lion / Mountain Lion Recovery mode
    - select english as your main language then the continue arrow
    - select "Disk Utility" on the resulting screen then 'Disk Utility' ---
    - When prompted to select a disk volume or image, select the 'Macintosh HD' in the left column
    - ensure the the "first aid" tab is highlighted blue in the middle of the screen
    - click the button at the bottom that says 'REPAIR DISK' .......NOT "Repair disk permissions"
    - this process will take a few minutes to run.  It will indicate it's done, by scrolling to the bottom of the "details area" displaying "The Macintosh HD appears OK"
    If you get any other message OTHER than "The Macintosh HD appears OK", like "the Macintosh HD was repaired successfully" run the "Disk Repair" again, until it displays  "The Macintosh HD appears OK".
    Once done, restart the computer as normal

  • Background change

    Hello everybody!
    I've a problem with background and frame.
    If I would change the background of a frame at the same time
    that the main frame's background change...how I can do that?
    The background of the main frame change on mouse click on a
    link on a left frame. I would the background of left frame change
    at the same of the main frame one.
    I'm sorry for my english but I'm Italian. Thanks to all. Good
    night! Laura

    sniper8752 wrote:
    i went to my pictures, and selected an image, then Set as Home Screen Image.  i do not want that anymore.  how do i remove it?
    Put another picture that you DO like there... same process--select the image > Set as Wallpaper.
    Or use the default theme wallpaper on the same menu.
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • Cinnamon background changer will not load background images

    Hello,
    Since about yesterday (when I did a pacman -Syu), the cinnamon background changer refuses to load any images to change the background to. When I run cinnamon-settings from a terminal and select backgrounds, the following occurs:
    Loading Backgrounds module
    Failed to convert /usr/share/backgrounds/gnome/Blinds.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Bokeh_Tails.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Chmiri.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Dark_Ivy.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Fabric.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Flowerbed.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/FootFall.png: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Locked.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Mirror.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Road.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Sandstone.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Stones.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Terraform-green.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Waves.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/backgrounds/gnome/Whispy_Tails.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /usr/share/themes/Adwaita/backgrounds/morning.jpg: 'NoneType' object has no attribute 'mode'
    Failed to convert /home/evan/.cinnamon/backgrounds/hd-wallpapers.jpg: 'NoneType' object has no attribute 'mode'
    So I'm assuming it's somehow not properly loading the images. I can't add any images manually either, since once I do it gives this error for that image as well.
    I can provide a list of the packages updated. Cinnamon was not one of them and downgrading Cinnamon did not help.
    Thanks in advance!
    Last edited by Ekkoria (2014-07-05 18:59:30)

    Possibly a change in python2-pillow
    Possible fix in /usr/lib/cinnamon-settings/bin/imtools.py line 862
    Change from
    if source == mask:
    to
    if mask and source == mask:

  • TS3274 I was just moving some of my apps around and the background changed to an x-ray look  ,,,  I don't think  I touched anything ,,, How do I get it back to the original ????

    I was just moving some of my apps around and the background changed to an x-ray look ,,, I don’t think  I touched anything ,,, How do I get it back to the original ????

    Settings>General>Accessibility>Invert Color>Off

  • How do you set an image into the background of a JPanel or JFrame?

    How do you set an image into the background of a JPanel or JFrame?

    Something like this, Ive thrown in an ImageIcon on a
    button too for good measure.
    import java.awt.*;
    import javax.swing.*;
    public class JFrameImage extends JFrame {
    public JFrameImage() {
    Container c    = getContentPane();
    JPanel panel = new JPanel(){
                 public void paintComponent(Graphics g)     {
    ImageIcon img = new
    = new ImageIcon("background.jpg");
                      g.drawImage(img.getImage(), 0, 0, null);
                      super.paintComponent(g);
            panel.setOpaque(false);
    ImageIcon icon = new ImageIcon("onButton.jpg");
    JButton button = new JButton(icon);
    panel.add(button);
    c.add(panel);
    public static void main(String[] args) {
    JFrameImage frame = new JFrameImage();
    frame.setSize(200,200);
    frame.setVisible(true);
    Going totally fancy pants
    ImageIcon bigImage = new ImageIcon(bgImage.getImage().getScaledInstance(getWidth(), getHeight(),Image.SCALE_REPLICATE));
    g.drawImage(bigImage.getImage(), 0, 0, this); Will scale the image to the size of the panel
    whereas
    for (int y = 0; y  < getHeight(); y = y + image.getHeight(null))
    for (int x = 0; x< getWidth(); x = x + image.getWidth(null))
    g.drawImage(image, x, y, this); Will give a tiled effect
    Try tiling with an animated gif and bring your processor to a standstill.

  • Flash CS5: Stage background changes to white upon export no matter what color I set it to?

    Hello,
    As the title speaks for itself, I've been running into this problem since yesterday and couldn't figure out what is wrong with flash. Normally, I like to work with black background, so my stage background is always set to black, However, when I exported to view my project, the background changes to white. I've tried to check the preferences, setups and nothing I found suspicious is causing this.
    I've tried to create a new AS 3.0 document, and still, it gives me a white background when I export to view regardless if I set the stage background to red or blue.
    I've tried restarting my computer, and yet nothing is fixing it.
    If anyone can help me figure out this problem, I'll gladly appreciate it! Thanks..

    Thanks for the wonderful quick fix simPod_, I've deleted my mm.cfg file, but somehow this isn't permanent. It should do for now except the hassle of deleting the file and restarting flash. If anyone else have a permanent solution to this, I'll gladly appreciate any responses. Thanks!

  • How to set an image as background for a jPanel in an applet

    hi,
    I want to set an image as background for a jPanel component in an applet. I am using netbeans environment. I found that to set an image as a background for a jPanel is not possible. Could someone help me .
    Thanks in advance,
    Joshua

    public class ImagePanel extends JPanel
        private Image image = null;
        public void setImage(Image image)
            this.image = image;
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            g.drawImage(this.image, 0, 0, this);
    }Adjust to taste.

Maybe you are looking for

  • Database error when creating a web app instance

    I'm trying to setup Project 2013 on SharePoint 2013 and am at the point where you create a Project web app instance. I'm on step 3 on this page. http://alinimer.com/2013/02/28/install-configure-and-deploy-project-server-2013-part-4/ I'm getting an er

  • Plz help me out of this problem

    Hey guys i dont know where to post this topic questions, I am using JasperReports and IReport to desgin jrxml. I am having a problem while executing the java program which generates the html file to the local folder as per the design. It was worked w

  • Missing TCP/IP settings for iPhone tethering (Bluetooth and USB)

    Is it just me or Apple has completely disabled the TCP/IP settings for BT and USB connections for iPhone tethering (and possibly any other mobile devices)? I can now only set the IP addresses and DNS server. No Proxies or WINS settings tabs. I have a

  • Can`t open saved file ( I named it 15.05.08 )

    I just finished a huge 20 hours project in Photoshop . I don`t save my files after I`ve finished working with them . I named my project 15.05.08 and I didn`t check if everything was OK . A few minutes later I wanted to open it but to my big surprise

  • Where is CS6 Extended Bridge? My laptop crashed, and on reinstallation of CS6 there is no Bridge

    My new laptop (Windows 8) crashed, and I reinstalled CS6 Extended. Everything went fine, except no Bridge was installed and when I ran the installation again to double check there is no option to install Bridge. Where did it go? Note: I had already c