Hi, I am making a program that has a JSplitPane and its background is transparent so I add two panels with a JScrollPane on the JSplitPane.. when I add the contents in the panel it doesn't appear.

This is the code that I was using:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JViewport;
import javax.swing.UIManager;
public class sample {
JFrame mainFrame;
JPanel mainPanel;
JPanel splitPaneLeftPanel;
JPanel splitPaneRightPanel;
JSplitPane splitPane;
JScrollPane rightPanelScroll;
JScrollPane leftPanelScroll;
JViewport viewport;
sample(){
    Frame();
    SplitPaneAndContent();
    mainFrame.setVisible(true);
     }
 public void Frame(){
        mainFrame = new JFrame();
        mainFrame.setTitle("Sample");
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setSize(1024,720);
        mainFrame.setMaximumSize(new Dimension(1366,768));
        mainFrame.setMinimumSize(new Dimension(800,600));
        mainFrame.setResizable(true);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

 public void SplitPaneAndContent(){
     mainPanel = new JPanel(new BorderLayout());
     splitPaneLeftPanel = new JPanel(new BorderLayout());
     splitPaneRightPanel = new JPanel(new BorderLayout());
     mainPanel.setBackground(Color.blue);
     JScrollPane rightPanelScroll = new JScrollPane(splitPaneRightPanel);
     JScrollPane LeftPanelScroll = new JScrollPane(splitPaneLeftPanel);
    viewport = new JViewport();
    rightPanelScroll.setViewport(viewport);
    rightPanelScroll.getViewport().setOpaque(false);
    JLabel Text = new JLabel("SAMPLE");
    Text.setBackground(Color.red);

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,LeftPanelScroll,rightPanelScroll);
    splitPane.setDividerLocation(300);

    splitPaneLeftPanel.setOpaque(false);
    splitPaneRightPanel.setOpaque(false);
    rightPanelScroll.setOpaque(false);
    splitPane.setOpaque(false);

     splitPaneRightPanel.add(Text, BorderLayout.CENTER);
     mainPanel.add(splitPane, BorderLayout.CENTER);
     mainFrame.add(mainPanel, BorderLayout.CENTER);
 }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new sample();
        } 
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Dani AI

Generated

Two problems are fighting each other here: the viewport is being replaced (losing the panel you originally set), and some components remain non‑opaque so nothing paints behind/inside them. As hinted, you also shadow the class fields by redeclaring JScrollPane locally, which makes the field versions remain uninitialized. The simplest fix is to stop replacing the viewport or, if you create a new one, make sure you set its view — or just use the convenience method setViewportView(...). (docs.oracle.com)

When you make things transparent you must set opacity correctly: JViewport is opaque by default, while JComponent’s opaque property defaults to false (so JLabel won’t paint its background unless you call setOpaque(true)). If you want the split pane to show the blue main panel through the scroll panes, set the viewport and scrollpane to non‑opaque but leave the underlying panel painting; if a label should have a colored background call label.setOpaque(true). (docs.oracle.com)

Concrete, minimal fixes you can apply (don’t re-declare the scroll fields and do UI work on the EDT):

rightPanelScroll.setViewportView(splitPaneRightPanel);
Text.setOpaque(true);

Also construct/update the GUI on the Event Dispatch Thread (SwingUtilities.invokeLater(...)) and remove the extra new JViewport() unless you also call viewport.setView(...). (docs.oracle.com)

Quick troubleshooting checklist: 1) print rightPanelScroll.getViewport().getView() to confirm the view is not null; 2) avoid shadowing the fields (remove the local JScrollPane types); 3) after structural changes call revalidate()/repaint(); 4) verify which components are opaque with isOpaque() while debugging. Following those steps should restore the visible content.

The declarations on 42/43 hide the variables declared on lines 17/18 - probably not causing trouble in this case, but a bad idea anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.