I'm creating an application using the NetBeans GUI Editor, in which I want to have a JSplitPane
, the top component of which will be a Canvas
within a JScrollPane
and the bottom component will be a JTextArea
, or something like that.
When I pull the divider downwards, and thus increasing the size of the top component everything seem to resize just fine.
The problem appears when I'm trying to push the divider upwards:
The divider seems to go beneath the Canvas
(and maybe beneath the JScrollPane
too).
I have tried various combinations of the preferred/minimum/maximum sizes of the JScrollPane
and Canvas
but nothing seems to work.
This is the part of the code that Netbeans generated that may have something to do with 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'm not allowed to embed an image in the question, so I will just post the link:
The red arrows indicate the position of the divider.
Thanks in advance for your time.
As I said in my comments, you should not mix AWT and Swing components. I think you are not using the components in the correct way. Take a look, it is a simple example of how to use a JSplitPane.
After reading the comment by davidbuzatto I googled about mixing AWT and Swing components and I was a little surprissed to find out that it is such a bad practice.
I found the most accurate answer to my question here
Thanks davidbuzatto for showing me the way :-)
Instead of
setPreferredSize()
, let your components calculate their own preferred size andpack()
the enclosingWindow
to accommodate. The example below adds an instance ofdraw.GraphPanel
to the top and a corresponding control panel to the bottom.