views:

53

answers:

1

Hi,

I am trying to use a JSplitPane in Netbeans 6.9.1, but I can not configure it properly for some reason. E.g. I add a jtree and a jtable in a jframe. Then I use "enclose in splitpane". Then I set the orientation to vertical (from horizontal).
Problem, when I try to adjust the divider location, I can not place it where I want. Either it goes all the way up or all the way down. After manually expanding one side, I get a format close to what I want. I try to do the same, and create a second jsplitpane in the same jform. Then I try to enclose both jsplitpane into another splitpane but the order of the splitpanes get reversed. Am I doing something wrong or splitpanes are not working ok?

Thanks

UPDATE

public class Testing extends javax.swing.JFrame {

    /** Creates new form Testing */
    public Testing() {
        initComponents();
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane2.setViewportView(jTable1);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTree1.setName("jTree1"); // NOI18N
        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(258, 258, 258))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
+1  A: 

If you want them the other way around just swap places of the tree and table before you choose to enclose them in a JSplitPane. Having said that it would not surprise me if NetBeans lets you choose the left/right components even after having done this.

As for the divider I'd normally use the setDividerLocation(Double location) method which takes a value between 0.0 and 1.0. This works as a percentage of where the divider should be located.

The method you are using setDividerLocation(intlocation) sets the absolute position in pixel values and I suspect that 5 is not really what you want. It also wont let you set the divider into a location that would make a component smaller than its preferred size and this is most certainly what happens when you set it to 5.

So if your screen is 400 pixels high and you want to split it in the middle set the divider to 200.

Recommended reading is How to Use Split Panes from the Java Tutorial trail.

willcodejavaforfood
@willcodejavaforfood: If I place the tree on the left of the table and enclose, their places are swapped. If I place the table on left, they are swapped but they have the location I want. But why is this behavior?Concerning the divisor location if I use the setDivider(double) as you say, the divisor is placed ok! But do you know why the layout works like that?I must place the components in last-comes first order or something in NetBeans? Finally there is an option "change order" but it does not work for some reason. The components stay there.
That seems to be the way NetBeans work, last comes first order :) Is everything fine now?
willcodejavaforfood
@willcodejavaforfood: Yes thank you. A final question. Do you use gui designers?Am I correct to use one?
@user384706 - Then please accept my answer as the correct one. When I started programming Swing I used emacs and that way I think I learned a lot more doing everything by hand than using a GUI designer. NetBeans is a very good GUI designer and I have used it a lot :)
willcodejavaforfood
@willcodejavaforfood "Do you use gui designers?Am I correct to use one?" a) No. b) Not unless you are an expert of coding GUIs by hand.
Andrew Thompson