Resizing issue with canvas within jscrollpane within jsplitpane
Asked Answered
K

3

8

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.

Karonkaross answered 13/8, 2012 at 22:0 Comment(4)
Why are using java.awt.Canvas instead of javax.swing.JPanel?Sidell
Because I'm new to Swing, and canvas seemed the right thing to choose for my purpose, which is to draw shapes on it. I will take a look to the JPanel as well, to see if it meets my needs.Karonkaross
It will fit your needs and it is the best choice, since it is not good to mix AWT and Swing components. AWT components should be avoided. I really don't know what to answer since I didn't understand what you want to do.Sidell
The problem is that the canvas will grow larger, but if i want to get it smaller again, it won't resize.Karonkaross
K
3

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

Heavyweight components have their own Z-ordering. This is the reason why Swing and AWT cannot be combined in a single container. If they are, the AWT components will be drawn on TOP of Swing components.

For example: When AWT components are used with JtabbedPane, they do not disappear when the tabs are switched.

Thanks davidbuzatto for showing me the way :-)

Karonkaross answered 14/8, 2012 at 8:47 Comment(0)
U
9

Instead of setPreferredSize(), let your components calculate their own preferred size and pack() the enclosing Window to accommodate. The example below adds an instance of draw.GraphPanel to the top and a corresponding control panel to the bottom.

image

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://mcmap.net/q/169837/-resizing-issue-with-canvas-within-jscrollpane-within-jsplitpane/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();
            }
        });
    }
}
Unctuous answered 14/8, 2012 at 0:39 Comment(2)
As an aside, don't let the GUI designer dictate your design.Unctuous
That GraphPanel class seems very interesting. Thanks for sharing :)Karonkaross
S
4

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.

import java.awt.*; // it is necessary to use the Dimension and BorderLayout classes
import javax.swing.*;

public class Foo extends JFrame {

    public Foo() {

        setTitle( "Splits" );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        setSize( 400, 400 );

        JSplitPane split = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
        split.setDividerLocation( 200 );
        add( split );

        JPanel panel1 = new JPanel();
        panel1.setLayout( new BorderLayout() );
        panel1.add( new JLabel( "top panel" ), BorderLayout.NORTH );

        JPanel myDrawPanel = new JPanel();
        myDrawPanel.setPreferredSize( new Dimension( 100, 100 ) );
        myDrawPanel.add( new JLabel( "draw panel here!" ) );
        panel1.add( new JScrollPane( myDrawPanel ), BorderLayout.CENTER );

        split.setTopComponent( panel1 );


        JPanel panel2 = new JPanel();
        panel2.add( new JLabel( "bottom panel" ) );
        split.setBottomComponent( panel2 );


        setVisible( true );

    }

    public static void main( String[] args ) {
        new Foo();
    }

}
Sidell answered 13/8, 2012 at 22:21 Comment(0)
K
3

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

Heavyweight components have their own Z-ordering. This is the reason why Swing and AWT cannot be combined in a single container. If they are, the AWT components will be drawn on TOP of Swing components.

For example: When AWT components are used with JtabbedPane, they do not disappear when the tabs are switched.

Thanks davidbuzatto for showing me the way :-)

Karonkaross answered 14/8, 2012 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.