JTree listener
Hi
Im using a TreeSelectionListener to get when the user changes the name of one of the nodes of the tree. I dont know if the correct behaviour is the one I'm getting, but I only get the new name typed when the user press enter AND selects another node. I wanted to get the new value as soon as the user press enter... How do I do that?
Maybe TreeModelListener will help you?
O
Similar Messages
-
I have the following JTree:
[root] project
[node1] car
[node2 = child of node1] car_Info
[children of node2] color, length, engine, price, etc...(about 80 items)
I use polymorphism to implement this:
public interface NodeInfo {
public void select();
public class car implements NodeInfo {
// rest of car class
public void select() {
// car specific implementation
public class car_Info implements NodeInfo {
// rest of car_info
public void select() {
// car_info specific implementation
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null) {
return;
NodeInfo nodeInfo = (NodeInfo) node.getUserObject();
nodeInfo.select(); // dynamic call to the actual runtime subtype of the NodeInfo object
How do i implement the leafs for my tree? (that is, color, length, etc....) And how can i put that into my valueChanged method?I have the following JTree:
[root] project
[node1] car
[node2 = child of node1] car_Info
[children of node2] color, length, engine, price,
etc...(about 80 items)
I use polymorphism to implement this:
public interface NodeInfo {
public void select();
public class car implements NodeInfo {
// rest of car class
public void select() {
// car specific implementation
public class car_Info implements NodeInfo {
// rest of car_info
public void select() {
// car_info specific implementation
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node =
e = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) {
return;
NodeInfo nodeInfo = (NodeInfo)
fo) node.getUserObject();
nodeInfo.select(); // dynamic call to the actual
ual runtime subtype of the NodeInfo object
How do i implement the leafs for my tree? (that is,
color, length, etc....) And how can i put that into
my valueChanged method?the valueChanged method exists only to allow the model to notify the view that it has changed, so that the view may change accordingly
I don't understand what you mean with your last sentence -
Help me on this:
I want to create a JTree on which when I click right button of mouse a JMenu shows up and I choose what kind of the Item I want to add to selected node:
Example :
Main
+
|
+-----> Chapter 1
| |
| +----------> Introduction
|
+------> Chapter 2
When I select Chapter 1 in the Jtree I want to be able to add Introduction node to it throught JMenu
Thanks GuysOkay the problem is I am adding the tree nodes dynamically andeach node will have it's own properties.
Example:
Step 1:
For the first time my tree has no node, Then I want to add node to it. First I have to check whether if there is any node? If no then my JMenu should apear with the following items:
-Add Course
-Add Presentation
We assume I have chosen Add Course, so a course node should be added to tree
Step 2:
Now we have a course node, if we select the course node and want to add a node to it we have to right click on the node, we have to decide wether if it is a course or a presentation node, then jmenu pops up according to what we have chosen: Let's assume we have chosen course, so the Jmen item should change to:
-Add on-line course
-Add off-line course
but if it was Presentation node jmenu should apear like:
-Add Confrenece
-Add Marketing Presentation
-Add .......
Step 3:
Let's assume we have chosen Add on-line course, first online course should be add to Course.
Now we have three chices adding something to course again by selecting course node, or adding a multimedia node or an HTML node to this on-line course.
As you can see every node will be dynamically added, them we have to see which one is selected to choose or change the jmenu according to the selected tree node.
Please help me with that I am so confused. I am not an expert GUI programmer, I mostly programm on server side like APIs. -
Hi,
What are the TreeListener/TreeModel methods that would be called if I
1. double click on a row
2. expand/collaps a node
Thanks in advance.
OlekA quick summary:
TreeModelListener
treeNodesChanged
treeNodesInserted
treeNodesRemoved
treeStructureChanged
TreeExpansionListener
TreeWillExpandListener
ExpandVetoException
TreeSelectionListener -
Ok here goes.
I currently have a main Jframe which contains a splitpanel. On the left side the split panel there is a jtree which is generated using beans. A jtree listener loads on the right hand side diffarent jpanels which are on separate files(classes) depending on which node the user selected. The jpanels loaded contain textboxes, labels and a save button. The problem is, I need the main class(Frame) to know if the save button has been pressed so that I can refresh the tree. Do i need to use a listener on this external class???
any help would be greatly appreciated!!
Thanks in advance.You should make the main frame an action listener.
public myFrame extends JFrame implements ActionListener Then add it to the save buttons' actionlisteners.
saveButton.addActionListener(this);In the frame's actionperformed event, update the tree.
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == saveButton) {
// do your updating
}Here is a link that should help:
http://java.sun.com/docs/books/tutorial/uiswing/events/index.html -
Implementing listener in JTree
Hi,
I am trying to implement a listener for JTree within a class other than the one where is tree is initialized and set. With this listener, I would like to grab the index of the selected child from a tree. Do I only need to implement void ValueChanged() or is there something else that is missing because it is not working. Hope you can help me. Thanks.
Here is what I am doing:
//Declaration of class
public class ServerJobList extends JTree implements TreeSelectionListener
//Listener
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
getLastSelectedPathComponent();
if (node == null) return;
if(node.isLeaf()){
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
//Get index of selected item from tree.
selectedIndex=parent.getIndex(node);
String sServer=parent.toString();
zServer=Document.getClient().getServerList().getServerByName(sServer);
displayInfoList(selectedIndex);
//Testing....
System.out.println(sServer + "'s index= " + selectedIndex);May someone please help me with this question. I am getting headaches trying to figure out what could be going wrong? Thanks.
-
SwiXML - JTree LazyLoad Expand listener issue
Hi,
Iam creating UI using SwiXML which is based on Swing. Able to create a Tree structure using the below code logic. Issue is, unable to invoke expansion listener method.
It would be great, if you can suggest on this issue.
We have configured Java file in a xml file as below
<scrollpane id="neh_task_tree">
<gridbagconstraints insets="5,5,5,5" gridx="1" gridy="1" weightx="1.0"
weighty="1.0" fill="GridBagConstraints.BOTH"
anchor="GridBagConstraints.WEST"/>
<tree id="neh_prod_activity_task_tree" initclass="com.oracle.appsfdoc.fusion.sos.LazyLoadDemo" Font="ARIAL-BOLD-14" VisibleRowCount="5"/>
</scrollpane>
public class LazyLoadDemo extends DefaultTreeModel {
JTree tree;
final TreeNode root;
public LazyLoadDemo() {
super(new DefaultMutableTreeNode("ROOT"));
System.out.println("LazyLoadDemo");
root = (DefaultMutableTreeNode) super.getRoot();
createNodes(root);
tree = new JTree((TreeNode)root);
//tree.addTreeExpansionListener(this);
//tree.addTreeWillExpandListener(this);
System.out.println("BEFORE HANDLER");
tree.addTreeExpansionListener(new TreeExpansionListener(){
@Override
public void treeCollapsed(TreeExpansionEvent e)
System.out.println("Node collapsed at " + e.getPath() + "\n");
@Override
public void treeExpanded(TreeExpansionEvent e)
System.out.println("Node expanded at " + e.getPath()+ "\n");
}); System.out.println("AFTER HANDLER");
private void treeForumReviewTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_treeForumReviewTreeExpanded
// TODO add your handling code here:
System.out.println("treeForumReviewTreeExpanded");
/*DefaultMutableTreeNode node = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
ForumView fv = (ForumView) node.getUserObject();
refreshReview(fv);*/
private TreeNode createNodes(TreeNode root) {
DefaultMutableTreeNode grandparent;
DefaultMutableTreeNode parent;
DefaultMutableTreeNode child;
DefaultMutableTreeNode temp = (DefaultMutableTreeNode)root;
root = new DefaultMutableTreeNode("San Francisco");
grandparent = new DefaultMutableTreeNode("Potrero Hill");
temp.add(grandparent);
parent = new DefaultMutableTreeNode("Restaurants");
grandparent.add(parent);
child = new DefaultMutableTreeNode("Thai Barbeque");
parent.add(child);
child = new DefaultMutableTreeNode("Goat Hill Pizza");
parent.add(child);
parent = new DefaultMutableTreeNode("Grocery Stores");
grandparent.add(parent);
child = new DefaultMutableTreeNode("Good Life Grocery");
parent.add(child);
child = new DefaultMutableTreeNode("Safeway");
parent.add(child);
grandparent = new DefaultMutableTreeNode("Noe Valley");
temp.add(grandparent);
parent = new DefaultMutableTreeNode("Restaurants");
grandparent.add(parent);
child = new DefaultMutableTreeNode("Hamano Sushi");
parent.add(child);
child = new DefaultMutableTreeNode("Hahn's Hibachi");
parent.add(child);
parent = new DefaultMutableTreeNode("Grocery Stores");
grandparent.add(parent);
child = new DefaultMutableTreeNode("Real Foods");
parent.add(child);
child = new DefaultMutableTreeNode("Bell Market");
parent.add(child);
return root;
Thanks in advance,
Samba.Can some one help on this issue? Why Iam unable to invoke expand/collapse listeners with SwiXML?
Thanks,
Samba. -
Which events should I listen for in JTree
OK I'm using TreeSelectionListener/Event to handle when someone selects a node, but I'd like to also create a JPopupMenu when someone right-clicks a node. How should I handle that?
Ok nevermind... After doing some more intensive searching I found this thread:
http://forum.java.sun.com/thread.jsp?forum=57&thread=274387&tstart=0&trange=15 -
One model for JTree and JTable
Hi.
Is it possible for a JTree and a JTable to share one model?
Thank youHope u r not using Comonent TreeTable
If u have Tree & Table different components, just want to have a common model then u can try this,
It is nothing but default implementation given in DefaultTableModel
public class MyTreeTableModel extends DefaultTreeModel implements TableModel, Serializable {
protected Vector dataVector;
/** List of listeners */
protected EventListenerList listenerList = new EventListenerList();
/** The <code>Vector</code> of column identifiers. */
protected Vector columnIdentifiers;
public MyTreeTableModel(TreeNode root) {
this(root, false);
// constructor for TreeModel only
public MyTreeTableModel(TreeNode root, boolean asksAllowsChildren) {
super(root, asksAllowsChildren);
// constructor for TableModel only
public MyTreeTableModel() {
this(0, 0);
private static Vector newVector(int size) {
Vector v = new Vector(size);
v.setSize(size);
return v;
// constructor for TableModel only
public MyTreeTableModel(int rowCount, int columnCount) {
this(newVector(columnCount), rowCount);
// constructor for TableModel only
public MyTreeTableModel(Vector columnNames, int rowCount) {
super(null);
setDataVector(newVector(rowCount), columnNames);
// constructor for TableModel only
public MyTreeTableModel(Object[] columnNames, int rowCount) {
this(convertToVector(columnNames), rowCount);
// constructor for TableModel only
public MyTreeTableModel(Vector data, Vector columnNames) {
super(null);
setDataVector(data, columnNames);
// constructor for TableModel only
public MyTreeTableModel(Object[][] data, Object[] columnNames) {
super(null);
setDataVector(data, columnNames);
* Returns a default name for the column using spreadsheet conventions:
* A, B, C, ... Z, AA, AB, etc. If <code>column</code> cannot be found,
* returns an empty string.
* @param column the column being queried
* @return a string containing the default name of <code>column</code>
private String getDefaultColumnName(int column) {
String result = "";
for (; column >= 0; column = column / 26 - 1) {
result = (char)((char)(column%26)+'A') + result;
return result;
* Returns a column given its name.
* Implementation is naive so this should be overridden if
* this method is to be called often. This method is not
* in the <code>TableModel</code> interface and is not used by the
* <code>JTable</code>.
* @param columnName string containing name of column to be located
* @return the column with <code>columnName</code>, or -1 if not found
public int findColumn(String columnName) {
for (int i = 0; i < getColumnCount(); i++) {
if (columnName.equals(getColumnName(i))) {
return i;
return -1;
* Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
* @param columnIndex the column being queried
* @return the Object.class
public Class getColumnClass(int columnIndex) {
return Object.class;
// Managing Listeners
* Adds a listener to the list that's notified each time a change
* to the data model occurs.
* @param l the TableModelListener
public void addTableModelListener(TableModelListener l) {
listenerList.add(TableModelListener.class, l);
* Removes a listener from the list that's notified each time a
* change to the data model occurs.
* @param l the TableModelListener
public void removeTableModelListener(TableModelListener l) {
listenerList.remove(TableModelListener.class, l);
* Returns an array of all the table model listeners
* registered on this model.
* @return all of this model's <code>TableModelListener</code>s
* or an empty
* array if no table model listeners are currently registered
public TableModelListener[] getTableModelListeners() {
return (TableModelListener[])listenerList.getListeners(
TableModelListener.class);
// Fire methods
* Notifies all listeners that all cell values in the table's
* rows may have changed. The number of rows may also have changed
* and the <code>JTable</code> should redraw the
* table from scratch. The structure of the table (as in the order of the
* columns) is assumed to be the same.
public void fireTableDataChanged() {
fireTableChanged(new TableModelEvent(this));
* Notifies all listeners that the table's structure has changed.
* The number of columns in the table, and the names and types of
* the new columns may be different from the previous state.
* If the <code>JTable</code> receives this event and its
* <code>autoCreateColumnsFromModel</code>
* flag is set it discards any table columns that it had and reallocates
* default columns in the order they appear in the model. This is the
* same as calling <code>setModel(TableModel)</code> on the
* <code>JTable</code>.
public void fireTableStructureChanged() {
fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
* Notifies all listeners that rows in the range
* <code>[firstRow, lastRow]</code>, inclusive, have been inserted.
* @param firstRow the first row
* @param lastRow the last row
public void fireTableRowsInserted(int firstRow, int lastRow) {
fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
* Notifies all listeners that rows in the range
* <code>[firstRow, lastRow]</code>, inclusive, have been updated.
* @param firstRow the first row
* @param lastRow the last row
public void fireTableRowsUpdated(int firstRow, int lastRow) {
fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
* Notifies all listeners that rows in the range
* <code>[firstRow, lastRow]</code>, inclusive, have been deleted.
* @param firstRow the first row
* @param lastRow the last row
public void fireTableRowsDeleted(int firstRow, int lastRow) {
fireTableChanged(new TableModelEvent(this, firstRow, lastRow,
TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
* Notifies all listeners that the value of the cell at
* <code>[row, column]</code> has been updated.
* @param row row of cell which has been updated
* @param column column of cell which has been updated
public void fireTableCellUpdated(int row, int column) {
fireTableChanged(new TableModelEvent(this, row, row, column));
* Forwards the given notification event to all
* <code>TableModelListeners</code> that registered
* themselves as listeners for this table model.
* @param e the event to be forwarded
public void fireTableChanged(TableModelEvent e) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners==TableModelListener.class) {
((TableModelListener)listeners[i+1]).tableChanged(e);
* Returns an array of all the objects currently registered
* as <code><em>Foo</em>Listener</code>s
* upon this <code>AbstractTableModel</code>.
* <code><em>Foo</em>Listener</code>s are registered using the
* <code>add<em>Foo</em>Listener</code> method.
* <p>
* You can specify the <code>listenerType</code> argument
* with a class literal,
* such as
* <code><em>Foo</em>Listener.class</code>.
* For example, you can query a
* model <code>m</code>
* for its table model listeners with the following code:
* <pre>TableModelListener[] tmls = (TableModelListener[])(m.getListeners(TableModelListener.class));</pre>
* If no such listeners exist, this method returns an empty array.
* @param listenerType the type of listeners requested; this parameter
* should specify an interface that descends from
* <code>java.util.EventListener</code>
* @return an array of all objects registered as
* <code><em>Foo</em>Listener</code>s on this component,
* or an empty array if no such
* listeners have been added
* @exception ClassCastException if <code>listenerType</code>
* doesn't specify a class or interface that implements
* <code>java.util.EventListener</code>
public EventListener[] getListeners(Class listenerType) {
return listenerList.getListeners(listenerType);
* Returns the <code>Vector</code> of <code>Vectors</code>
* that contains the table's
* data values. The vectors contained in the outer vector are
* each a single row of values. In other words, to get to the cell
* at row 1, column 5: <p>
* <code>((Vector)getDataVector().elementAt(1)).elementAt(5);</code><p>
* @return the vector of vectors containing the tables data values
public Vector getDataVector() {
return dataVector;
private static Vector nonNullVector(Vector v) {
return (v != null) ? v : new Vector();
* Replaces the current <code>dataVector</code> instance variable
* with the new Vector of rows, <code>dataVector</code>.
* <code>columnIdentifiers</code> are the names of the new
* columns. The first name in <code>columnIdentifiers</code> is
* mapped to column 0 in <code>dataVector</code>. Each row in
* <code>dataVector</code> is adjusted to match the number of
* columns in <code>columnIdentifiers</code>
* either by truncating the <code>Vector</code> if it is too long,
* or adding <code>null</code> values if it is too short.
* <p>Note that passing in a <code>null</code> value for
* <code>dataVector</code> results in unspecified behavior,
* an possibly an exception.
* @param dataVector the new data vector
* @param columnIdentifiers the names of the columns
public void setDataVector(Vector dataVector, Vector columnIdentifiers) {
this.dataVector = nonNullVector(dataVector);
this.columnIdentifiers = nonNullVector(columnIdentifiers);
justifyRows(0, getRowCount());
fireTableStructureChanged();
* Replaces the value in the <code>dataVector</code> instance
* variable with the values in the array <code>dataVector</code>.
* The first index in the <code>Object[][]</code>
* array is the row index and the second is the column index.
* <code>columnIdentifiers</code> are the names of the new columns.
* @param dataVector the new data vector
* @param columnIdentifiers the names of the columns
public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
* Equivalent to <code>fireTableChanged</code>.
* @param event the change event
public void newDataAvailable(TableModelEvent event) {
fireTableChanged(event);
// Manipulating rows
private void justifyRows(int from, int to) {
// Sometimes the MyTreeTableModel is subclassed
// instead of the AbstractTableModel by mistake.
// Set the number of rows for the case when getRowCount
// is overridden.
dataVector.setSize(getRowCount());
for (int i = from; i < to; i++) {
if (dataVector.elementAt(i) == null) {
dataVector.setElementAt(new Vector(), i);
((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
* Ensures that the new rows have the correct number of columns.
* This is accomplished by using the <code>setSize</code> method in
* <code>Vector</code> which truncates vectors
* which are too long, and appends <code>null</code>s if they
* are too short.
* This method also sends out a <code>tableChanged</code>
* notification message to all the listeners.
* @param e this <code>TableModelEvent</code> describes
* where the rows were added.
* If <code>null</code> it assumes
* all the rows were newly added
public void newRowsAdded(TableModelEvent e) {
justifyRows(e.getFirstRow(), e.getLastRow() + 1);
fireTableChanged(e);
* Equivalent to <code>fireTableChanged</code>.
* @param event the change event
public void rowsRemoved(TableModelEvent event) {
fireTableChanged(event);
* Obsolete as of Java 2 platform v1.3. Please use <code>setRowCount</code> instead.
* Sets the number of rows in the model. If the new size is greater
* than the current size, new rows are added to the end of the model
* If the new size is less than the current size, all
* rows at index <code>rowCount</code> and greater are discarded. <p>
* @param rowCount the new number of rows
public void setNumRows(int rowCount) {
int old = getRowCount();
if (old == rowCount) {
return;
dataVector.setSize(rowCount);
if (rowCount <= old) {
fireTableRowsDeleted(rowCount, old-1);
else {
justifyRows(old, rowCount);
fireTableRowsInserted(old, rowCount-1);
* Sets the number of rows in the model. If the new size is greater
* than the current size, new rows are added to the end of the model
* If the new size is less than the current size, all
* rows at index <code>rowCount</code> and greater are discarded. <p>
public void setRowCount(int rowCount) {
setNumRows(rowCount);
* Adds a row to the end of the model. The new row will contain
* <code>null</code> values unless <code>rowData</code> is specified.
* Notification of the row being added will be generated.
* @param rowData optional data of the row being added
public void addRow(Vector rowData) {
insertRow(getRowCount(), rowData);
* Adds a row to the end of the model. The new row will contain
* <code>null</code> values unless <code>rowData</code> is specified.
* Notification of the row being added will be generated.
* @param rowData optional data of the row being added
public void addRow(Object[] rowData) {
addRow(convertToVector(rowData));
* Inserts a row at <code>row</code> in the model. The new row
* will contain <code>null</code> values unless <code>rowData</code>
* is specified. Notification of the row being added will be generated.
* @param row the row index of the row to be inserted
* @param rowData optional data of the row being added
* @exception ArrayIndexOutOfBoundsException if the row was invalid
public void insertRow(int row, Vector rowData) {
dataVector.insertElementAt(rowData, row);
justifyRows(row, row+1);
fireTableRowsInserted(row, row);
* Inserts a row at <code>row</code> in the model. The new row
* will contain <code>null</code> values unless <code>rowData</code>
* is specified. Notification of the row being added will be generated.
* @param row the row index of the row to be inserted
* @param rowData optional data of the row being added
* @exception ArrayIndexOutOfBoundsException if the row was invalid
public void insertRow(int row, Object[] rowData) {
insertRow(row, convertToVector(rowData));
private static int gcd(int i, int j) {
return (j == 0) ? i : gcd(j, i%j);
private static void rotate(Vector v, int a, int b, int shift) {
int size = b - a;
int r = size - shift;
int g = gcd(size, r);
for(int i = 0; i < g; i++) {
int to = i;
Object tmp = v.elementAt(a + to);
for(int from = (to + r) % size; from != i; from = (to + r) % size) {
v.setElementAt(v.elementAt(a + from), a + to);
to = from;
v.setElementAt(tmp, a + to);
* Moves one or more rows from the inlcusive range <code>start</code> to
* <code>end</code> to the <code>to</code> position in the model.
* After the move, the row that was at index <code>start</code>
* will be at index <code>to</code>.
* This method will send a <code>tableChanged</code> notification
* message to all the listeners. <p>
* <pre>
* Examples of moves:
* <p>
* 1. moveRow(1,3,5);
* a|B|C|D|e|f|g|h|i|j|k - before
* a|e|f|g|h|B|C|D|i|j|k - after
* <p>
* 2. moveRow(6,7,1);
* a|b|c|d|e|f|G|H|i|j|k - before
* a|G|H|b|c|d|e|f|i|j|k - after
* <p>
* </pre>
* @param start the starting row index to be moved
* @param end the ending row index to be moved
* @param to the destination of the rows to be moved
* @exception ArrayIndexOutOfBoundsException if any of the elements
* would be moved out of the table's range
public void moveRow(int start, int end, int to) {
int shift = to - start;
int first, last;
if (shift < 0) {
first = to;
last = end;
else {
first = start;
last = to + end - start;
rotate(dataVector, first, last + 1, shift);
fireTableRowsUpdated(first, last);
* Removes the row at <code>row</code> from the model. Notification
* of the row being removed will be sent to all the listeners.
* @param row the row index of the row to be removed
* @exception ArrayIndexOutOfBoundsException if the row was invalid
public void removeRow(int row) {
dataVector.removeElementAt(row);
fireTableRowsDeleted(row, row);
// Manipulating columns
* Replaces the column identifiers in the model. If the number of
* <code>newIdentifier</code>s is greater than the current number
* of columns, new columns are added to the end of each row in the model.
* If the number of <code>newIdentifier</code>s is less than the current
* number of columns, all the extra columns at the end of a row are
* discarded. <p>
* @param newIdentifiers vector of column identifiers. If
* <code>null</code>, set the model
* to zero columns
public void setColumnIdentifiers(Vector columnIdentifiers) {
setDataVector(dataVector, columnIdentifiers);
* Replaces the column identifiers in the model. If the number of
* <code>newIdentifier</code>s is greater than the current number
* of columns, new columns are added to the end of each row in the model.
* If the number of <code>newIdentifier</code>s is less than the current
* number of columns, all the extra columns at the end of a row are
* discarded. <p>
* @param newIdentifiers array of column identifiers.
* If <code>null</code>, set
* the model to zero columns
public void setColumnIdentifiers(Object[] newIdentifiers) {
setColumnIdentifiers(convertToVector(newIdentifiers));
* Sets the number of columns in the model. If the new size is greater
* than the current size, new columns are added to the end of the model
* with <code>null</code> cell values.
* If the new size is less than the current size, all columns at index
* <code>columnCount</code> and greater are discarded.
* @param columnCount the new number of columns in the model
public void setColumnCount(int columnCount) {
columnIdentifiers.setSize(columnCount);
justifyRows(0, getRowCount());
fireTableStructureChanged();
* Adds a column to the model. The new column will have the
* identifier <code>columnName</code>, which may be null. This method
* will send a
* <code>tableChanged</code> notification message to all the listeners.
* This method is a cover for <code>addColumn(Object, Vector)</code> which
* uses <code>null</code> as the data vector.
* @param columnName the identifier of the column being added
public void addColumn(Object columnName) {
addColumn(columnName, (Vector)null);
* Adds a column to the model. The new column will have the
* identifier <code>columnName</code>, which may be null.
* <code>columnData</code> is the
* optional vector of data for the column. If it is <code>null</code>
* the column is filled with <code>null</code> values. Otherwise,
* the new data will be added to model starting with the first
* element going to row 0, etc. This method will send a
* <code>tableChanged</code> notification message to all the listeners.
* @param columnName the identifier of the column being added
* @param columnData optional data of the column being added
public void addColumn(Object columnName, Vector columnData) {
columnIdentifiers.addElement(columnName);
if (columnData != null) {
int columnSize = columnData.size();
if (columnSize > getRowCount()) {
dataVector.setSize(columnSize);
justifyRows(0, getRowCount());
int newColumn = getColumnCount() - 1;
for(int i = 0; i < columnSize; i++) {
Vector row = (Vector)dataVector.elementAt(i);
row.setElementAt(columnData.elementAt(i), newColumn);
else {
justifyRows(0, getRowCount());
fireTableStructureChanged();
* Adds a column to the model. The new column will have the
* identifier <code>columnName</code>. <code>columnData</code> is the
* optional array of data for the column. If it is <code>null</code>
* the column is filled with <code>null</code> values. Otherwise,
* the new data will be added to model starting with the first
* element going to row 0, etc. This method will send a
* <code>tableChanged</code> notification message to all the listeners.
public void addColumn(Object columnName, Object[] columnData) {
addColumn(columnName, convertToVector(columnData));
// Implementing the TableModel interface
* Returns the number of rows in this data table.
* @return the number of rows in the model
public int getRowCount() {
return dataVector.size();
* Returns the number of columns in this data table.
* @return the number of columns in the model
public int getColumnCount() {
return columnIdentifiers.size();
* Returns the column name.
* @return a name for this column using the string value of the
* appropriate member in <code>columnIdentifiers</code>.
* If <code>columnIdentifiers</code> does not have an entry
* for this index, returns the default
* name provided by the superclass
public String getColumnName(int column) {
Object id = null;
// This test is to cover the case when
// getColumnCount has been subclassed by mistake ...
if (column < columnIdentifiers.size()) {
id = columnIdentifiers.elementAt(column);
return (id == null) ? getDefaultColumnName(column)
: id.toString();
* Returns true regardless of parameter values.
* @param row the row whose value is to be queried
* @param column the column whose value is to be queried
* @return true
public boolean isCellEditable(int row, int column) {
return true;
* Returns an attribute value for the cell at <code>row</code>
* and <code>column</code>.
* @param row the row whose value is to be queried
* @param column the column whose value is to be queried
* @return the value Object at the specified cell
* @exception ArrayIndexOutOfBoundsException if an invalid row or
* column was given
public Object getValueAt(int row, int column) {
Vector rowVector = (Vector)dataVector.elementAt(row);
return rowVector.elementAt(column);
* Sets the object value for the cell at <code>column</code> and
* <code>row</code>. <code>aValue</code> is the new value. This method
* will generate a <code>tableChanged</code> notification.
* @param aValue the new value; this can be null
* @param row the row whose value is to be changed
* @param column the column whose value is to be changed
* @exception ArrayIndexOutOfBoundsException if an invalid row or
* column was given
public void setValueAt(Object aValue, int row, int column) {
Vector rowVector = (Vector)dataVector.elementAt(row);
rowVector.setElementAt(aValue, column);
fireTableCellUpdated(row, column);
// Protected Methods
* Returns a vector that contains the same objects as the array.
* @param anArray the array to be converted
* @return the new vector; if <code>anArray</code> is <code>null</code>,
* returns <code>null</code>
protected static Vector convertToVector(Object[] anArray) {
if (anArray == null) {
return null;
Vector v = new Vector(anArray.length);
for (int i=0; i < anArray.length; i++) {
v.addElement(anArray[i]);
return v;
* Returns a vector of vectors that contains the same objects as the array.
* @param anArray the double array to be converted
* @return the new vector of vectors; if <code>anArray</code> is
* <code>null</code>, returns <code>null</code>
protected static Vector convertToVector(Object[][] anArray) {
if (anArray == null) {
return null;
Vector v = new Vector(anArray.length);
for (int i=0; i < anArray.length; i++) {
v.addElement(convertToVector(anArray[i]));
return v; -
Hi,
I am setting the selection mode for a JTree to be single selection. Suppose the node which I selected was in expanded state, after making the selection it is in collapsed state. Now suppose I want to resume the expanded state and again try to select the same node without making any intermediate selection the listener doesn't listen. But if I make make a different node selection in between the 2 consecutive selections made to the same node the 2nd selection is listened.
Is it something like if two similar selections are made one after the other the second one is discarded or not listened. If so how can I overcome this? If not please tell me where am I making the mistake.selection will be fired only when the selection property changed. so, collapsing and expanding should be done in mouse event handlers.
-
How to show only all children of selected node in JTree??
Dear friends:
I have Two Panels, PA and PB,
PA has a Jtree as code below, and PB listens to PA,
I hope to do following,
If I select a node called A in PA, then Node A's all children such as A1, A2, A3 will be displayed in PB, but not display A1, A2, A3's children such as A3 has C1, C2, C3, C4 & C5, until I select A3 then PB will display only all A3's children: C1, C2, C3, C4 & C5;
i.e, only populate each ONE level of children of Node A or any node I select, not its grandchildren and its grand-grand children;
Please help how to do it??
I tried amny times, failed.
Thanks
[1]. PA panel code:
package com.atest;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class DefaultMutableTreeMain extends JPanel {
protected DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
protected DefaultMutableTreeNode selectedNode = null;
protected final JTree tree;
protected final JTextField jtf;
protected Enumeration vEnum = null;
private TreeModel m;
protected DefaultMutableTreeNode getDefaultMutableTreeNode() {
//textArea.getText();
return selectedNode;
protected DefaultMutableTreeNode setDefaultMutableTreeNode(DefaultMutableTreeNode tt) {
//textArea.getText();
selectedNode = tt;
return selectedNode;
protected TreeModel getJTModel() {
//textArea.getText();
return m;
protected TreeModel setJTModel(TreeModel ta) {
m = ta;
return m;
public DefaultMutableTreeMain() {
setSize(300,300);
setLayout(new BorderLayout());
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("A3");
a.add(a3);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
DefaultMutableTreeNode c = new DefaultMutableTreeNode("C");
a3.add(c);
DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("C1");
c.add(c1);
DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("C2");
c.add(c2);
DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C3");
c.add(c3);
DefaultMutableTreeNode c4 = new DefaultMutableTreeNode("C4");
c.add(c4);
DefaultMutableTreeNode c5 = new DefaultMutableTreeNode("C5");
c.add(c5);
tree = new JTree(top);
JScrollPane jsp = new JScrollPane(tree);
jsp.setPreferredSize(new Dimension(400,300));
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath path = tree.getSelectionPath();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
setDefaultMutableTreeNode(selectedNode);
System.out.println("Current node selected is (tp.toString()=" + tp.toString());
System.out.println("Current node selected is getDefaultMutableTreeNode()=" + getDefaultMutableTreeNode());
if (tp != null){
jtf.setText(tp.toString());
System.out.println("It Has Children as selectedNode.getChildCount()= " + selectedNode.getChildCount());
Enumeration vEnum = selectedNode.children();
int i = 0;
while(vEnum.hasMoreElements()){
System.out.println("2 selectedNode = " + path.toString() + " has " + i++ + " Children in vEnum.nextElement(" + i + ") = " + vEnum.nextElement());
else
jtf.setText("");
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new DefaultMutableTreeMain());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}[2]. PB Panel code
package com.atest;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DefaultMutableTreeSub extends JPanel implements java.io.Serializable{
private JButton removeButton;
private JButton addButton;
JTree tree;
private TreeModel m;
protected TreeDragSource ds;
protected TreeDropTarget dt;
protected TreeModel getJTModel() {
//textArea.getText();
return m;
protected TreeModel setJTModel(TreeModel ta) {
m = ta;
return m;
protected DefaultTreeModel model;
protected DefaultMutableTreeNode rootNode;
DefaultMutableTreeMain dmm = null;
JPanel inputPanel = new JPanel();
public JPanel SLTreeDNDEditableDynamic(DefaultMutableTreeMain tdnd ) {
//super("Rearrangeable Tree");
setSize(400,450);
dmm = tdnd;
setLayout(new BorderLayout());
inputPanel.setLayout(new BorderLayout());
JPanel outputPanel = new JPanel();
System.out.println("Sub selectedNode tdnd= " + tdnd);
tdnd.tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent evt){
TreePath[] paths = evt.getPaths();
TreePath path = dmm.tree.getSelectionPath();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
DefaultMutableTreeNode itemNode = dmm.getDefaultMutableTreeNode();
System.out.println("Sub node selected is dmm.getDefaultMutableTreeNode()=" + dmm.getDefaultMutableTreeNode());
model = new DefaultTreeModel(itemNode);
tree = new JTree(model);
System.out.println("Sub selectedNode paths= " + paths);
System.out.println("Sub selectedNode path= " + path);
System.out.println("Sub selectedNode = " + selectedNode);
System.out.println("Sub itemNode = " + itemNode);
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.setRootVisible(true);
inputPanel.add(new JScrollPane(tree),BorderLayout.CENTER);
return inputPanel;
public DefaultMutableTreeSub() {
super();
}thanks
sunnyThanks so much, I use your code and import followig:
import java.util.ArrayList;
import java.awt.List;
but
private static List<Object> getChildNodes(JTree j) {
Object parent = j.getLastSelectedPathComponent();
int childNodeCount = j.getModel().getChildCount(parent);
List<Object> results = new ArrayList()<Object>;
for (i = 0; i < childNodeCount; i++) {
results.add(parent, i);
return results;
here List<Object> and ArrayList()<Object> show red,
Is my JDK version problem??
my one is JKD
C:\temp\swing>java -version
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
Error as follows:
C:\temp\swing>javac DefaultMutableTreeSub.java
DefaultMutableTreeSub.java:38: <identifier> expected
private static List<Object> getChildNodes(JTree j) {
^
1 error
any idea??
Thanks -
How to expand a JTree depending on a property
Hi I have a JTree that needs to be expandAll if a property in the ini file is true. Can some tell me how I can do it or give me an example. If needed i can post my code. Here the addtreeDisplays() has to expandAll depending on a string(true ).
Can some help me out.
Thanks,
public class TreeDisplayPanel extends JPanel implements QMRequestListener,
TopologySelectionListener,
PropertyChangeListener
/** Creates new TreeDisplayPanel */
//Key to get from property file
String _treeKey = "tree.display.type";
//default display
public final static int TREE_DISPLAY = 0;
public final static int STAR_DISPLAY = 1;
public final static int BOTH_DISPLAY =2;
Properties _displayProp = null;
//Main panel where everything gets put on to be displayed
public JPanel _mainDisplayPanel;
//current default display
private int treeDisplayPreference = STAR_DISPLAY;
private MQETabbedPane _treeTabPane;
private MQETabbedPane _viewTabPane;
private String treeDisplayTitle;
static private final String treeviewKey = "tree.view.text";
static private final String starviewKey = "star.view.text";
static private final String splitviewKey = "split.view.text";
static private final String mergeviewKey = "merge.view.text";
// Default tree displays.
private HyperbolicTreePanel hyperbolicTreePanel = null;
private NavigatorTreePanel navigatorTreePanel = null;
// A store for any queue manager tree displays created
// by the user. This will enable these trees to be
// modified whenever the user invokes a expand/collapse
// all action on a node or whenever the user changes
// the leaf node expansion preference.
private Vector<NavigatorTreePanel> qmgrTreeDisplays =
new Vector<NavigatorTreePanel>();
//HashMap of indexes correspond to indexes in tabbed paned that are merged, it contains
//hashtables of components in those merged indexes
HashMap _qmMergedIndexes = new HashMap();
//Current listeners to this panel on node selections
protected Vector<TopologySelectionListener> _tsListeners =
new Vector<TopologySelectionListener>();
private TopologyModel m_model = null;
private TopologyModelNode m_nnode = null;
private TopologyDisplayPanel tdp = null;
public TreeDisplayPanel(TopologyModel model, Properties prop, String displayTitle) {
super(new BorderLayout());
m_model = model;
_displayProp = prop;
treeDisplayTitle = displayTitle;
initComponent();
initGui();
private void initComponent()
_mainDisplayPanel = ComponentFactory.getInstance().createTitledPanel(treeDisplayTitle);
_mainDisplayPanel.setLayout(new BorderLayout());
add(_mainDisplayPanel, BorderLayout.CENTER);
private void initGui()
if (_displayProp != null)
// Get the current display preference.
String defaultDisplay = Integer.toString(STAR_DISPLAY);
treeDisplayPreference = Integer.parseInt(_displayProp.getProperty(_treeKey, defaultDisplay));
try
//Add according to your display property
_treeTabPane = new MQETabbedPane();
_viewTabPane = new MQETabbedPane(JTabbedPane.BOTTOM);
_viewTabPane.addMouseListener(new MouseListener()
public void mouseClicked(MouseEvent e)
final int tabNum = _viewTabPane.getUI().tabForCoordinate(_viewTabPane,e.getX(),e.getY());
//Only if the mouse click is a right mouse and tab number is not on overview pane or doc pane is the popup valid
if (SwingUtilities.isRightMouseButton(e) && tabNum > 1)
final String tabStr = _viewTabPane.getTitleAt(tabNum);
JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem1 = new JMenuItem(new AbstractAction("Close " + tabStr)
public void actionPerformed(ActionEvent e)
//Remove it from our HashMap of merged panes if it exists
if (_qmMergedIndexes.containsKey(tabStr))
_qmMergedIndexes.remove(tabStr);
// Remove the Qmgr tree display.
NavigatorTreePanel treePanel = (NavigatorTreePanel)_viewTabPane.getComponentAt(tabNum);
ExpandingModelNode model = (ExpandingModelNode)treePanel.getNavigatorTreeModel();
model.getNode().removeTreeModelListener(model);
qmgrTreeDisplays.remove(treePanel);
_viewTabPane.removeTabAt(tabNum);
String mergeview = StringFactory.getString(mergeviewKey);
JMenu merge = new JMenu(mergeview);
merge.setEnabled(false);
int numTabs = _viewTabPane.getTabCount();
//System.out.println("Num of tabs " + numTabs);
//Only allow merging if you have more then 2 panes (1 - Overview, 2 - Qmgr, 3-Qmgr....
//Also if the current pane is not a merge pane already
if (numTabs > 3 && !_qmMergedIndexes.containsKey(tabStr))
//Do not enable this menu if the number of already merged Indexes - the number of tabs
//is greater then two (One Valid pane + Overview pane). The reason is because then there is
//no valid pane to merge with
if ((numTabs - _qmMergedIndexes.size()) > 3)
merge.setEnabled(true);
for (int i = 2; i<numTabs; i++)
//Add only valid tabs and not already merged tabs
if (i != tabNum && !_qmMergedIndexes.containsKey(_viewTabPane.getTitleAt(i)))
//JMenuItem mergeItem = new JMenuItem(new AbstractAction(_viewTabPane.getTitleAt(i))
JMenuItem mergeItem = new JMenuItem(new AbstractAction(_viewTabPane.getToolTipTextAt(i))
public void actionPerformed(ActionEvent e)
try
//System.out.println("Action Name for " + e.getActionCommand());
//Work around for java bug in JTabbedPane
_viewTabPane.setSelectedIndex(0);
_viewTabPane.validate();
//End workaround
//Strip off the fully qualified name to contain only the name of the QM name
String mergeTabName = m_model.getQMgrName(e.getActionCommand());
JPanel splitPanel = new JPanel(new BorderLayout());
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setBorder(null);
splitPane.setOneTouchExpandable(true);
int mergeTabIndex = _viewTabPane.indexOfTab(mergeTabName);
String mergeTabString = _viewTabPane.getTitleAt(mergeTabIndex);
Component mergeComp = _viewTabPane.getComponentAt(mergeTabIndex);
String mergeCompAddress = _viewTabPane.getToolTipTextAt(mergeTabIndex);
splitPane.setLeftComponent(mergeComp);
int currentTabIndex = _viewTabPane.indexOfTab(tabStr);
Component currentComp = _viewTabPane.getComponentAt(currentTabIndex);
String currentCompAddress = _viewTabPane.getToolTipTextAt(currentTabIndex);
splitPane.setRightComponent(currentComp);
splitPanel.add(splitPane, BorderLayout.CENTER);
String mergeName = mergeTabName + "/" + tabStr;
String toolTipName = mergeCompAddress + " | " + currentCompAddress;
//_viewTabPane.addTab(mergeName, splitPanel);
addTabToDisplay(mergeName, toolTipName, splitPanel);
splitPane.setDividerLocation(.5);
int mergeIndex = _viewTabPane.indexOfTab(mergeName);
if (mergeIndex != -1)
//_viewTabPane.setToolTipTextAt(mergeIndex, mergeName);
//_viewTabPane.setSelectedIndex(mergeIndex);
//Now lets update our ongoing vector/hashmap
//Create a hash map with the current merged view
Hashtable mergeViews = new Hashtable();
mergeViews.put(mergeCompAddress,mergeComp);
mergeViews.put(currentCompAddress,currentComp);
_qmMergedIndexes.put(mergeName, mergeViews);
catch (Exception ex)
ex.printStackTrace();
merge.add(mergeItem);
String splitKey = StringFactory.getString(splitviewKey);
JMenu splitMenu = new JMenu(splitKey);
splitMenu.setEnabled(false);
//Only allow to split if the current tab has a merged view
if (_qmMergedIndexes.containsKey(tabStr))
splitMenu.setEnabled(true);
Hashtable mergeHash = (Hashtable)_qmMergedIndexes.get(tabStr);
Enumeration enumeration = mergeHash.keys();
while (enumeration.hasMoreElements())
//Create a new menu item for each QM in View
String node = (String)enumeration.nextElement();
JMenuItem mergeItem = new JMenuItem(new AbstractAction(node)
public void actionPerformed(ActionEvent e)
//Work around for java bug in JTabbedPane
_viewTabPane.setSelectedIndex(0);
_viewTabPane.validate();
//End workaround
String splitTabName = e.getActionCommand();
//Get the merge hash for this tab num
Hashtable splitHash = (Hashtable)_qmMergedIndexes.get(tabStr);
Enumeration enumeration = splitHash.keys();
_viewTabPane.removeTabAt(tabNum);
int iIndexOfSplit = 0;
while (enumeration.hasMoreElements())
//Add new pane for the views in the hashtable
String nodeAddress = (String)enumeration.nextElement();
String nodeName = m_model.getQMgrName(nodeAddress);
NavigatorTreePanel currentComp = (NavigatorTreePanel)splitHash.get(nodeAddress);
addTabToDisplay(nodeName,nodeAddress,currentComp);
//Now if this tab this we just added is equal to the menu item of the split menu,
//store this so that we can give focus to it later
if (splitTabName.equals(nodeAddress))
iIndexOfSplit = _viewTabPane.getTabCount()-1;
//Also remove it from our vector of merged tabs
_qmMergedIndexes.remove(tabStr);
//int mergeIndex = _viewTabPane.indexOfTab(splitTabName);
//if (mergeIndex != -1)
if (iIndexOfSplit != -1)
//_viewTabPane.setSelectedIndex(mergeIndex);
_viewTabPane.setSelectedIndex(iIndexOfSplit);
splitMenu.add(mergeItem);
popup.add(menuItem1);
popup.add(merge);
popup.add(splitMenu);
if (popup != null)
Point p = e.getPoint();
popup.show((Component)e.getSource(), (int)p.getX(), (int)p.getY());
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
//Don't forget to add the tree's
addTreeDisplays();
_viewTabPane.addTab(StringFactory.getString("perspective.display.overview.tab"), _treeTabPane);
_viewTabPane.addTab(StringFactory.getString("perspective.display.documentation.tab"), new DocDisplayPanel());
_mainDisplayPanel.add(_viewTabPane, BorderLayout.CENTER);
catch(Exception e)
e.printStackTrace();
private void addTabToDisplay(String nameStr, String toolStr, JComponent c)
if (_viewTabPane == null)
return;
_viewTabPane.addTab(nameStr, c);
int tabNum = _viewTabPane.getTabCount()-1;
if (toolStr != null)
_viewTabPane.setToolTipTextAt(tabNum, toolStr);
_viewTabPane.setSelectedIndex(tabNum);
private void addTreeDisplays()
String starKey = StringFactory.getString(starviewKey);
String treeKey = StringFactory.getString(treeviewKey);
//navigatorTreePanel.expandTreePath(this, true);
String test = MQEPreferencesDialog.getPreferenceValue("expand.leaf.startup");
System.out.println("test{{{{{{{{{{"+test);
//System.out.println("test{{{{{{{{{{"+m_nnode.);
if (treeDisplayPreference == BOTH_DISPLAY)
hyperbolicTreePanel = new HyperbolicTreePanel(m_model);
hyperbolicTreePanel.addQMRequestListener(this);
hyperbolicTreePanel.addTopologySelectionListener(this);
//hyperbolicTreePanel.expandTreePath((TopologyModelNode)m_model.getRoot(), true);
//hyperbolicTreePanel.expandTreePath(m_nnode, true);
_treeTabPane.addTab("", IconFactory.getInstance().getIcon("staricon"), hyperbolicTreePanel, starKey);
navigatorTreePanel = new NavigatorTreePanel(m_model);
navigatorTreePanel.addQMRequestListener(this);
navigatorTreePanel.addTopologySelectionListener(this);
_treeTabPane.addTab("", IconFactory.getInstance().getIcon("treeicon"), navigatorTreePanel, treeKey);
else if (treeDisplayPreference == TREE_DISPLAY)
navigatorTreePanel = new NavigatorTreePanel(m_model);
navigatorTreePanel.addQMRequestListener(this);
navigatorTreePanel.addTopologySelectionListener(this);
_treeTabPane.addTab("", IconFactory.getInstance().getIcon("treeicon"), navigatorTreePanel, treeKey);
else
hyperbolicTreePanel = new HyperbolicTreePanel(m_model);
hyperbolicTreePanel.addQMRequestListener(this);
hyperbolicTreePanel.addTopologySelectionListener(this);
_treeTabPane.addTab("", IconFactory.getInstance().getIcon("staricon"), hyperbolicTreePanel, starKey);
public void addTopologySelectionListener(TopologySelectionListener tsl)
if (tsl != null)
_tsListeners.add(tsl);
public void removeTopologySelectionListener(TopologySelectionListener tsl)
if (tsl != null)
_tsListeners.remove(tsl);
protected void fireTopologySelection(TopologyModelNode node)
for (TopologySelectionListener tsl : _tsListeners)
tsl.receiveTopologySelection(node);
/* TopologySelectionListener methods */
public void receiveTopologySelection(TopologyModelNode node)
//Since this panel can have multiple panels in it's current Tabbed display.
//This class registers to each of the TopologyDisplayPanels as a listener for selections
//This way no matter who is currently active, they will funnel the event to here and it this
//panel will send the event on foward
if (node != null)
fireTopologySelection(node);
/* QMRequestListener methods */
public void receiveQMRequest(TopologyModelNode node)
//Check to see if this node is already in a Tab already
System.out.println("Queue Manager request");
boolean doesExist = false;
int tabNum =0;
//Fix for activity 00033248 TAB PANES FOR IDENTICAL QMANAGERS ON DIFF MACHINES
//The only unique names are in the tooltips so lets just cycle through all the tabs
//and search for this Queue Manager name. Using indexOfTab in JTabbedPane will not
//work here because it will return first location of a matching queue manager name, but
//we could have multiple tabs open with same queue manager name.
for (int i = 0; i < _viewTabPane.getTabCount();i++)
String toolTipStr = _viewTabPane.getToolTipTextAt(i);
//Using the string method for indexOf, covers us when we have a merged tab window
if ((toolTipStr != null) && (toolTipStr.indexOf(((ResourceProxy)node).getAddress().trim()) != -1))
//We already have existing queue manager tab open
doesExist = true;
tabNum = i;
break;
if ( !doesExist)
//Create the tab
NavigatorTreePanel treePanel = new NavigatorTreePanel(new ExpandingModelNode(node));
treePanel.addTopologySelectionListener(this);
// Add the tree panel to a container so that it can be accessed
// for expand/collapse all and tree refresh actions.
qmgrTreeDisplays.add(treePanel);
//Fix for activity 00033248 TAB PANES FOR IDENTICAL QMANAGERS ON DIFF MACHINES
addTabToDisplay(((ResourceProxy)node).getName().trim(), ((ResourceProxy)node).getAddress().trim(), treePanel);
node.getModel().expandNode(node);
else
_viewTabPane.setSelectedIndex(tabNum);
/* PropertyChangeListener methods */
public void propertyChange(PropertyChangeEvent evt){
String propertyChanged = evt.getPropertyName();
if (propertyChanged.equals(MQEDisplayPreferences.treeDisplayProperty)){
int newPreference = ((Integer)evt.getNewValue()).intValue();
changeTreeDisplays(newPreference);
else if (propertyChanged.equals(MQEDisplayPreferences.expandLeafProperty)){
// The enable leaf node preference has changed so update the MQE
// tree displays according to the new preference setting.
refreshTreeDisplays();
else{
// Ignore the property change event.
* Changes the trees displayed by MQE according to
* the preference set by the current user.
* @param the new trees display preference.*/
public void changeTreeDisplays(int preference)
// Assign the new tree display preference.
treeDisplayPreference = preference;
// Recreate the tree display according
// to the new preference.
m_model.removeTreeModelListeners();
_treeTabPane.removeAll();
addTreeDisplays();
refreshTreeDisplays();
* Fully expands the Navigator display trees from the specified node.
* @param the node from which each tree will be fully expanded.
public void expandAll(TopologyModelNode node)
node.expandAll();
final TopologyModelNode fnode = node;
Runnable doTask = new Runnable(){
public void run(){
// Fire a tree structure changed notification for
// the StarTree, (1) because it will not display
// tree nodes correctly without it and (2) it appears
// to be the only tree interested in doing anything
// with it!
fnode.fireTreeStructureChanged(TopologyModel.STRUCTURE_CHANGE);
if (hyperbolicTreePanel != null)
hyperbolicTreePanel.expand(fnode);
if (navigatorTreePanel != null)
navigatorTreePanel.expand(fnode);
for (NavigatorTreePanel navTreePanel : qmgrTreeDisplays){
navTreePanel.expand(fnode);
SwingUtilities.invokeLater(doTask);
* Fully collapses the Navigator display trees from the specified node.
* @param the node from which each tree will be fully collapsed.
public void collapseAll(TopologyModelNode node)
final TopologyModelNode fnode = node;
Runnable doTask = new Runnable(){
public void run(){
if (hyperbolicTreePanel != null)
hyperbolicTreePanel.collapse(fnode);
if (navigatorTreePanel != null)
navigatorTreePanel.collapse(fnode);
for (NavigatorTreePanel navTreePanel : qmgrTreeDisplays){
navTreePanel.collapse(fnode);
SwingUtilities.invokeLater(doTask);
* Will ensure that all tree nodes, starting from the root, are correctly displayed.
public void refreshTreeDisplays()
if (hyperbolicTreePanel != null){
TopologyModel model = hyperbolicTreePanel.getTopologyModel();
if (model != null){
hyperbolicTreePanel.expandTreePath((TopologyModelNode)model.getRoot(), true);
hyperbolicTreePanel.refreshTreeDisplay((TopologyModelNode)model.getRoot());
if (navigatorTreePanel != null){
TopologyModel model = navigatorTreePanel.getTopologyModel();
if (model != null){
navigatorTreePanel.refreshTreeDisplay((TopologyModelNode)model.getRoot());
navigatorTreePanel.repaint();
for (NavigatorTreePanel navTreePanel : qmgrTreeDisplays){
ExpandingModelNode model = (ExpandingModelNode)navTreePanel.getNavigatorTreeModel();
if (model != null){
navigatorTreePanel.expand((TopologyModelNode)model.getRoot());
navTreePanel.refreshTreeDisplay((TopologyModelNode)model.getRoot());
navTreePanel.repaint();
}you really don't need to post all that code, few people will read it, or try to compile/run it.
just a tree in a scrollpane in a frame is all you need, then add the method/problem to the basic,
and post that so its only 20 to 30 lines long.
here's your basic tree/scrollpane/frame, with an expandAll()
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
public void buildGUI()
JTree tree = new JTree();
expandAll(tree);
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(tree));
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
public void expandAll(JTree tree)
int row = 0;
while (row < tree.getRowCount())
tree.expandRow(row);
row++;
public static void main(String[] args)
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}so, is your problem with the expandAll(), or with reading the properties file? -
JTree - Trying to make selection a parent select all children
I have a jtree with checkboxes and titles. This is my first semester working with java, I started out in cpp so I'm pretty new to GUIs. I found an example of a Jtree with checkboxes on the leaf nodes online and was able to modify it to have checkboxes on all nodes.
I have been reading through example code of JTrees but have not been able to figure out how to modify children when a parent is modified. If I clicked on an expandable node with sub nodes, I'd want all checkboxes under it to become checked. I've been trying to figure this out for a few days now.
I tried adding a System.out to the getTreeCellEditorComponent class of the cell editor, to see if that's where I'm supposed to put the code (when i figure out what it is) but it gave unexpected output. Clicking a checkbox the first time called the println once, but after that, each click called println multiple times per click. I'm worried that having the extra calls to the code could mess things up. Where would I want to put code for it to be called only once per time the checkbox is clicked?
Oh, I also tried adding a listener to the checkbox in the renderer but it gave really weird results too.
How does the TreePath class work? I tried some stuff with getClosestPathForLocation but couldn't get anything to work.
Thank you very much for your time and help.Thanks for pointing that out. I had tried a few others but yes his was very close to what I want. I just removed the node icons.
Thanks -
How should I know where I click in jTree, actually I want to read the value of the selected node in JTree.
Thanks
RaheelYou can a implement a selection listener and get the node each time a selection is made.
Or from the API you can glean the method
Object getLastSelectedPathComponent()
Returns the last path component in the first node of the current selection. -
Display JTree in browser using JSP
i have a program that converts xml file in to tree structure(using Swing). When i run this using eclipse then it is working. Swing is an extension of applet , right. I want to embed this in an HTML page(JSP). so that i can display the tree structure. Its gives class not foung error.
It is not posiible to embed it. I think if it extends an Applet then it will display. But i don't know how to convert that. It gives error if i convert.
pls help
CODE:
package TreeGen;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.io.IOException;
import org.w3c.dom.Document;
// Basic GUI components
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
// GUI components for right-hand side
import javax.swing.JSplitPane;
import javax.swing.JEditorPane;
// GUI support classes
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
// For creating borders
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
// For creating a TreeModel
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.*;
public class TreeGen extends JPanel
static Document document;
boolean compress = false;
static final int windowHeight = 660;
static final int leftWidth = 300;
static final int rightWidth = 640;
static final int windowWidth = leftWidth + rightWidth;
public TreeGen()
EmptyBorder eb = new EmptyBorder(5,5,5,5);
BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
CompoundBorder cb = new CompoundBorder(eb,bb);
this.setBorder(new CompoundBorder(cb,eb));
JTree tree = new JTree(new DomToTreeModelAdapter());
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(
new Dimension( leftWidth, windowHeight ));
final
JEditorPane htmlPane = new JEditorPane("text/html","");
htmlPane.setEditable(true);
JScrollPane htmlView = new JScrollPane(htmlPane);
htmlView.setPreferredSize(
new Dimension( rightWidth, windowHeight ));
tree.addTreeSelectionListener(
new TreeSelectionListener()
public void valueChanged(TreeSelectionEvent e)
TreePath p = e.getNewLeadSelectionPath();
if (p != null)
AdapterNode adpNode =
(AdapterNode) p.getLastPathComponent();
htmlPane.setText(adpNode.content());
JSplitPane splitPane =
new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
treeView,
htmlView );
splitPane.setContinuousLayout( false );
splitPane.setDividerLocation( leftWidth );
splitPane.setDividerSize(1);
splitPane.setPreferredSize(
new Dimension( windowWidth + 10, windowHeight+10 ));
this.setLayout(new BorderLayout());
this.add("Center", splitPane );
//return menuBar;
} // constructor
public static void main(String argv[])
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse("C:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/parser1/sample.xml");
makeFrame();
} catch (SAXException sxe){
System.out.println("ERROR");
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} // main
public static void makeFrame()
JFrame frame = new JFrame("DOM Echo");
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
final TreeGen echoPanel =
new TreeGen();
frame.getContentPane().add("Center", echoPanel );
frame.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int w = windowWidth + 10;
int h = windowHeight + 10;
frame.setSize(w, h);
frame.setVisible(true);
} // makeFrame
static final String[] typeName = {
"none",
"Element",
"Attr",
"Text",
"CDATA",
"EntityRef",
"Entity",
"ProcInstr",
"Comment",
"Document",
"DocType",
"DocFragment",
"Notation",
static final int ELEMENT_TYPE = 1;
static final int ATTR_TYPE = 2;
static final int TEXT_TYPE = 3;
static final int CDATA_TYPE = 4;
static final int ENTITYREF_TYPE = 5;
static final int ENTITY_TYPE = 6;
static final int PROCINSTR_TYPE = 7;
static final int COMMENT_TYPE = 8;
static final int DOCUMENT_TYPE = 9;
static final int DOCTYPE_TYPE = 10;
static final int DOCFRAG_TYPE = 11;
static final int NOTATION_TYPE = 12;
static String[] treeElementNames = {
"slideshow",
"slide",
"title", // For slideshow #1
"slide-title", // For slideshow #10
"item",
boolean treeElement(String elementName) {
for (int i=0; i<treeElementNames.length; i++) {
//System.out.println(treeElementNames);
if ( elementName.equals(treeElementNames[i]) )
return true;
return false;
public class AdapterNode
org.w3c.dom.Node domNode;
public AdapterNode(org.w3c.dom.Node node)
domNode = node;
public String toString()
String s = typeName[domNode.getNodeType()];
String nodeName = domNode.getNodeName();
if (! nodeName.startsWith("#"))
s += ": " + nodeName;
if (compress)
String t = content().trim();
int x = t.indexOf("\n");
if (x >= 0) t = t.substring(0, x);
s += " " + t;
return s;
if (domNode.getNodeValue() != null)
if (s.startsWith("ProcInstr"))
s += ", ";
else
s += ": ";
// Trim the value to get rid of NL's at the front
String t = domNode.getNodeValue().trim();
int x = t.indexOf("\n");
if (x >= 0) t = t.substring(0, x);
s += t;
return s;
public String content()
String s = "";
org.w3c.dom.NodeList nodeList = domNode.getChildNodes();
for (int i=0; i<nodeList.getLength(); i++)
org.w3c.dom.Node node = nodeList.item(i);
int type = node.getNodeType();
//System.out.println(type);
AdapterNode adpNode = new AdapterNode(node); //inefficient, but works
if (type == ELEMENT_TYPE)
if ( treeElement(node.getNodeName()) ) continue;
s += "<" + node.getNodeName() + ">";
s += adpNode.content();
s += "</" + node.getNodeName() + ">";
else if (type == TEXT_TYPE)
s += node.getNodeValue();
else if (type == ENTITYREF_TYPE)
s += adpNode.content();
else if (type == CDATA_TYPE)
StringBuffer sb = new StringBuffer( node.getNodeValue() );
for (int j=0; j<sb.length(); j++)
if (sb.charAt(j) == '<')
sb.setCharAt(j, '&');
sb.insert(j+1, "lt;");
j += 3;
else if (sb.charAt(j) == '&')
sb.setCharAt(j, '&');
sb.insert(j+1, "amp;");
j += 4;
s += "<pre>" + sb + "\n</pre>";
return s;
public int index(AdapterNode child)
int count = childCount();
for (int i=0; i<count; i++)
AdapterNode n = this.child(i);
if (child.domNode == n.domNode) return i;
return -1; // Should never get here.
public AdapterNode child(int searchIndex)
org.w3c.dom.Node node =
domNode.getChildNodes().item(searchIndex);
if (compress)
int elementNodeIndex = 0;
for (int i=0; i<domNode.getChildNodes().getLength(); i++)
node = domNode.getChildNodes().item(i);
if (node.getNodeType() == ELEMENT_TYPE
&& treeElement( node.getNodeName() )
&& elementNodeIndex++ == searchIndex)
break;
return new AdapterNode(node);
public int childCount()
if (!compress)
return domNode.getChildNodes().getLength();
int count = 0;
for (int i=0; i<domNode.getChildNodes().getLength(); i++)
org.w3c.dom.Node node = domNode.getChildNodes().item(i);
if (node.getNodeType() == ELEMENT_TYPE
&& treeElement( node.getNodeName() ))
++count;
return count;
public class DomToTreeModelAdapter
implements javax.swing.tree.TreeModel
public Object getRoot()
return new AdapterNode(document);
public boolean isLeaf(Object aNode)
AdapterNode node = (AdapterNode) aNode;
if (node.childCount() > 0) return false;
return true;
public int getChildCount(Object parent)
AdapterNode node = (AdapterNode) parent;
return node.childCount();
public Object getChild(Object parent, int index)
AdapterNode node = (AdapterNode) parent;
return node.child(index);
public int getIndexOfChild(Object parent, Object child)
AdapterNode node = (AdapterNode) parent;
return node.index((AdapterNode) child);
public void valueForPathChanged(TreePath path, Object newValue)
private Vector listenerList = new Vector();
public void addTreeModelListener(TreeModelListener listener)
if ( listener != null
&& ! listenerList.contains( listener ) )
listenerList.addElement( listener );
public void removeTreeModelListener(TreeModelListener listener)
if ( listener != null )
listenerList.removeElement( listener );
public void fireTreeNodesChanged( TreeModelEvent e )
Enumeration listeners = listenerList.elements();
while ( listeners.hasMoreElements() )
TreeModelListener listener =
(TreeModelListener) listeners.nextElement();
listener.treeNodesChanged( e );
public void fireTreeNodesInserted( TreeModelEvent e )
Enumeration listeners = listenerList.elements();
while ( listeners.hasMoreElements() )
TreeModelListener listener =
(TreeModelListener) listeners.nextElement();
listener.treeNodesInserted( e );
public void fireTreeNodesRemoved( TreeModelEvent e )
Enumeration listeners = listenerList.elements();
while ( listeners.hasMoreElements() )
TreeModelListener listener =
(TreeModelListener) listeners.nextElement();
listener.treeNodesRemoved( e );
public void fireTreeStructureChanged( TreeModelEvent e )
Enumeration listeners = listenerList.elements();
while ( listeners.hasMoreElements() )
TreeModelListener listener =
(TreeModelListener) listeners.nextElement();
listener.treeStructureChanged( e );I actually had to do this a few months ago. There are ways to perform this kind display using various distributed object (i.e., using MS Word OLE objects to "interpret" the byte for you, etc.). But this soon got extremely difficult to manage (and I actually had to use Perl/CGI for the majority of it).
The solution I went with was to implement a "cache" directory on the web server. Basically, the JSP/Servlet can simply check the cache and if file not there, create it from the database. Then send a redirect back to the browser to this newly-created file. The browser will then appropriately open the document. I tested this with both Netscape and IE browsers and common MIME types such as text files, MS Office docs, zip files, PDFs, RTFs.
Not ideal, but unfortunately the best I came up with.
Maybe you are looking for
-
Dreamweaver CS3 First Run problems
Installing Dreamweaver CS3 Saga. I've had some trouble and despite looking for answers and trying a few things I'm getting nowhere. I've included a timeline of events below to help explain. 0) System: New Dell Opliplex 745 running XP Pro Sp2 and netw
-
How to use a ring to select axes of an xy graph
Hello, I'm a relatively new user to Labview and so far have found it comepletely stupifying. I am trying to use drop down menus (ring) to select the axes of an xy-graph. I'm using the Daq Assistant to gather my signal which consist of three parts (
-
How to Stop the timer in the TimedTrigger UI element when an event occurs
Hi all I have a webdynpro View where i have many UI elements(which supports an event) along with TimedTrigger UI element. Now my requirement , i want to check at single line if any event happens in the View then i want to stop the timer in the Timed
-
Authorization object P_APPL
Hi experts, How should I use authorization object P_APPL in order to give access to all infotypes except for one concrete? Is there any way I can only specify which the user should not have access instead of specify to which it should? Thanks, mforma
-
Wrong login prompts, how do I remove them?
On several sites that I log onto regularly, I've made syntax errors while typing it in and have had to re enter my information. When I return to that site, I not only get the prompt for the correct one, which I want, I also get a prompt for the accid