Problem with changing background colors in JPanels

Hi,
I have made an applet with Grid Layout and I have added JPanels in each grid. I have assigned a background color to all panels.
Now what i am trying to do is, change the clicked JPanel's background color to "blue" on a mouse click and convert the previous changed JPanel's background to originally assigned color.
This is the code that I have used:
JPanel temp=null, m;
String tempc, m_color;
public void mouseClicked(MouseEvent me)
          m=(JPanel)me.getComponent();
          if(m!=temp)
          m_color=m.getBackground().toString();   // retaining current background color so as to change later
          m.setBackground(Color.blue);
               if(temp!=null) 
                    temp.setBackground(Color.getColor(tempc));  // change back to original color
          tempc=m_color;       //tempc and m_color are Strings
          temp=m;               //temp and m are JPanels
     }When I am executing the above logic, background color changes to blue on a mouse click but the previously changed JPanel is not changing back to original color.
Can anyone please tell where is the above logic going wrong??
Thanks
Rohan

Hi Camickr,
I tried to do it without changing Color to Strings but i was getting an error in the line temp.setBackground(Color.tempc);, so i decided to change it to String and retrieve it's value in the method.
SSCCE for my code is:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.lang.reflect.InvocationTargetException;
<applet code="test" width=400 height=400>
</applet>
public class test extends JApplet
     JPanel t[][]=new JPanel[8][8];
     JPanel p[][];
     public void init()
     try{
     SwingUtilities.invokeAndWait(new Runnable()
          public void run()
               t=makeGUI(8,8);
               logic(t,8,8);
     }catch(InterruptedException e){e.printStackTrace();}
     catch(InvocationTargetException e){e.printStackTrace();}
/* Board*/
private JPanel[][] makeGUI(int rows, int cols)
    p = new JPanel[rows][cols];
    Container contentPane = getContentPane();
    contentPane.setLayout(new GridLayout(rows, cols));
    for (int i = 0; i < rows; i++)
      for (int j = 0; j < cols; j++)
        p[i][j] = new JPanel();
        contentPane.add(p[i][j]);
        if (i % 2 == j % 2)
          p[i][j].setBackground(Color.white);
          else
          p[i][j].setBackground(Color.darkGray);
     return p;
private void logic(JPanel p[][], int rows, int cols)     
          for (int i = 0; i < rows; i++)
          for (int j = 0; j < cols; j++)
          final int k=i, l=j;
          p[i][j].addMouseListener(new MouseListener()
               JPanel temp=null,m;
               String tempc, m_color;
               public void mouseClicked(MouseEvent me)
                    m=(JPanel)me.getComponent();
                    if(m!=temp)
                    m_color=m.getBackground().toString();     //retaining current background color
                    m.setBackground(Color.blue);
                    if(temp!=null)     // this method sets the panel
                         {               //     to its original color
                         temp.setBackground(Color.getColor(tempc));
                    tempc=m_color;     //color Strings
                    temp=m;          //JPanels
               public void mouseEntered(MouseEvent me){}
               public void mouseExited(MouseEvent me){}
               public void mousePressed(MouseEvent me){}
               public void mouseReleased(MouseEvent me){}
}

Similar Messages

  • Weird dual monitor problem with changing image colors

    I have dual monitors, same brand and type, same color settings and adjustments, and so far image was the same on both of them. Nothing regarding monitors or color preferences I have changed, but suddenly I started having this weird problem. Example:
    1. Here is the screenshot. I have pure red image, palettes and layers panel are on the second monitor.
    2. I start dragging image window to the other monitor and STILL HOLDING the mouse button (like I said, dragging)
    3. And when I release the mouse color suddenly changes.
    If I pick window and drag again color returns to normal. As soon as I release the mouse color changes.
    This is only happening on second monitor.
    I re-installed graphic drivers, and also re-installed Photoshop CC 2014.
    I also changed settings so the image both monitors show is the same (duplicate screen in windows) and there is no problem then. If I return to extended display problems FIXES. But as soon as I lock the computer, and login again the problem shows up again.
    Any ideas what should I do or how to fix it?

    I see your using Window have two displays.  I not sure how windows handle profiles for both displays.  Your displays are the the same models.  My Displays are physically about the same size  bothe 16:9. One displays 1920x1080 pixels the other 1360 x 768 so the resolution are quite different requires me to create special wallpaper. to make sean looks seamless or image on have the same composition.
    I notice that when I drag a window between displays a size difference but the color consistent. When I release the mouse button I see a color shift and a screen capture  shows this. And if I drag the split  window the cole os once again consistent.   I think what we are seeing is more a Windows thing not a Photoshop thing. 
    I'm using Windows 7 and CC 2014.  I do notice my window drags over the toolbar and under the pallets on the second display. Your window drags under your toolbar.

  • Problems with changing background in Mountain Lion

    After I upgraded to Mountain Lion I went into the System Settings - Desktop & Screensaver and selected one of the included images  - Earth and Moon, as my background. Afterwards I went back into the settings and I get the Beachball of Death and it is telling me "loading images". It will set there and do nothing never loading the images. I have to force quit the app. I have tried several way to enter the settings with same result.
    Eveything else seems to work OK. What Broke??????

    Quit System Preferences if it's running. In the Finder, hold down the option key and select Go ▹ Library from the menu bar. From the Library folder, delete the following item, if it exists:
    Caches/com.apple.systempreferences
    and move the following item to the Desktop:
    Preferences/com.apple.desktop.plist
    Launch System Preferences and test. If you still have the issue, put the item on the Desktop back where it came from and post again. Otherwise, delete the item.

  • How to change Background colors in photos

    Hi all,
    I could use some help with changing background color of attached example.  I want to be able to change background of this photo and have a few examples of it with blue, red and green background color.
    any help is appreciated.
    Thanks,
    Den

    With the selection still active apply a Solid Color adjustment layer in a new colour.
    Adjust the opacity and blending mode to suit.
    Do a Save as...    using a different file name and repeat with different colours.

  • How do I change the colors of Ring texts individually so when it pulls down, I can see each test with different background color?

    I have been using LabView for a almost 18 months now. There was no problem which I could not solve with help of all resources available. But, this is one where I am scratching my head since long time now.
    I would like to change colors of each individual text in Ring control programmatically. I have attached example VI. In this VI, I change background color of ring text, but it changes only the current (active) background color, so when you pull down the whole menu, it shows same default color for all the text background (not even the color I asked it to). Is there anyway I can define individually as to what color should be related to each text in ring menu?
    Attachments:
    Ring Menu Colors.JPG ‏16 KB
    RGB Ring.vi ‏33 KB

    " Is there anyway I can define individually as to what color should be related to each text in ring menu?"
    Well not with a ring and not programatically.
    The best I can suggest is a picture ring, see below.
    Ben
    Message Edited by Ben on 11-18-2005 02:16 PM
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction
    Attachments:
    Picture Ring.JPG ‏5 KB

  • How to change background color only in specific pages???

    Hi! I try to change color of background in pdf document using adobe reader, but it only changes all pages together and I can't find a way to do it on selected pages(or even select them). For example I need to change only 1st and last page to grey background color and leave the middle pages with white background color.  Please help! Thanks

    I seldom use Word so I can't help you with that problem. It could be in the way you are adding the background in Word but again, I use it about oncve a month.
    If you have Adobe Illustrator, you could do it there by adding a colored box in a layer behind the text (you can open the pdf directly in Illy).

  • Color problem with ProPhoto RGB color space

    Hi, everyone,
    I have wery special problem I think. I use MacBook pro 15" with retina display, adobe Photoshop CC and when I export RAW (from Nikon D7000) from Lightroom 5 to Photoshop with settings: 16 bit TIF, color space: ProPhoto RGB I have a problem with displaying the correct colors. As you can see in this picture:
    My problem are some "green" artefacts in absolutely black and white picture. I tried myself to solve this problem and found the following facts: when I convert picture into Adobe RGB or sRGB color space is everything OK - without green artefacts.
    But here is one important fact: I have calibrated monitor by datacolor spyder4elite and problem with ProPhoto RGB incorrect color displaying is only when the color calibration configuration is loaded. When I change my display calibration to standard apple color LCD profile than is everything OK.
    But using uncalibrated monitor and also Adobe RGB color space are no right solutions for me.
    My question is why I have problem with displaying ProPhoto RGB color space in photoshop under calibrated monitor profile and can anybody help me please?
    Thanks for answers.

    That's a classic example of the basic problem with 16 bit color. There is no solution that I know of.

  • How to change background color in AutoComplete window ?

    Is it possible to change background color in AutoComplete window ?

    Bob, A.Ankit, you're both chasing a ghost here.
    The screenshot shows an autocomplete enabled textbox with its dropdown list of autocomplete values to choose from. There is no property defining its backcolor. Not in the textbox nor in any class, neither any other baseclass nor _combobox of _base.vcx
    The only way to chnage that color is not recommended, via changing windows theme colors. That would effect any window and control.
    If you need another color even turning off themes won't help as VFP doesn't offer any property controlling that color, so you really would need to implement the autocomplete feature yourself, if you want the specify this backcolor.
    Bye, Olaf.
    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

  • How to change background color in online editor

    How to change background color in online editor

    Jeff,
    if you try to change all the plsql keywords to the same background color (that is not either white or black or blue) via the options panel in SQL Developer (Code Editor > PLSQL syntax colors), you will get the new background color in the worksheet only for the areas with plsql text, while the areas without any text will have the same background color as the base color scheme you started with. This means that there is no way to change the "general background" color via the options panel, but you have to use the same background color of one of the predefined color schemes.
    This seems to me a bug, but probably it's not considered high priority, so it will not be fixed for the time being.
    I would like to stress the fact that being able to change the color scheme of the development environment that you use every day about 8 hours a day can make quite a difference on your eyes at the end of the day.
    Thanks,
    Paolo

  • How to export PDF in Adobe Reader with changed background

    Hey Folks,
    I change background and text color by going to Preferences -> Accesebility and when I save the file with changed background it is still the old background. How to I save/export this PDF to have changed background?
    Thanks a lot!
    Krioni

    Adobe Acrobat.  A bit expensive just to change the document background...

  • Problem with changing Label in Bridge CS3 and CS6

    Hi there,
    I am having some problem with changing Label in Bridge CS3 and CS6 in windows.
    1. In Bridge CS3 (Windows XP), most of the time with most of the files, there is no problem. But I found that in some files, the Label does not get changed. I usually change Label by right clicking on the picture, then Label and select any Label. When I do so in those files, nothing happens. But when I rename the file by changing or adding just a single character, then it works. And if I again rename to original name, it still works. But without renaming the Label does not change. Again this does not happen with all pictures, but occationally with some. We usually receive pictures from different clients for editing purpose.
    2. In Bridge CS6 (Windows 7), if you create lot of Path using Pen tool in Photoshop for the purpose of removing background, you save the file, close, and change Label in Bridge, again open the file in Photoshop, you see no Path in the Path Pallette. So that means, changing Label works, but it removes the Path from the file. But if there is small amount of path, then no prboblem. But if there is lot of path, like you need to create for a necklace to remove background, then the problem arises.
    I am using Bridge for a purpose where it is very helpful so I really need to use Bridge, and Labeling feature is the most important to me, but after feaguring out this problem, I am really tensed as to whether or not I will find a solution to this. So any help, guidance will really be appreciated.
    Thanks very much in advance for any solution.
    Best regards

    I have re-checked the issue and I found that the problem is with JPG file.
    I wasn't even aware you could save a path to a jpeg file (always use PSD because of wanting to keep the Alpha Channel, jpeg only can contain 1 layer and no alpha channels and/or transparency).
    However I tried it on a file with a simple path and one with a complex path. It seems indeed you have found a bug. The simple path is no problem but the more complex path is indeed disappearing.
    It is not only label but also rate or adding IPTC in the description field. The moment metadata is saved to this jpeg file and reopened the earlier present work path of a more complex path is gone.
    Despite the fact that to my opinion a jpeg is not the most suitable file format for saving a work path this should not happen. If a work path is saved to a jpeg then it should be kept saved after just altering metadata in Bridge.
    It still is the same behavior in Bridge CC because that is what I use. Don't know when it started but it still needs to be repaired
    Here is the link for filing a problem or bug:
    http://feedback.photoshop.com/photoshop_family/

  • Change background color

    HI guys,
    I have a question:
    In the Form 6I exists a Text Item with own scrollbar.
    Are there any options to change background color on scrollbar attached to that item?
    Thanks in advance,
    John.
    Message was edited by:
    IMJL

    In the Form 6I exists a Text Item with own scrollbar.If you rae talking about text item, you can set property word yes and vertical scroll = yes
    For second question ,as Francois said, the answer is No

  • Change background color of textbox based on non-visible value

    Hello,
    I have a 10g master - detail form. I was wondering how can I change background color of text box (NAME) based on non-visivle item (MODIFIED_BY) value.
    So far, I have created two visual attributes and have put following code on "WHEN_NEW_BLOCK_INSTANCE" trigger
    if( :main.MODIFIED_BY = 'COCO') then
         SET_ITEM_PROPERTY('main.NAME',VISUAL_ATTRIBUTE,'VA_BLUE');
    else
         SET_ITEM_PROPERTY('main.NAME',VISUAL_ATTRIBUTE,'VA_RED');
    end if;
         But, it always goes to "ELSE" part and make RED color for all records. Could you please help me with this?
    Thanks.

    Got it.
    Post Query trigger on block
         if (:main.MODIFIED_BY ='COCO') then
              set_item_instance_property('main.NAME',current_record,visual_attribute,'VA_BLUE');
         else
              set_item_instance_property('main.NAME',current_record,visual_attribute,'VA_RED');
         end if;Thanks

  • Change background color in notes.

    Can I change background color in notes?  to black with white font? If not, why doesn't Apple already make this an option?

    I have the same issue. This is ridiculous. Yellow on a white background? Some of my notes are all black and white however if  I have an email address or series of numbers, phone, account etc those are in yellow. But not always..

  • Hover over image will change background color and size since IE8

    Hello,
    I have a problem that only occurs in IE8 without compability view.
    I have in CSS set all textlinks to change background color on hover.
    Now my sliced imageslinks also change background color and for a larger background area than the actual image.
    It filles out the area outside the sliced image.
    This hasnt happened before in any other versions of IE and it looks fine in Firefox.
    Please can some one help me !
    I have been searching net for hours.
    I tried to change the background color when hovering imagelinks. Maybe I did something wrong but it didnt help.
    BR,
    Peter

    Noone that can give me a clue?
    I have been trying to figure this out all day

Maybe you are looking for