• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jtree - message becomes blurry when scrolls

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have implemented JTree which is dynamically updated. It works like charm except when the horizonal widht is enough to bring into picture the horizontal scroll bar. When the bar comes up, the message becomes fuzzy. In stread of "user:message" it's like "user:mesmes" or "useuseme". When I click on the node which has this blurry message, of course, that node displays the correct value. This problem comes up only when the scroll bar comes. Please have a look at the code. All suggestions are welcome.
Thank YOu.
- Walk Rustin
package com.info.chatclient;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
public class DynamicTree extends JPanel {
protected DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
protected JTree tree;
private ChatClient client;
MyTreeSelectionListener myTreeSelectionListener;
boolean isLoadingData=true;
public DynamicTree(ChatClient c) {
super(new GridLayout(1,0));
client = c;
try {
/** UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ); */
}catch( Exception e ) {
System.out.println(e);
}
rootNode = new DefaultMutableTreeNode("Welcome");
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);

/** Tell the tree it is being rendered by custom cell renderer */
tree.setCellRenderer( new CustomCellRenderer() );
myTreeSelectionListener = new MyTreeSelectionListener();
tree.addTreeSelectionListener(myTreeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane);
}
public String getSelectedMessageID(){
isLoadingData=false;
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
String messageID;
if (parentPath == null) {
messageID= "-1";
} else {
parentNode = (DefaultMutableTreeNode)(parentPath.getLastPathComponent());
if(parentNode.isRoot()){
messageID= "0";
}else {
MessageInfo messageInfo = (MessageInfo)parentNode.getUserObject();
messageID = messageInfo.getMessageID();
/** also change the status of the node from new to not-new */
messageInfo.setIsNew(false);
parentNode.setUserObject(messageInfo);
}
}
return messageID;
}

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 parentNode,Object child) {
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parentNode,Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
if (parentNode == null) {
parentNode = rootNode;
}
treeModel.insertNodeInto(childNode, parentNode,parentNode.getChildCount());
/** Make sure the user can see the lovely new node. */
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}

public DefaultMutableTreeNode getMatchingNode(String messageID){
TreeModel model = tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
DefaultMutableTreeNode node = null;
if (root != null){
for (Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements() {
DefaultMutableTreeNode current = (DefaultMutableTreeNode)e.nextElement();
if (current.isRoot()){
//
} else{
MessageInfo messageInfo = (MessageInfo)current.getUserObject();
if (messageID.equals(messageInfo.getMessageID())) {
node = current;
break;
}
}// end of current.isRoot()
}// end of for loop
} // end of if loop
return node;
}// end of method

public void addTheNewNode(String messageID, MessageInfo messageInfo){

DefaultMutableTreeNode selectedNode = getMatchingNode(messageID);
addObject(selectedNode, messageInfo);
/** make sure that the newly added line shows up in the message panel */
myTreeSelectionListener.manageSelection(messageInfo.getMessageID());
/** use this only if you want nothing be selected
tree.clearSelection(); */
/** disable the send screen */
client.sendMessagePanel.initialize_sendScreen_to_disable();
}
/**** ********************************************************/
/** NEW CLASS */
/**** ********************************************************/
class MyTreeSelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
/** enable the send message panel */
client.sendMessagePanel.enable_screen();
client.sendMessagePanel.user_input_textfield.requestFocus();
manageSelection(getSelectedMessageID());
}//valueChanged

public void manageSelection(String messageID){
client.setSelectedMessageID(messageID);
MessageInfo messageInfo = new MessageInfo();
boolean moreRecords = true;
Vector useridV = new Vector();
Vector displayMessageV = new Vector();
Vector messageIDV = new Vector();
while(moreRecords){
String message = (String)client.messagesHashTable.get(messageID);
if (message == null){
moreRecords = false;
} else {
messageInfo.setInputMessage(message);
String USERID = messageInfo.getUserID();
String NMID = messageInfo.getMessageID();
String TMID = messageInfo.getToMessageID();
String BMSG = messageInfo.getBMSG();
useridV.addElement(USERID);
displayMessageV.addElement(BMSG + "\n");
messageIDV.addElement(NMID);
messageID = TMID;
}//else
}// while
int numberOfElements = useridV.size();
/**start printing the messages to the window now */
if (useridV.size() > 0){
client.myTable.m_data.removeAll();
for (int i=useridV.size()-1;i>=0 ;i-- ){
String userid = (String)useridV.elementAt(i);
String messageid = (String)messageIDV.elementAt(i);
String BMSG = (String)displayMessageV.elementAt(i);
if (!isLoadingData){
client.myTable.m_data.add(userid + ":", messageid + " - " + BMSG);
}
/** numberOfElements minus i is done to reverse the order of entry */
client.smallHashTable.put(Integer.toString(numberOfElements - i),messageid);
}//for loop
if (!isLoadingData){
client.myTable.table.tableChanged(new TableModelEvent(client.myTable.m_data));
int rowCount = client.myTable.m_data.getRowCount();
int columnCount = client.myTable.m_data.getColumnCount();
for (int i=0;i<rowCount ;i++ ) {
for(int j=0;j<columnCount;j++){
setValueAt(client.myTable.m_data.getValueAt(i,j),i,j);
}
}
client.myTable.table.repaint();
} // if isLoadingData
}//if
}//end of method
public void setValueAt(Object value,int nRow, int nCol) {
TableCellRenderer renderer= client.myTable.table.getCellRenderer(nRow,nCol);
Component c=renderer.getTableCellRendererComponent(client.myTable.table,value,true,true,nRow,nCol);
Dimension size=c.getPreferredSize();
// Adjust row's height
int width = client.myTable.table.getColumnModel().getColumn(nCol).getWidth();
int rowHeight = c.getPreferredSize().height;
client.myTable.table.setRowHeight(nRow, rowHeight - 15);
}
}//class MyTreeSelectionListener
}// class DynamicTree
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic