Recursively populate JTree, using list of list

159
May 11, 2019, at 8:50 PM

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?
   });
 }

}

Some clue for recursively populate my tree?,

Answer 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);
  }
}
READ ALSO
How to show all the pages of a pdf instead of just one in pdf.js

How to show all the pages of a pdf instead of just one in pdf.js

I am having trouble showing all the pages of a PDF instead of one using pdfjs

162
How to fix &#39;window.minimize() is not a function&#39;?

How to fix 'window.minimize() is not a function'?

so I'm fairly new to javascript and electronI created a frameless window and added a bar with 3 buttons (minimize, maximize and close)

259
invis banner 100% on the page and when user click on it the page be unlocked

invis banner 100% on the page and when user click on it the page be unlocked

how can I make this? -> a invis banner "lock" the page and when a user click it be the website unlocked(the invis banner be removed) so he can use the website again

156