View Full Version : JSplitPane to add JPanels to a JFrame

November 25th, 2008, 01:13 PM

I am using Java to develop an easy to use CAD drawing package.

I am going to use one panel as an options section which will have a selection of buttons so the user can choose what state they require, ie straight line, arc, delete, etc. A second panel is going to act as the canvas.

I have set up an options and a canvas class to hold the two JPanels and the objects that will be displayed on them, but I am having trouble putting them on to the JFrame that I have created. Everything compiles ok and I can add one panel or the other so that individually they are visible within the frame. It appears that the JSplitPane is the problem.

Here are what I hope the necessary parts of code that I am using:

//Initialise frame options and canvas
JFrame frame = new JFrame();
Options opt = new Options();
Canvas canv = new Canvas();

//Make frame visible
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

//Add the objects
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, opt.getPanel(), canv.getPanel());

Any help would be much appreciated


November 25th, 2008, 01:33 PM
I'm by no means an advanced Java programmer, but I think what you're trying to achieve is a layout thing (rather than a pane that can be dynamicallly split using a GUI knob) and for that I would use a Layout instead.

For such tasks I often find myself using a GridBagLayout in combination with a GridBagConstraints object.

November 25th, 2008, 01:51 PM
Reiger is almost right - except for the fact that it's perfectly OK to use SplitPanes here. But you have to do two things:

Even if you just add one Split Pane to your windows, you still need to setup a layout as otherwise since your Panel doesn't contain anything, they will shrink to 0 pixels.

Also, Never mix AWT and Swing components! If you need to draw something, derice a JPanel an override its painting method. The Swing Tutorial will aid you here.

Here's an example I've just set up using the Netbeans GUI editor:

* Testing.java
* Created on November 25, 2008, 12:43 PM

* @author me
public class Testing extends javax.swing.JFrame {

/** Creates new form Testing */
public Testing() {

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jSplitPane1 = new javax.swing.JSplitPane();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(300, 300));
getContentPane().setLayout(new java.awt.GridBagLayout());


jPanel1.setMinimumSize(new java.awt.Dimension(50, 200));

jPanel2.setMinimumSize(new java.awt.Dimension(200, 200));

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jSplitPane1, gridBagConstraints);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
setBounds((screenSize.width-410)/2, (screenSize.height-329)/2, 410, 329);
}// </editor-fold>

* @param args the command line arguments
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Testing().setVisible(true);

// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSplitPane jSplitPane1;
// End of variables declaration


November 25th, 2008, 01:59 PM

Thanks for that, I am using GridBagConstraints in the other classes and didnt even think to use it here.

I am having a problem though with the constraints. Here is the code I am now using:

Container content = frame.getContentPane();
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
content.add(opt.getPanel(), constraints);

And I am now getting this error:

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

Any ideas?