0

I am facing a problem with JFrame, when I am adding a background Image to frame, all the other components are hidden under the image, but I want to have it in opposite way, that everything goes on top of image (JButton, JPasswordField etc.)

Thank's for your effort and time.

    setUndecorated(true);
    sestSize(new Dimension(width, height));
    window.setLayout(null);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("MyFrame\u2122");
    getContentPane().add(window);
    setResizable(false);
    setContentPane(new JLabel(new ImageIcon("C:/Users/path/to/some/image/Image.jpg")));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Helvijs
  • 160
  • 13
  • How much effort and time have you spent? You do not add Swing components and backgrounds to a JFrame. You add them to a JPanel, which is added to the JFrame. Here's the first [Stack Overflow question](http://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background) I found with a Google search that answers your question. – Gilbert Le Blanc Nov 08 '16 at 13:02
  • I am looking for a possible solution about 5 hrs already. The problem is that I want to find any possible way to set the Background picture directly on JFrame without using JPanel. – Helvijs Nov 08 '16 at 13:08
  • 1
    Then you have a real problem, because Java Swing was not designed to draw on a JFrame. You **must** use a JPanel. – Gilbert Le Blanc Nov 08 '16 at 13:10
  • Okay. Thank you for helping. – Helvijs Nov 08 '16 at 13:14
  • Look at this search for [background image JFrame](http://stackoverflow.com/search?q=background+image+%5Bjframe%5D) to see the hundreds of possible duplicates. I could not even be bothered looking through them to select one to close as a duplicate. @GilbertLeBlanc technically it is *possible* to write directly to a frame, but all the experienced developers would recommend doing the custom painting in a `JPanel` that is added to the frame and in this case, there is no valid reason to change that approach. *"Background picture directly on JFrame without using JPanel."* ... ***Why?*** – Andrew Thompson Nov 08 '16 at 16:04
  • *" I could not even be bothered looking through them to select one to close as a duplicate."* OK I changed my mind. It was the 2nd post in the search. – Andrew Thompson Nov 08 '16 at 16:06
  • @Andrew Thompson: If you want to draw an image on a JFrame, be my guest. /s I will still advise an obvious beginner to draw on a JPanel. – Gilbert Le Blanc Nov 08 '16 at 22:48
  • @GilbertLeBlanc No, don't get me wrong, the duplicate was answered by me, and used a `JPanel`. But just pointing out the difference between 'strongly inadvisable' (to the extent that you lose support/help of most of the highly experienced Swing developers such as you & me) and 'impossible'. – Andrew Thompson Nov 08 '16 at 23:17

0 Answers0