javaswinggraphicslayout-managerpaintcomponent

How can I use the paintComponent method with a layout manager


I want to use a layout manager to layout several JButtons, JComboBoxes, etc.. I also want to use the paintComponent method to make a custom background for the JFrame rather than just the normal solid color background. For some reason, the JButtons and other component that have been added to the layout manager show correctly but the shapes that I have drawn in the background with paintComponent do not show up. How can I fix this?


Solution

  • If you want to be able to use paintComponent with a layout manager you are not going to be able to do it from withing the class that extends the JFrame. I would recommend adding a JPanel to the JFrame and having the paintComponent method in the JPanel. The JPanel should have no problem using the paintComponent method with the layout manager. If you use setLayout with the JPanel then you should be able to exclude it from the layout manager.

    I hope that this answers your question.