CardLayout でのカードの変更に問題があります。CardLayout をレイアウトとして使用する JPanel と、JTree を使用する JPanel を含む他の JPanel (他のクラス) があります。TreeSelectionListener を追加したい。ノードをクリックすると、カードがノードのタイトルに変わります。
ここにいくつかのコードがあります:
カードを変更したいリスナーは、159 行目の 2 番目のコード スニペットにあります。
カードを ThumbPanel (レイアウトとして JPanel と CardLayout を持つクラス) から変更すると機能しますが、TreeSelectionListener は他のクラスにあり、そこから変更しようとしても何も起こりません。
カードレイアウトの JPanel:
import com.alee.extended.image.WebImage;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.TooltipWay;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class ThumbPanel extends JPanel{
public CardLayout cl = new CardLayout();
public ThumbPanel(){
super();
initComponents();
}
public void initComponents() {
final ThumbPopupMenu tpm = new ThumbPopupMenu();
setMinimumSize(new Dimension(200, 320));
setPreferredSize(new Dimension(650, 400));
setVisible(true);
setLayout(cl);
Border blackline = BorderFactory.createLineBorder(Color.black);
setBorder(blackline);
JPanel p1 = new JPanel();
p1.add(new JLabel("parent 1"));
JPanel p2 = new JPanel();
p2.add(new JLabel("parent 2"));
add(p1, "Parent 1");
add(p2, "Parent 2");
}
public void showCategoryPanel(String panelName){
cl.show(this, panelName);
}
}
TreeSelectionListener を含む JTreeHelper を次に示します (このコードの下部にあります)。
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.Enumeration;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import net.miginfocom.swing.MigLayout;
public class JTreeHelper extends JPanel{
protected DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
private ThumbPanel tp = new ThumbPanel();
public JTreeHelper() {
super();
ml = new MigLayout("",
"[]",
"[]");
setLayout(ml);
setMinimumSize(new Dimension(200, 320));
setPreferredSize(new Dimension(250, 400));
rootNode = new DefaultMutableTreeNode("Root Node");
treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener());
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
//add JTree listeners
tree.addTreeSelectionListener(createSelectionListener());
JScrollPane scrollPane = new JScrollPane(tree);
scrollPane.setMinimumSize(new Dimension(200, 320));
scrollPane.setPreferredSize(new Dimension(250, 400));
scrollPane.setBorder(null);
add(scrollPane);
}
/** Remove all nodes except the root node. */
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}
/** Remove the currently selected node. */
public void removeCurrentNode() {
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
(currentSelection.getLastPathComponent());
MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
if (parent != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}
// Either there was no selection, or the root was selected.
toolkit.beep();
}
/** Add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child) {
return addObject(parent, child, false);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
if (parent == null) {
parent = rootNode;
}
//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());
//Make sure the user can see the new node.
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)(node.getChildAt(index));
System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
/*tree selection listener
*
* after click on tree node, the category is shown on the right
*
*
*/
private TreeSelectionListener createSelectionListener() {
return new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getPath();
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
// HERE SHOULD CHANGE CARD
tp.cl.show(tp, parentNode.toString());
System.out.println(parentNode.toString());
}
};
}
/*
*
* Method which finds JTree node with given name
*
*/
public TreePath find(DefaultMutableTreeNode root, String s) {
@SuppressWarnings("unchecked")
Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = e.nextElement();
if (node.toString().equalsIgnoreCase(s)) {
return new TreePath(node.getPath());
}
}
return null;
}
}
これは 3 番目のカードで、GUI に JTree を追加するだけです。
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.xml.ws.BindingProvider;
import net.miginfocom.swing.MigLayout;
public class CategoryTreePanel extends JPanel{
private JTreeHelper treePanel;
public CategoryTreePanel(){
super();
initComponents();
}
public void initComponents() {
setMinimumSize(new Dimension(200, 320));
setPreferredSize(new Dimension(250, 400));
setVisible(true);
ml = new MigLayout("",
"[]",
"[]");
setLayout(ml);
Border blackline = BorderFactory.createLineBorder(Color.black);
setBorder(blackline);
treePanel = new JTreeHelper();
populateTree(treePanel, thumbPanel);
add(treePanel);
}
public void populateTree(JTreeHelper treePanel, JPanel mainPanel) {
String p1Name = new String("Parent 1");
String p2Name = new String("Parent 2");
String c1Name = new String("Child 1");
String c2Name = new String("Child 2");
String[] categoryArray = {p1Name, p2Name,c1Name,c2Name};
for (String s : categoryArray){
if(s.equals(p1Name) == true){
createNode(s, true, null, mainPanel);
}
if(s.equals(p2Name) == true){
createNode(s, true, null, mainPanel);
}
if(s.equals(c1Name) == true){
createNode(s, false, "Parent 1", mainPanel);
}
if(s.equals(c2Name) == true){
createNode(s, false, "Parent 2", mainPanel);
}
}
/*
* DefaultMutableTreeNode p1, p2;
p1 = treePanel.addObject(null, p1Name);
p2 = treePanel.addObject(null, p2Name);
treePanel.addObject(p1, c1Name);
treePanel.addObject(p1, c2Name);
treePanel.addObject(p2, c1Name);
treePanel.addObject(p2, c2Name);
*/
}
public void createNode(String categoryName, boolean isParent, String parent,
JPanel mainPanel){
DefaultMutableTreeNode categoryNode;
if(isParent == true){
categoryNode = treePanel.addObject(null, categoryName);
}else{
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) treePanel.tree.getModel().getRoot();
TreePath path = treePanel.find(rootNode, parent);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
categoryNode = treePanel.addObject(parentNode, categoryName);
}
}
}
JTreeHelper のリスナーから ThumbPanel のカードを変更するにはどうすればよいですか?