Skip to main content

JXMultiPlitLayout and adding Nodes dynamically

2 replies [Last post]
olla
Offline
Joined: 2010-07-14

I have been trying to develop an application that allows the user to add Nodes or splits dynamically by pressing a button for example. However, the problem is when trying to add the new leaf on the right side where the divider is not moving.

rightSplit.setChildren(multiSplitPane.getMultiSplitLayout().getModel(), new Divider(), new Leaf("right"));

However, when adding the new Leaf as the first child the problem is solved, but I need to add the new leaf to the right

rightSplit.setChildren( new Leaf("right"), new Divider(), multiSplitPane.getMultiSplitLayout().getModel());

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.JXMultiSplitPane.DividerPainter;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Node;
import org.jdesktop.swingx.MultiSplitLayout.Split;

public class MultiSplitting2 extends JFrame {
/** */
private static final long serialVersionUID = 1L;
private JButton leftButton, rightButton;
private JPanel mainPanel, mainPanel2, leftPanel, rightPanel;
private JScrollPane mainPanelScrollPane1, mainPanelScrollPane2, rightPanelScrollPane, leftPanelScrollPane;
private Split modelRoot;
private JXMultiSplitPane multiSplitPane;

public MultiSplitting2() {
mainPanel = new JPanel();
mainPanel2 = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
modelRoot = new Split();
multiSplitPane = new JXMultiSplitPane();
multiSplitPane.setDividerSize(5);

multiSplitPane.setDividerPainter(new DividerPainter() {
@Override
protected void doPaint(Graphics2D g, Divider object, int width, int height) {
g.setColor(Color.gray);
g.fillRect(0, 0, width, height);
}
});

mainPanel.add(new JLabel("You are in Panel 1"));
leftButton = new JButton("Open left Panel");
mainPanel.add(leftButton);
rightButton = new JButton("Open right Panel");
mainPanel.add(rightButton);
mainPanel2.add(new JLabel("Choose a button"));
mainPanel2.add(new JLabel("to view panel"));

rightPanel.add(new JLabel("Welcome to Right Panel "));
String[] imageNames = {"Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed", "kathyCosmo" };

JList list = new JList(imageNames);
JScrollPane listScrollPane = new JScrollPane(list);
rightPanel.add(listScrollPane);
leftPanel.add(new JLabel("Welcome to Left Panel "));
String[] leftList = { "David", "Bob", "Dexter" };
JList Llist = new JList(leftList);
JScrollPane leftistScrollPane = new JScrollPane(Llist);
leftPanel.add(leftistScrollPane);

Node[] children = { new Leaf("root1"), new Divider(), new Leaf("root2") };
modelRoot.setChildren(children);
multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
mainPanelScrollPane1 = new JScrollPane(mainPanel);
mainPanelScrollPane2 = new JScrollPane(mainPanel2);
rightPanelScrollPane = new JScrollPane(rightPanel);
leftPanelScrollPane = new JScrollPane(leftPanel);
multiSplitPane.add(mainPanelScrollPane1, "root2");
multiSplitPane.add(mainPanelScrollPane2, "root1");

leftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Split leftSplit = new Split();
leftSplit.setChildren(new Leaf("left"), new Divider(), multiSplitPane.getMultiSplitLayout().getModel());
multiSplitPane.getMultiSplitLayout().setModel(leftSplit);
multiSplitPane.add(leftPanelScrollPane, "left");
multiSplitPane.revalidate();
}
});

rightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Split rightSplit = new Split();
rightSplit.setChildren(multiSplitPane.getMultiSplitLayout().getModel(), new Divider(), new Leaf("right"));
multiSplitPane.getMultiSplitLayout().setModel(rightSplit);
multiSplitPane.add(rightPanelScrollPane, "right");
multiSplitPane.revalidate();
}
});
}

public static void main(String[] args) {
JFrame frame = new MultiSplitting2();
MultiSplitting2 multiSplitting = new MultiSplitting2();
frame.getContentPane().add(multiSplitting.multiSplitPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alacret
Offline
Joined: 2010-09-15

I have work it out, what i think a much better approach to the solution:

public void agregarComponenteIzquierda(T comp){
//Split nuevoModelo = new Split();
String nombre = "C" + (++num);
Leaf nuevoNodo = new Leaf(nombre);

Split modeloViejo = (Split) getMultiSplitLayout().getModel();
List nodosNuevos = new ArrayList();

nodosNuevos.add(nuevoNodo);
nodosNuevos.add(new Divider());
nodosNuevos.addAll(modeloViejo.getChildren());

// ajustarAnchos(nodosNuevos);
modeloViejo.setChildren(nodosNuevos);

getMultiSplitLayout().setModel(modeloViejo);
add(comp, nombre);
revalidate();
}

BUT, I STILL HAVE THE PROBLEM, AND I HAVE DEDICATED SEVERAL HOURS WITH OUT ANY CLUE OF HOW TO DO IT, I CAN'T MANAGE ARRANGE ALL THE SPLITS WITH THE SAME WEIGHT AFTER I ADD A NEW ONE, CAN anyone help me with that?

kleopatra
Offline
Joined: 2003-06-11

so what?

Hint: shouting in a forum helps as much as honking in a traffic jam.

Jeanette

PS: deleted the first two messages, looked like duplicates. Please be more careful in future to avoid duplication.

BTW, you can format code by taggin it with [ code ] .... [ /code ] - without the spaces. Please complain to the site admin that this isn't documented anywhere ;-)