{"action":"created","issue":{"url":"https://api.github.com/repos/jgraph/jgraphx/issues/49","repository_url":"https://api.github.com/repos/jgraph/jgraphx","labels_url":"https://api.github.com/repos/jgraph/jgraphx/issues/49/labels{/name}","comments_url":"https://api.github.com/repos/jgraph/jgraphx/issues/49/comments","events_url":"https://api.github.com/repos/jgraph/jgraphx/issues/49/events","html_url":"https://github.com/jgraph/jgraphx/issues/49","id":127258165,"number":49,"title":"mxHierarchicalLayout not working as expected","user":{"login":"albertotn","id":12526457,"avatar_url":"https://avatars.githubusercontent.com/u/12526457?v=3","gravatar_id":"","url":"https://api.github.com/users/albertotn","html_url":"https://github.com/albertotn","followers_url":"https://api.github.com/users/albertotn/followers","following_url":"https://api.github.com/users/albertotn/following{/other_user}","gists_url":"https://api.github.com/users/albertotn/gists{/gist_id}","starred_url":"https://api.github.com/users/albertotn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/albertotn/subscriptions","organizations_url":"https://api.github.com/users/albertotn/orgs","repos_url":"https://api.github.com/users/albertotn/repos","events_url":"https://api.github.com/users/albertotn/events{/privacy}","received_events_url":"https://api.github.com/users/albertotn/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-01-18T16:09:55Z","updated_at":"2016-06-13T10:32:31Z","closed_at":null,"body":"I'm using [JGraphx](https://github.com/jgraph/jgraphx) 3.3.0.0 to draw a graph inside a Swing Panel and all works fine.\r\nI try to insert two cells inside a target cell and is working, but layout is changing and all cells are not horizontal.\r\n\r\nSo my example is:\r\n1) build a graph with some cells:\r\n\r\n![Horizontal layout](http://i.stack.imgur.com/fn3PI.png)\r\n\r\n\r\n2) press top botton and add two cells inside Hello:\r\n\r\n![after insertion](http://i.stack.imgur.com/xvpqg.png)\r\n\r\nAs you can see, problem is layout is not horizontal anymore, even if I call again mxHierarchicalLayout.\r\n\r\nAlso see [http://stackoverflow.com/questions/32758018/mxhierarchicallayout-not-working-as-expected](here): there is a small discussion on it and I've found a quick solution..\r\n\r\n>A quick workaround for this is to use mxHierarchicalLayout but also put your coordinate inside cell's geometry. Is NOT the right thing to do, but could save your day\r\n\r\nMaybe this issue is linked to #5 ?\r\n\r\nThanks for answers!\r\n\r\nHere my test code:\r\n\r\n```java\r\n    package com.mxgraph.examples.swing;\r\n    \r\n    import java.awt.BorderLayout;\r\n    import java.awt.Dimension;\r\n    import java.awt.event.ActionEvent;\r\n    import java.awt.event.MouseAdapter;\r\n    import java.awt.event.MouseEvent;\r\n    \r\n    import javax.swing.AbstractAction;\r\n    import javax.swing.JButton;\r\n    import javax.swing.JFrame;\r\n    import javax.swing.JOptionPane;\r\n    import javax.swing.SwingConstants;\r\n    \r\n    import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;\r\n    import com.mxgraph.model.mxCell;\r\n    import com.mxgraph.model.mxGraphModel;\r\n    import com.mxgraph.model.mxICell;\r\n    import com.mxgraph.swing.mxGraphComponent;\r\n    import com.mxgraph.view.mxGraph;\r\n    \r\n    public class HelloWorld extends JFrame {\r\n    \r\n        private mxGraphComponent graphComponent;\r\n    \r\n        private int idgenerator = 0;\r\n    \r\n        public HelloWorld() {\r\n    \tsuper(\"Hello, World!\");\r\n    \tmxGraph graph = new mxGraph();\r\n    \tObject parent = graph.getDefaultParent();\r\n    \r\n    \tgraph.getModel().beginUpdate();\r\n    \ttry {\r\n    \t    Object v1 = graph.insertVertex(parent,\r\n    \t\t    String.valueOf(++idgenerator), \"Hello\", 20, 20, 80, 30);\r\n    \t    Object v2 = graph.insertVertex(parent,\r\n    \t\t    String.valueOf(++idgenerator), \"World!\", 240, 150, 80, 30);\r\n    \t    graph.insertEdge(parent, String.valueOf(++idgenerator), \"Edge\", v1,\r\n    \t\t    v2);\r\n    \t    Object v3 = graph.insertVertex(parent,\r\n    \t\t    String.valueOf(++idgenerator), \"Brando!\", 240, 150, 80, 30);\r\n    \t    graph.insertEdge(parent, String.valueOf(++idgenerator), \"Edge\", v2,\r\n    \t\t    v3);\r\n    \t    Object v4 = graph.insertVertex(parent,\r\n    \t\t    String.valueOf(++idgenerator), \"Jotaro!\", 240, 150, 80, 30);\r\n    \t    graph.insertEdge(parent, String.valueOf(++idgenerator), \"Edge\", v3,\r\n    \t\t    v4);\r\n    \r\n    \t} finally {\r\n    \t    graph.getModel().endUpdate();\r\n    \t}\r\n    \r\n    \tgraphComponent = new mxGraphComponent(graph);\r\n    \tgetContentPane().add(graphComponent, BorderLayout.CENTER);\r\n    \r\n    \tgraphComponent.getGraphControl().addMouseListener(new MouseAdapter() {\r\n    \r\n    \t    @Override\r\n    \t    public void mouseReleased(MouseEvent e) {\r\n    \t\tObject cell = graphComponent.getCellAt(e.getX(), e.getY());\r\n    \r\n    \t\tif (cell != null) {\r\n    \t\t    System.out.println(\"cell=\" + graph.getLabel(cell));\r\n    \t\t    JOptionPane.showMessageDialog(graphComponent, \"cell=\"\r\n    \t\t\t    + graph.getLabel(cell));\r\n    \t\t}\r\n    \t    }\r\n    \t});\r\n    \r\n    \tlayoutGraph(graph);\r\n    \r\n    \tJButton change = new JButton(\"change\");\r\n    \tchange.setPreferredSize(new Dimension(100, 50));\r\n    \tchange.setAction(new AbstractAction() {\r\n    \r\n    \t    private static final long serialVersionUID = -1085992528036117542L;\r\n    \r\n    \t    @Override\r\n    \t    public void actionPerformed(ActionEvent e) {\r\n    \t\tmxGraph graph = graphComponent.getGraph();\r\n    \t\tmxGraphModel model = (mxGraphModel) graph.getModel();\r\n    \t\tmxCell before = (mxCell) model.getCells().get(\"2\");\r\n    \t\tgraph = insertIn(graph, before);\r\n    \r\n    \t\tlayoutGraph(graph);\r\n    \t    }\r\n    \r\n    \t    public mxGraph insertIn(mxGraph graph, mxCell target) {\r\n    \t\tObject parent = graph.getDefaultParent();\r\n    \r\n    \t\tgraph.getModel().beginUpdate();\r\n    \t\ttry {\r\n    \t\t    mxGraphModel model = (mxGraphModel) graph.getModel();\r\n    \t\t    mxCell ed = (mxCell) target.getEdgeAt(0);\r\n    \r\n    \t\t    mxICell t = ed.getTarget();\r\n    \r\n    \t\t    model.remove(ed);\r\n    \r\n    \t\t    Object v1 = graph.insertVertex(parent,\r\n    \t\t\t    String.valueOf(++idgenerator), \"HERE!\", 20, 20, 80,\r\n    \t\t\t    30);\r\n    \r\n    \t\t    Object v2 = graph.insertVertex(parent,\r\n    \t\t\t    String.valueOf(++idgenerator), \"HERE2\", 20, 20, 80,\r\n    \t\t\t    30);\r\n    \r\n    \t\t    target.insert((mxICell) v1);\r\n    \t\t    target.insert((mxICell) v2);\r\n    \t\t    target.setCollapsed(false);\r\n    \r\n    \t\t    graph.insertEdge(parent, String.valueOf(++idgenerator), \"\",\r\n    \t\t\t    v1, v2);\r\n    \r\n    \t\t    graph.insertEdge(parent, String.valueOf(++idgenerator), \"\",\r\n    \t\t\t    v2, t);\r\n    \r\n    \t\t} finally {\r\n    \t\t    graph.getModel().endUpdate();\r\n    \t\t}\r\n    \t\treturn graph;\r\n    \t    }\r\n    \t});\r\n    \r\n    \tgetContentPane().add(change, BorderLayout.PAGE_START);\r\n    \r\n        }\r\n    \r\n        public void layoutGraph(mxGraph graph) {\r\n    \tmxHierarchicalLayout layout = new mxHierarchicalLayout(graph);\r\n    \tlayout.setOrientation(SwingConstants.WEST);\r\n    \tlayout.execute(graph.getDefaultParent());\r\n        }\r\n    \r\n        public static void main(String[] args) {\r\n    \tHelloWorld frame = new HelloWorld();\r\n    \tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\n    \tframe.setSize(800, 400);\r\n    \tframe.setVisible(true);\r\n        }\r\n    \r\n    }\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/jgraph/jgraphx/issues/comments/225545426","html_url":"https://github.com/jgraph/jgraphx/issues/49#issuecomment-225545426","issue_url":"https://api.github.com/repos/jgraph/jgraphx/issues/49","id":225545426,"user":{"login":"albertotn","id":12526457,"avatar_url":"https://avatars.githubusercontent.com/u/12526457?v=3","gravatar_id":"","url":"https://api.github.com/users/albertotn","html_url":"https://github.com/albertotn","followers_url":"https://api.github.com/users/albertotn/followers","following_url":"https://api.github.com/users/albertotn/following{/other_user}","gists_url":"https://api.github.com/users/albertotn/gists{/gist_id}","starred_url":"https://api.github.com/users/albertotn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/albertotn/subscriptions","organizations_url":"https://api.github.com/users/albertotn/orgs","repos_url":"https://api.github.com/users/albertotn/repos","events_url":"https://api.github.com/users/albertotn/events{/privacy}","received_events_url":"https://api.github.com/users/albertotn/received_events","type":"User","site_admin":false},"created_at":"2016-06-13T10:32:31Z","updated_at":"2016-06-13T10:32:31Z","body":"you're welcome @zxthirteen \r\n\r\nYou need to set for every geometry a set of coordinates and use mxHierarchicalLayout  too. You can use any kind of coordinates and let layout arrange them according"}}
{"actor":{"display_login":"albertotn"}}