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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
sham999
  • 121
  • 1
  • 3
  • 12
  • 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)? – Paul Samsotha Apr 04 '14 at 14:33
  • 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 know – sham999 Apr 04 '14 at 18:14

1 Answers1

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

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 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 me – sham999 Apr 04 '14 at 18:30
  • What is the error? Did you resolve all imports by pressing `CTRL+SHIFT+I` ? – Paul Samsotha Apr 05 '14 at 00:56
  • 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? – sham999 Apr 05 '14 at 05:50
  • Please check this snapshot image.. http://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 http://i61.tinypic.com/28gzi1g.png – sham999 Apr 05 '14 at 05:55
  • 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. – Paul Samsotha Apr 05 '14 at 05:58
  • 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 that – sham999 Apr 05 '14 at 06:14
  • 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. – Paul Samsotha Apr 05 '14 at 06:22
  • I edited the code a little. Maybe easier for you to understand. – Paul Samsotha Apr 05 '14 at 06:26
  • 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 https://drive.google.com/file/d/0B8hPfL-uTFNtN1p0aEtNVG9KVDg/edit?usp=sharing – sham999 Apr 05 '14 at 07:13
  • 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. – Paul Samsotha Apr 05 '14 at 07:23
  • Sir can you please download my netbeans project and check please. this si the link https://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 .. – sham999 Apr 05 '14 at 14:29