For a programming exercise I am using a JSplitPane to make a window with a nice render of a fractal on the right, and some other garbage on the left. However, I have come across a very strange issue: the left element is completely fine, but the right element is also always left aligned. As far as I can tell, the right element is given the size of the right part of the screen, but it's always fully to the left, with the left element over top of it.
I create the JSplitPane and add its elements like this:
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
splitPane.add(sidebar, JSplitPane.LEFT);
splitPane.add(fractalWindow, JSplitPane.RIGHT);
frame.add(splitPane);
I've tried many different ways of constructing the JSplitPane and adding components to it (using setLeftComponent and setRightComponent, etc), but whatever I do, both components are always left aligned. What am I doing wrong?
EDIT: sidebar is a JPanel, fractalWindow is a subclass of JPanel. I've tried adding fractalWindow to another JPanel and adding that to splitPane, but then the right component is not drawn at all. I have not specified a layout for JFrame frame.
EDIT 2: it's solved. My Fractal class was using methods getX() and getY() and I didn't know that I was overriding these. They would usually both just return 0, which meant that the JPanel was always put in the top left corner of the parent component.
You can try to put a JPanel in the right side, and place whatever you want of the JPanel:
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
splitPane.add(sidebar, JSplitPane.LEFT);
JPanel panel = new JPanel();
panel.add(fractalWindow,BorderLayout.CENTER);
splitPane.add(panel, JSplitPane.RIGHT);
frame.add(splitPane);
That way, the panel aligns what you put in it to the center, and the split pane puts the panel on the right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With