JXTreeTable не показывает повторяющиеся значения - PullRequest
0 голосов
/ 25 сентября 2019

У меня есть JXTreeTable, который показывает продукты, добавленные пользователями.Пользователь может добавлять один и тот же продукт сколько угодно раз.Когда это происходит, JXTreeTable показывает только Id продукта в последней строке, и я не могу развернуть первый продукт, чтобы увидеть бекон, который я добавил к нему, как показано ниже:
weird jxtreetable behaviour
Ожидаемое:
enter image description here
Я понял, что это происходит, потому что я переопределил equals и hashCode со свойством id в моем классе Product,но я не могу удалить их, потому что мне нужно это в некоторых других частях для моего приложения для правильной работы.Есть ли способ, которым я могу решить это?


class Product {

    private int id;
    private String name;
    private BigDecimal price;
    private List<ProductExtra> productExtras = new ArrayList<>();

    // ...

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

}

class ProductExtra {

    private int id;
    private String name;
    private BigDecimal price;

    // ...

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 41 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ProductExtra other = (ProductExtra) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

}

public class TreeTableMain extends JFrame {

    private JXTreeTable treeTable;
    protected DefaultMutableTreeTableNode rootNode;
    protected MyTreeTableModel treeModel;

    public TreeTableMain() {
        List<Product> productList = new ArrayList<>();
        productList.add(new Product(1, "BBQ Burger", new BigDecimal("9.99"), Arrays.asList(new ProductExtra(1, "Bacon", new BigDecimal("1.00")))));
        productList.add(new Product(1, "BBQ Burger", new BigDecimal("9.99"), Arrays.asList(new ProductExtra(2, "Onion", new BigDecimal("0.50")))));

        rootNode = new DefaultMutableTreeTableNode("Root Node");
        treeModel = new MyTreeTableModel(productList);

        treeTable = new JXTreeTable(treeModel);
        treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        treeTable.setRootVisible(false);

        add(new JScrollPane(treeTable));

        setTitle("JXTreeTable");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TreeTableMain();
            }
        });
    }
}
...