When I add panels and objects in panels, I want the image to be set as the background and show through the panels put on top of it. I read you can do this bet setting the
You're replacing the backgroundFrame
with centerFrame
add(backgroundFrame);
add(topFrame, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);
This is feature of BorderLayout
, where it can only support a single component at each of it's 5 available positions
Instead, what you want to do is either add the other panels to the backgroundFrame
or set backgroundFrame
as the frame's contentPane
, for example
setContentPane(backgroundFrame);
add(topFrame, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);
Also, as a general suggestion, avoid using setPreferredSize
, there are simply too many things that can make that go wrong
Sorry to bother you again, it looks like it is not showing through the JScrollPane. Should I open this up as a new question?
The JScrollPane
is a more complex component, which is made up of two components which are used to provide the functionality of scrolling
You need to make the JScrollPane
and it's JViewport
transparent as well (as well as the view you wrap into it)
So, this is a modified version of your createCenterFrame
method which creates a JTextArea
, wraps into a JScrollPane
and adds it to the pnl
private JPanel createCenterFrame() {
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224, 224, 224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);
pnl.setBorder(topFrameTitle);
pnl.setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane();
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
JTextArea ta = new JTextArea();
ta.setOpaque(false);
ta.setText("This is an example of a transparent text area inside a transparent scroll pane, but you could just as easily wrap a transparent panel into it and get the same result");
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
scrollPane.setViewportView(ta);
pnl.add(scrollPane);
pnl.setOpaque(false);
return pnl;
}