Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseEvents not recognised on JTree

 
Richard Squires
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with the Sun provided example "Creating TreeTables: Part 2" located at Java Tree Table2 Example
What I am trying to do is add a MouseListener to my JTree but with limited success.
1. It only fires for a left mouse click - it doesn't appear to fire at all if I use right click.
2. I only get a response from the mousePressed method, I get nothing from the mouseReleased or mouseClicked methods.
3. I've tried to implement an if getClickCount structure to detect single or double clicks but it seems to only ever register a single mouse click.
The src.zip can be found at the link given above. My version of the TreeTableExmaple2.java is below (I've added a number of JOptionPane.showMessageDialog calls to determine what actions are being performed).
Any advice on what I'm doing wrong would be greatly appreciated.
Thanks
Richard

/*
* @(#)TreeTableModelAdapter.java1.2 98/10/27
*
* Copyright 1997, 1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
/**
* This is a wrapper class takes a TreeTableModel and implements
* the table model interface. The implementation is trivial, with
* all of the event dispatching support provided by the superclass:
* the AbstractTableModel.
*
* @version 1.2 10/27/98
*
* @author Philip Milne
* @author Scott Violet
*/
public class TreeTableModelAdapter extends AbstractTableModel
{
JTree tree;
TreeTableModel treeTableModel;
public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
this.tree = tree;
this.treeTableModel = treeTableModel;
tree.addTreeExpansionListener(new TreeExpansionListener() {
// Don't use fireTableRowsInserted() here; the selection model
// would get updated twice.
public void treeExpanded(TreeExpansionEvent event) {
fireTableDataChanged();
}
public void treeCollapsed(TreeExpansionEvent event) {
fireTableDataChanged();
}
});
MouseListener ml = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {JOptionPane.showMessageDialog(null,"Left Mouse Button");}
if (SwingUtilities.isRightMouseButton(e)) {JOptionPane.showMessageDialog(null,"Right Mouse Button");}
int selRow = ( (JTree)e.getComponent()).getRowForLocation(e.getX(), e.getY());
TreePath selPath = ( (JTree)e.getComponent()).getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
//mySingleClick(selRow, selPath);
//JOptionPane.showMessageDialog(null,"Released Single Click path="+selPath);
;
}
else if(e.getClickCount() == 2) {
//myDoubleClick(selRow, selPath);
JOptionPane.showMessageDialog(null,"Released Double Click");
}
}
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {JOptionPane.showMessageDialog(null,"Left Mouse Button");}
if (SwingUtilities.isRightMouseButton(e)) {JOptionPane.showMessageDialog(null,"Right Mouse Button");}
int selRow = ( (JTree)e.getComponent()).getRowForLocation(e.getX(), e.getY());
TreePath selPath = ( (JTree)e.getComponent()).getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
//mySingleClick(selRow, selPath);
JOptionPane.showMessageDialog(null,"Clicked Single Click path="+selPath);
}
else if(e.getClickCount() == 2) {
//myDoubleClick(selRow, selPath);
JOptionPane.showMessageDialog(null,"Clicked Double Click");
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {JOptionPane.showMessageDialog(null,"Pressed Left Mouse Button");}
if (SwingUtilities.isRightMouseButton(e)) {JOptionPane.showMessageDialog(null,"Pressed Right Mouse Button");}
int selRow = ( (JTree)e.getComponent()).getRowForLocation(e.getX(), e.getY());
TreePath selPath = ( (JTree)e.getComponent()).getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
//mySingleClick(selRow, selPath);
//JOptionPane.showMessageDialog(null,"Pressed Single Click path="+selPath);
;
}
else if(e.getClickCount() == 2) {
//myDoubleClick(selRow, selPath);
JOptionPane.showMessageDialog(null,"Pressed Double Click"+e.getButton());
}
}
}
};
tree.addMouseListener(ml);

// Install a TreeModelListener that can update the table when
// tree changes. We use delayedFireTableDataChanged as we can
// not be guaranteed the tree will have finished processing
// the event before us.
treeTableModel.addTreeModelListener(new TreeModelListener() {
public void treeNodesChanged(TreeModelEvent e) {
delayedFireTableDataChanged();
}
public void treeNodesInserted(TreeModelEvent e) {
delayedFireTableDataChanged();
}
public void treeNodesRemoved(TreeModelEvent e) {
delayedFireTableDataChanged();
}
public void treeStructureChanged(TreeModelEvent e) {
delayedFireTableDataChanged();
}
});
}
// Wrappers, implementing TableModel interface.
public int getColumnCount() {
return treeTableModel.getColumnCount();
}
public String getColumnName(int column) {
return treeTableModel.getColumnName(column);
}
public Class getColumnClass(int column) {
return treeTableModel.getColumnClass(column);
}
public int getRowCount() {
return tree.getRowCount();
}
protected Object nodeForRow(int row) {
TreePath treePath = tree.getPathForRow(row);
return treePath.getLastPathComponent();
}
public Object getValueAt(int row, int column) {
return treeTableModel.getValueAt(nodeForRow(row), column);
}
public boolean isCellEditable(int row, int column) {
return treeTableModel.isCellEditable(nodeForRow(row), column);
}
public void setValueAt(Object value, int row, int column) {
treeTableModel.setValueAt(value, nodeForRow(row), column);
}
/**
* Invokes fireTableDataChanged after all the pending events have been
* processed. SwingUtilities.invokeLater is used to handle this.
*/
protected void delayedFireTableDataChanged() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
fireTableDataChanged();
}
});
}


}
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using sun's treetable code as well, and i have implement a popupmenu for on the treetable.
I added the mouselistener to the JTreeTable in my view class (the one that initializes the JTreeTable), not in the TreeTableModelAdapter.
as the popup trigger is platform specific the listener code looks like:

hope this is of help,
Chantal
 
Richard Squires
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Chantal
Your tip to place the mouse listener on the JTree Table (in TreeTableExample2.java) is what I needed and has resolved the issues I was experiencing.
Thankyou for your help, it is very much appreciated.
Richard
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am glad I could help.
chantal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic