How to change background color of jDesktopPane which is created usning tools in netbeans
Asked Answered
A

1

6

By unsing netbeans ide , I created a JDesktopPane inside the JFrame. and I cannot change the color of the jdesktopPane.. I tried all I can. But when I open the JFrame .. the JDesktopPane inside that JFrame is in some blue color background.

Please help me to change the background of JDesktopPane

Armandinaarmando answered 4/4, 2014 at 13:46 Comment(2)
What you want to change it to? Just a color, or a background design? Also are you using Nimbus look and feel (the default for GUI Builder)?Mckinleymckinney
Color and background both... Yes I think Im using default GUI builder. How to check whether its Nimbus or not? i just installed netbeans ide.. that is it what I knowArmandinaarmando
M
7

I'm going to assume you're using GUI Builder with the default Nimbus look and feel (because you said you've tried everything, and I'll assume you've tried setBackground). The look and feel has the background set. But you have options around it.

  1. You can just paint the background. You want to look at this answer for how to edit the auto-generated code. Then you can just to this, when you edit the code. Don't forget to hit
    ctrl+shift+I afterwards, to resolve all imports. I'm too lazy to write fully qualified names.

    jDesktopPane1 = new javax.swing.JDesktopPane() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    };
    

    enter image description here

  2. If you want an image, you can paint an image

    jDesktopPane1 = new javax.swing.JDesktopPane() {
        private Image image;
        {
            try {
                image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/12/16/345t34.jpeg"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    };
    

    enter image description here

  3. You could also override the Nimbus default DesktopPane[Enabled].backgroundPainter. See Nimbus Defaults here

    public static void main(String[] args) {
        try {
    
            for (UIManager.LookAndFeelInfo laf : UIManager
                    .getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put(
                            "DesktopPane[Enabled].backgroundPainter",
                            new DesktopPainter());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JDesktopPaneDemo();
            }
        });
    }
    
    static class DesktopPainter implements Painter<JComponent> {
        private Image image;
    
        public DesktopPainter() {
            try {
                image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        @Override
        public void paint(Graphics2D g, JComponent object, int width, int height) {
            g.drawImage(image, 0, 0, width, height, null);
        }
    }
    

    enter image description here

Mckinleymckinney answered 4/4, 2014 at 15:23 Comment(11)
Thanks sir.. But I could not do it. Im new to java and netbeans. Can you clearly explain,how to edit the auto generated code? right clicking jDesktopPane and then clicked customized code... then do you need me to copy and paste the code number 1 which you have given me? I did like that. but the program does not start.. it shows an error "C:\Users\Ahamed\AppData\Local\NetBeans\Cache\7.3.1\executor-snippets\run.xml:48: " what can I do ? please help meArmandinaarmando
What is the error? Did you resolve all imports by pressing CTRL+SHIFT+I ?Mckinleymckinney
No.. but can you please tell me where should I put the code which you have given me? Is it inside the "Customize code" ? or where?Armandinaarmando
Please check this snapshot image.. i61.tinypic.com/fwlxm8.jpg This is how I did it. No errors came when I press ok. But when i run the program following error comes.. see the below image i61.tinypic.com/28gzi1g.pngArmandinaarmando
In the drop-down that says default code, change it to custom creation, and just change that code. I'm not sure why you have two sets of jDesktopPane1 in your picture.Mckinleymckinney
Ok are you telling me that I should change the whole code in "Customize code" window into your code number 1 which you gave? I tried it. but its not possible.. I changed the default code to custom creation... But it doesn't allow to delete that 1st line and some comments below thatArmandinaarmando
No, When the dialog opens, select Custom Creation from the drop-down. You can then put the cursor on the line jDesktopPane1 = javax.swing.JDesktopPane();. Put the cursor before the ; and type {} in between (){};. Put the curse between the {} and hit enter. You can put your code in the block.Mckinleymckinney
I edited the code a little. Maybe easier for you to understand.Mckinleymckinney
It doesnot work bro :( :( .. shal I give you the netbeans project file? can you please check it out? It will be e great help for me.. I have to submit this project on 8th .. please help me.. following is the project.. just 5 jInternalFrames it has drive.google.com/file/d/0B8hPfL-uTFNtN1p0aEtNVG9KVDg/…Armandinaarmando
What exactly doesn't work? If you're jDesktopPane1 initializing code looks like the first option I have, there's no reason it shouldn't, besides the fact that you need to resolve all imports. The GUI Builder uses fully qualified names like javax.swing.JDesktopPane where no imports are needed. In the first example Graphics and Color need to be imported. You can import all classes needed by hittin ctrl+shift+I. Other than that, I don't know what else to tell you. I've explained it as easy as I could.Mckinleymckinney
Sir can you please download my netbeans project and check please. this si the link docs.google.com/file/d/0B8hPfL-uTFNtN1p0aEtNVG9KVDg/edit?pli=1 Go to this link and just press ctrl+s , then it will download automatically.. its just 900kbytes ..Armandinaarmando

© 2022 - 2024 — McMap. All rights reserved.