Java — the problem of adjusting canvas in JScrollPane in jsplitpane
I am using NetBeans GUI editor to create an application. I want to have a jsplitpane in it. The top component will be canvas in JScrollPane, and the bottom component will be jtextarea, or something similar
When I pull down the divider to increase the size of the top component, everything seems to adjust well
There was a problem when I tried to push the divider up: the separator seemed to be under the canvas (or possibly under the JScrollPane)
I've tried various combinations of preferred / minimum / maximum sizes of JScrollPane and canvas, but it doesn't seem to have any effect
This is part of the code generated by NetBeans and may be related to the problem at hand:
jSplitPane1 = new javax.swing.JSplitPane(); jScrollPane1 = new javax.swing.JScrollPane(); canvas1 = new java.awt.Canvas(); jTextField1 = new javax.swing.JTextField(); jSplitPane1.setDividerLocation(300); jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jScrollPane1.setViewportView(canvas1); jSplitPane1.setTopComponent(jScrollPane1); jTextField1.setText("jTextField1"); jSplitPane1.setRightComponent(jTextField1);
Since this is my first question, I do not allow images to be embedded in the question, so I will publish the link:
The red arrow indicates the position of the separation line
Thank you for your time
Solution
Instead of setpreferredsize (), let your components calculate their preferred size and package () closed windows to accommodate The following example adds a draw at the top Graphpanel instance, a corresponding control panel is added at the bottom
import draw.GraPHPanel; import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; /** * @see https://stackoverflow.com/q/11942961/230513 */ public class SplitGraph extends JPanel { public SplitGraph() { super(new GridLayout()); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); GraPHPanel graPHPanel = new GraPHPanel(); split.setTopComponent(new JScrollPane(graPHPanel)); split.setBottomComponent(graPHPanel.getControlPanel()); this.add(split); } private void display() { JFrame f = new JFrame("SplitGraph"); f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokelater(new Runnable() { @Override public void run() { new SplitGraph().display(); } }); } }