Recursively populate JTree, using list of list

僤鯓⒐⒋嵵緔 提交于 2020-01-06 07:11:56

问题


this question has the reverse process in get nested List of CustomObject from JTree, including leaf

I want to implement a method optimization of process, I reduced this process simple (multiplication and division) like a Tree shown.

The Bottom and Bold Rectangles (the online tool don't let me change the color) represent the total multiplication between N Numerators and D Denominators of each list.

Now, I was creating list of list in Java, in order to calculate the total cost of each list (process).

My first step was create a class...

public class Pair {

  private int num;
  private int den;

  public Pair(int num, int den) {
    this.num = num;
    this.den = den;
  }

  public int getNum() {
    return num;
  }

  public void setNum(int num) {
    this.num = num;
  }

  public int getDen() {
    return den;
  }

  public void setDen(int den) {
    this.den = den;
  }

  @Override
  public String toString() {
    return "Pair{" + "num=" + num + ", den=" + den + '}';
  }
}

The second step was...

public class TestPair {

 public static void main(String... args) {
   ArrayList<ArrayList<Pair>> listOfListPair = new ArrayList<>();

   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));


   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));


   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));

   javax.swing.JTree myTree = new javax.swing.JTree();
   listOfListPair.stream().forEachOrdered(listPair -> {

     //HOW POPULATE THE myTree?

   });
 }

}

Important Question for me:JTree, Optimization algorithm, Java

Some clue for recursively populate my tree?,


回答1:


My answer with few changes...

public class TestPair {

  public static void main(String... args) {
    List<List<Pair>> listOfListPair = new ArrayList<>();

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));

    final JTree tree = new JTree();
    tree.setRootVisible(false);
    removeAllTreeNodes(tree);
    listOfListPair.stream().forEachOrdered(listPair -> {
      addPairToTree(tree, listPair);
    });

    JFrame frame = new JFrame("Optimization Pair");
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(tree);
    frame.add("Center", scrollPane);
    frame.pack();
    frame.setSize(new Dimension(600, 600));
    frame.setVisible(true);
  }

  private static void addPairToTree(JTree tree, List<Pair> listPair) {
    try {
      DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
      DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
      DefaultMutableTreeNode pairTreeNode = rootTreeNode;
      final Pair leafPair = new Pair(1, 1);
      for (Pair pair : listPair) {
        pairTreeNode = getDefaultMutableTreeNode(pairTreeNode, pair, true);
        leafPair.setDen(leafPair.getDen() * ((Pair) pairTreeNode.getUserObject()).getDen());
        leafPair.setNum(leafPair.getNum() * ((Pair) pairTreeNode.getUserObject()).getNum());
      }
      getDefaultMutableTreeNode(pairTreeNode, leafPair, false);
      treeModel.reload(rootTreeNode);
    } catch (Exception e) {
      throw e;
    }
  }

  private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, Pair newChild, Boolean isLeaf) {
    if (parent != null) {
      DefaultMutableTreeNode child;
      for (int i = 0; i < parent.getChildCount(); i++) {
        child = (DefaultMutableTreeNode) parent.getChildAt(i);
        if (child.toString().equals(newChild.toString())) {
          return child;
        }
      }
      child = new DefaultMutableTreeNode(newChild, isLeaf);
      parent.add(child);
      return child;
    } else {
      return null;
    }
  }

  private static void removeAllTreeNodes(JTree tree) {
    DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
    if (rootTreeNode != null) {
      rootTreeNode.removeAllChildren();
    }
    reloadTree(tree);
  }

  private static void reloadTree(JTree tree) {
    DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
    DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
    treeModel.reload(rootTreeNode);
  }

}


来源:https://stackoverflow.com/questions/56086678/recursively-populate-jtree-using-list-of-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!