0

I am trying to make a gui where I use a splitPane with stuff on the left and the main content on the right. I realized that when the splitPane gets resized the content from the right panel moves accordingly to fit the new size which is not the behavior I am looking for, I want the main content on the right to remain as is when I resize. So I used a layeredPane to add the main content behind a split pane that has stuff on the left and a transparent panel on the right to show the main content behind it.

Here is a snippet of my code

JPanel phantomPanel = new JPanel();
phantomPanel.setOpaque(false);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, phantomPanel);

JLayeredPane layeredPane = new JLayeredPane();
rightPanel.setBounds(0,0,900,600);
splitPane.setBounds(0,0,900,600);
layeredPane.add(rightPanel,0,0); //Main Content is behind the split pane
layeredPane.add(splitPane, 1,0);

I am seeing the splitPane with the left panel as it should be but the right panel is white, switching the layers on the layeredPane I do see my (non white) main panel.

Changing the background color of the right panel I realized that the white is indeed the transparent panel and the problem is that the panel behind it isn't showing. What am I missing?

The split Pane with the non transparent right panel What should show behind it

Different
  • 11
  • 2
  • *"I realized that when the splitPane gets resized the content from the right panel moves accordingly to fit the new size which is not the behavior I am looking for, I want the main content on the right to remain as is when I resize"* - Put the "main" content into a `JScrollPane` instead – MadProgrammer Feb 05 '23 at 22:33
  • Does this question, using the links in the answers, help? https://stackoverflow.com/questions/4416454/jpanel-said-to-be-opaque-what-does-that-mean – Old Dog Programmer Feb 05 '23 at 22:41
  • 1
    *I want the main content on the right to remain as is when I resize.* - use the `setResizeWeight(...)` method of the split pane to control how space is allocated when the frame size changes. – camickr Feb 05 '23 at 22:58
  • @MadProgrammer I tried your suggestion but it seems that resizing the SplitPane still moves the ScrollPane around as opposed to clipping some of its content to still show the same view point – Different Feb 05 '23 at 23:05
  • Edit your question and include a [mre] so we can see the problematic behavior for ourselves. Currently, I am unable to understand why opacity and JLayeredPanes address the problem described in your first two sentences. – VGR Feb 05 '23 at 23:10

1 Answers1

0

thanks @VGR as I was making the minimal reproducible example I found the solution, both the transparent panel and the SplitPane need to be transparent in order for the Panel behind to show

package FrontEnd;

import javax.swing.*;
import java.awt.*;

public class Problem {
    public static void main(String[] args){

        JFrame frame = new JFrame("test");
        frame.setSize(600,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JPanel left = new JPanel();
        left.setBackground(Color.blue);

        JPanel transparent = new JPanel();
        transparent.setOpaque(false);

        JSplitPane SP = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,transparent);
        SP.setBounds(0,0,600,600);
        SP.setOpaque(false);

        JPanel right = new JPanel();
        right.setBackground(Color.red);
        right.setBounds(0,0,600,600);

        JLayeredPane LP = new JLayeredPane();
        LP.add(right,0,0);
        LP.add(SP,1,0);

        frame.add(LP);
        frame.setVisible(true);
    }
}

Now theoretically the right panel (red) shouldn't move around when resizing the SplitPane as that affects the left panel and the transparent panel

Different
  • 11
  • 2