我在CardLayout更换卡时遇到问题。我有一个带有CardLayout的JPanel作为布局,另一个JPanel包含带有JTree的JPanel(在其他类中)。我想添加TreeSelectionListener。如果我单击节点,卡应该更改为节点标题。
以下是一些代码:
我想要更改卡片的监听器位于第159行的第二个代码段中。
当我从ThumbPanel(以JPanel和CardLayout作为布局的类)更改卡时,它可以工作,但TreeSelectionListener在其他类中,当我尝试从那里更改它时,没有任何反应。
带有cardlayout的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);
}
}
这是JTreeHelper,它包含TreeSelectionListener(在此代码的底部)
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;
}
}
这是第三张牌,只是将JTree添加到gui。
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中的卡片?
答案 0 :(得分:3)
检查
返回的内容parentNode.toString()
尝试改为
tp.cl.show(tp, parentNode.getUserObject().toString());
答案 1 :(得分:0)
问题解决了,我已将所有代码放在一个类中,现在它正在运行。我不知道为什么当我将代码分成三个不同的类时,我无法更换卡片。
现在它看起来很糟糕,但它正在工作,我可以继续我的项目。
谢谢大家的时间。
最诚挚的问候 MK