本文介绍了从Jtree中的组件获取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我推了jpanel-我在Jtree中称为LayerPanel-作为一个单元格,这个jpanel包含4个Jlabels我通过实现TreeCellRenderer接口重新渲染jtree单元问题是,如何在jpanel中捕获每个组件的事件?我尝试使用TreeCellEditor但是没有工作
I putted jpanel -that i called LayerPanel- in Jtree as a cell, this jpanel contains 4 Jlabels i have rerender the jtree cells by implementing TreeCellRenderer interface m question is, how can i catch the event for each component in jpanel ? i tries using TreeCellEditor but in didn't work
public class LayerPanelNode extends DefaultMutableTreeNode
{
public static class LayerCellEditor extends AbstractCellEditor implements TreeCellEditor, ActionListener, MouseListener
{
private LayerPanel layerpanel;
private Object value;
public LayerCellEditor(String layerName)
{
layerpanel=new LayerPanel(layerName);
layerpanel.getNameLabel().addMouseListener(this);
}
public Object getCellEditorValue(){
return layerpanel;
}
public void actionPerformed(ActionEvent e){//this is not called in my code
layerpanel = (LayerPanel) value;
System.out.println("Pressed: " );
stopCellEditing();
}
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row){
layerpanel = (LayerPanel)value;
layerpanel.getNameLabel().setText(value.toString());
return layerpanel;
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Pressed: " );
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
String val = value.toString();
JOptionPane.showInputDialog("GFDGD");
JOptionPane.showInputDialog ("Clicked: " + val);
stopCellEditing();
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void addCellEditorListener(CellEditorListener arg0) {
// TODO Auto-generated method stub
}
public void cancelCellEditing() {
// TODO Auto-generated method stub
}
public boolean isCellEditable(EventObject arg0) {
// TODO Auto-generated method stub
return false;
}
public void removeCellEditorListener(CellEditorListener arg0) {
// TODO Auto-generated method stub
}
public boolean shouldSelectCell(EventObject arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean stopCellEditing() {
// TODO Auto-generated method stub
return false;
}
}
public static class LayerCellRenderer extends JLabel implements TreeCellRenderer
{
private LayerPanel layerpanel;
public LayerCellRenderer(String layerName){
//super(new BorderLayout());
layerpanel=new LayerPanel(layerName);
layerpanel.nameLabel.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
JOptionPane.showInputDialog("GFDGD");
System.out.println("1111111111");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
//add(layerpanel ,BorderLayout.EAST);
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
//super.getTreeCellRendererComponent(tree, value, sel,expanded, leaf, row,hasFocus);
layerpanel.getNameLabel().setText(value.toString());// i think value is layerPanel object
if (leaf)
{
JLabel label =new JLabel(layerpanel.getNameLabel().getText());
label.setIcon(Utilities.loadResourceImageIcon("eye.png"));
return label;
}
return layerpanel;
}
}
}
推荐答案
这篇关于从Jtree中的组件获取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!