Implementation and application of Java checkbox tree

When using java swing to develop UI programs, you may encounter the need to use the tree with check box, but Java Swing does not provide this component. Therefore, if you have this requirement, you have to implement the tree with check box yourself.

Check@R_740_2419 @There are differences between tree and JTree on two levels:

1. On the model layer, Check@R_740_2419 @Each node of the tree needs a member to save whether it is selected or not, but the node of JTree does not. 2. On the view layer, Check@R_740_2419 @Each node of the tree displays one more check box than that of the JTree.

Since there are two differences, as long as we fill these two differences through our own implementation, the tree with check box will be implemented. Now let's solve the first difference. In order to solve the first difference, a new node class needs to be defined Check@R_740_2419 @Treenode. This class inherits defaultmutabletreenode and adds a new member isselected to indicate whether the node is selected. For one Check@R_740_2419 @Tree, if a node is selected, its check box will be checked and used Check@R_740_2419 @The motivation of tree is to select a subtree at one time. Then, when selecting or canceling a node, its ancestor node and descendant node should make some changes. Here, we apply the following recursive rules:

1. If a node is selected manually, all its descendants should be selected; If the node is selected so that all child nodes of its parent node are selected, its parent node is selected. 2. If a node is manually deselected, all its descendants should be deselected; If the parent node of the node is selected, its parent node is deselected.

Note: the above two rules are recursive rules. When a node changes and another node changes, the other node will also change other nodes. In the above two rules, manual is emphasized because manually selecting or deselecting a node will lead to non manual selection or deselection of other nodes. Such non manual selection or non deselection is not applicable to the above rules.

Implemented according to the above rules Check@R_740_2419 @The treenode source code is as follows:

The first difference is defined by inheriting defaultmutabletreenode Check@R_740_2419 @Treenode has solved the problem. Next, we need to solve the second difference. The second difference is in appearance. Each node of JTree is displayed through treecellrenderer. To solve the second difference, we define a new class Check@R_740_2419 @Treecellrenderer, which implements the treecellrenderer interface. Check@R_740_2419 @The source code of treereader is as follows:

stay Check@R_740_2419 @In the implementation of treecellrenderer, the gettreecellrendercomponent method returns JPanel instead of jlabel like defaulttreecellrenderer. Therefore, jlabel in JPanel cannot respond to the selection, so we re implemented a subclass of jlabel Check@R_740_2419 @Treelabel, which can respond to selected, has the following source code:

By definition Check@R_740_2419 @Treenode and Check@R_740_2419 @TreeCellRenderer。 We solved it Check@R_740_2419 @There are two fundamental differences between tree and JTree, but there is another detail to be solved, that is Check@R_740_2419 @The tree can respond to user events and decide whether to select a node. To this end, we Check@R_740_2419 @Tree adds a listener that responds to user mouse events Check@R_740_2419 @Treenodeselectionlistener. The source code of this class is as follows:

only this and nothing more, Check@R_740_2419 @All the components required by the tree have been completed. The next step is how to use these components. The source code using these components is given below:

The execution results are shown in the figure below:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>