1

I am creating an app which adds Tables to a Form and arrages them in a specific layout. I'm creating a new leaf in MultiSplitPane everytime a new table needs to be added to the form, and I'm adding the new table to the leaf. Everything works fine until I move a divider and then try to add new leaf with a table inside.

Does anyone know what's causing this mess and how I can fix it?

Here's a working SSCCE that you can copy:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.GraphicsConfiguration;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;

/**
 * @author Igor
*/
public class Example extends javax.swing.JFrame {

/**
 * Creates new form Example
 */
@Override
public void add(Component cmpnt, Object o) {
    super.add(cmpnt, o);
}

@Override
public void add(PopupMenu pm) {
    super.add(pm);
}

@Override
public boolean action(Event event, Object o) {
    return super.action(event, o);
}

public Example(GraphicsConfiguration gc) {
    super(gc);
}

@Override
public Component add(Component cmpnt, int i) {
    return super.add(cmpnt, i);
}

@Override
public Component add(Component cmpnt) {
    return super.add(cmpnt);
}
// Initialize a MultiSplitPane
MultiSplitPane multiSplitPane = new MultiSplitPane();
int boxCount = 0;
String left = " (LEAF name=left0 weight=1)";
String right = "(LEAF name=right0 weight=0)";
String layoutDef = "(ROW" + left + " " + right + ")";
MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

public Example() {
    initComponents();

    multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
    setLayout(new BorderLayout());
    add(new JScrollPane(multiSplitPane), BorderLayout.CENTER);
    add(new JButton("Go") {
        {
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Example.this.addNode();
                }
            });
        }
    }, BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();

}
//a function that adds a new Leaf to the multiSplitPane
private void addNode() {

    if (boxCount == 1) {
        left = " (LEAF name=left0 weight=0.5)";
        right = " (LEAF name=right0 weight=0.5)";
    } else if (boxCount == 2) {
        left = "(COLUMN weight=0.5 left0 left" + new Integer(boxCount - 1).toString() + ")";
    } else if (boxCount == 3) {
        right = "(COLUMN weight=0.5 right0 right" + new Integer(boxCount - 2).toString() + ")";
    } else if ((boxCount % 2 == 0) && (boxCount != 0)) {
        left = left.substring(0, left.length() - 1);
        left += " left" + new Integer(boxCount / 2).toString() + ")";
    } else if ((boxCount % 2 != 0)) {
        right = right.substring(0, right.length() - 1);
        right += " right" + new Integer(boxCount / 2).toString() + ")";
    }
    String layoutDef = "(ROW " + left + " " + right + ")";
    MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);
    multiSplitPane.getMultiSplitLayout().setModel(modelRoot);

    JTable t = new JTable(1, 1);
    t.getColumnModel().getColumn(0).setHeaderValue("Box Title" + boxCount);
    Dimension dimension = new Dimension(190, 190);
    t.setPreferredScrollableViewportSize(dimension);
    t.setRowHeight(800);

    if (boxCount == 0) {
        multiSplitPane.add(new JScrollPane(t), "left0");
    } else if (boxCount == 1) {
        multiSplitPane.add(new JScrollPane(t), "right0");
    } else if (boxCount == 2) {
        multiSplitPane.add(new JScrollPane(t), "left1");
    } else if (boxCount == 3) {
        multiSplitPane.add(new JScrollPane(t), "right1");
    } else if (boxCount % 2 == 0) {
        multiSplitPane.add(new JScrollPane(t), "left" + new Integer(boxCount / 2).toString());
    } else if (boxCount % 2 != 0) {
        multiSplitPane.add(new JScrollPane(t), "right" + new Integer(boxCount / 2).toString());
    }

    multiSplitPane.revalidate();
    boxCount++;
}// End of AddNode *****************

/**
 * 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() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Example().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
// End of variables declaration
}

P.S. Increase window size (or maximize) when you run the app.

Igor
  • 1,532
  • 4
  • 23
  • 44
  • Which SwingX version do you use? Recent versions don't have org.jdesktop.swingx.MultiSplitPane, they have JXMultiSplitPane. – lbalazscs Sep 10 '12 at 20:04
  • @Ibalazscs I have the latest NetBeans (7.2), so probably the latest swing version. MultiSplitPane wasn't in it though. I had to create the file and import it. Any noticeable different between JXMultiSplitPane and MultiSplitPane? Should I use JXMultiSplitPane Instead? – Igor Sep 10 '12 at 21:17

2 Answers2

0

SwingX is not the same as Swing. Swing (javax.swing packages) is part of Java, SwingX (org.jdesktop.swingx package) is an open-source library that provides additional components.

According to your import

import org.jdesktop.swingx.MultiSplitPane;

you are trying to use SwingX. Recent versions don't have org.jdesktop.swingx.MultiSplitPane, they have JXMultiSplitPane. Check your SwingX version.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • Thanx, but tThat doesn't solve the problem. Adding "multiSplitPane.getMultiSplitLayout().setFloatingDividers(true)" however does. But, the dividers go back to their original positions when new Leaf is added. Now gotta find a way to keep their positions steady. – Igor Sep 11 '12 at 21:55
  • If you used a recent SwingX version, that would increase your chances to get a more helpful answer. Of course, if you are maintaining some old code, this could be risky, because upgrading could break something... – lbalazscs Sep 12 '12 at 08:32
0

I think multiSplitPane.setFloatingDividers(false) fixed the issue.

Igor
  • 1,532
  • 4
  • 23
  • 44