Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 225
sham999 Avatar asked Apr 04 '14 13:04

sham999


People also ask

How do I change the background color of a JFrame in NetBeans?

In general, to set the JFrame background color, just call the JFrame setBackground method, like this: jframe. setBackground(Color. RED);

How do I change the background of a frame in Java?

We use the following code for the creation of a JFrame: JFrame f = new JFrame(); // Creation of a JFrame object named as f f. setTitle("Change Background Color"); //To set the title of the frame f. setVisible(true); // To present the frame on the screen f.


1 Answers

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

like image 167
Paul Samsotha Avatar answered Nov 10 '22 02:11

Paul Samsotha