JTree disappears all by developing the node. Maybe a problem from hashCode().
Hi, I have a JTree with custom template. I test with the following structure:+ root
- + category
- + file
When the application starts, root and the category are visible, the file is hidden. When I try to expand the class node, the JTree set disappears. A click away in the place previously occupied by the JTree result in a java.lang.NullPointerException at javax.swing.plaf.basic.BasicTreeUI$ Handler.handleSelection ().
The nodes that I use represent a simple tree, where each node has its value and a LinkedList of his children. The model simply translated this to the 'language' of TreeModel.
It is essential for me to properly define equals() and hashCode() for nodes. I discovered that if hashCode() of the nodes does not rely on a list of the children, everything works fine. But I want to rely on the children!
I use 1.6.0_22 JRE on Win7.
Where should I do something wrong, please?
Thanks a lot for any help ;)
PS: I managed to get around that - in a TreeSelectionListener I call tree.setModel (tree.getModel ()) and then I restore the nodes selected and expanded. It works, but of course I'd rather have a cleaner solution. In addition, this seems like a bug in JRE for me.
Included NBS:
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class JTreeTest
{
public static void main(String[] args)
{
// setup the tree
final File list = new File();
list.setValue("root");
File category = new File();
list.getFiles().add(category);
category.setValue("category");
File file = new File();
category.getFiles().add(file);
file.setValue("file");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
JFrame frame = new JFrame();
// setup the JTree
JTree tree = new JTree(new MyModel(list));
frame.add(tree);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(new Dimension(200, 100));
frame.setVisible(true);
}
});
}
// this model is very very basic and I don't see much space here for errors...
static class MyModel implements TreeModel
{
protected File root;
protected List<TreeModelListener> treeModelListeners = new LinkedList<TreeModelListener>();
public MyModel(File root)
{
this.root = root;
}
@Override
public File getRoot()
{
return root;
}
@Override
public Object getChild(Object parent, int index)
{
if (index < 0)
return null;
if (!(parent instanceof File))
return null;
File fParent = (File) parent;
try {
return fParent.getFiles().get(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
@Override
public int getChildCount(Object parent)
{
if (!(parent instanceof File))
return 0;
File fParent = (File) parent;
return fParent.getFiles().size();
}
@Override
public boolean isLeaf(Object node)
{
return getChildCount(node) == 0;
}
@Override
public void valueForPathChanged(TreePath path, Object newValue)
{
if (!(newValue instanceof File))
return;
if (path.getParentPath() == null) {
fireTreeNodesChanged(new TreeModelEvent(this, path, null, null));
} else {
fireTreeNodesChanged(new TreeModelEvent(this, path.getParentPath(), new int[] { getIndexOfChild(path
.getParentPath().getLastPathComponent(), newValue) }, new Object[] { newValue }));
}
}
@Override
public int getIndexOfChild(Object parent, Object child)
{
if (parent == null || child == null)
return -1;
if (!(parent instanceof File) || !(child instanceof File))
return -1;
File fParent = (File) parent;
return fParent.getFiles().indexOf(child);
}
@Override
public void addTreeModelListener(TreeModelListener l)
{
treeModelListeners.add(l);
}
@Override
public void removeTreeModelListener(TreeModelListener l)
{
treeModelListeners.remove(l);
}
public void fireTreeNodesChanged(TreeModelEvent e)
{
for (TreeModelListener listener : treeModelListeners) {
listener.treeNodesChanged(e);
}
}
}
static class File
{
static int fileId = 0;
int id;
List<File> files = null;
String value = null;
public File()
{
// ensure each item will have a really unique identifier, so no equals() collisions should occur
id = fileId++;
}
public List<File> getFiles()
{
if (files == null)
files = new LinkedList<File>();
return files;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
// generated by Eclipse code helpers
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
// ///////HERE IT IS////////
// if you uncomment the following line, the list will get empty when you expand the second category and
// any following clicks in any place that should be occupied by an item will result in a
// NullPointerException
//
result = prime * result + ((files == null) ? 0 : files.hashCode());
//
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
File other = (File) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
if (files == null) {
if (other.files != null)
return false;
} else if (!files.equals(other.files))
return false;
if (id != other.id)
return false;
return true;
}
@Override
public String toString()
{
return "File [value=" + value + "]";
}
}
}
peci1 wrote:
If you think that it is not possible to have directly implemented in nodes to the evolution of codes in a JTree.
If not know because the javadoc, said he. Once again:
In the same way JTree and its associated classes place TreePaths in maps. Thus, if a node is requested twice, return values must be equal (using the equals method) and have the same hash code.
I hope that the treeStructureChanged event would help here...
It might work if you launch a node struct event has changed because you have changed the hash code of the real parent when you changed the children of it. But catch all these traps (restore expand status, etc.) seems too much work just to avoid a node wrapper class.
Tags: Java
Similar Questions
-
CD player passes all tests but the computer does not start from cd player
CD player passes all the tests, but the computer does not start from cd drive. entered the bios and it specifically indicate to boot from the cd drive. Windows 7 installation disc does not start. Win 7 disk works in another computer to start. Why is it not this cd player read the disc and from the win 7 installation process?
I see this problem on systems having older IDE and SATA drives requiring firmware updates and assigned to the region 1 aka they boot from XP CD, but not DVD windows 7or media.
-
I have a Dvd-R/CD player David encoder and I uninstalled Roxio because it allows me to copy onto a Dvd - r. But now windows won't allow me not to one, it gives just this error message:
«Problem copy»
Windows encountered a problem when trying to copy this folder.
What do you want Windows to do? "I think it might have something to do with the fact that I had there before only on cd player, but I don't know because windows does show that it is a Dvd Player when a dvd is in.
Hi SanithDemil,
· What it shows as in Windows Explorer?
Method 1: Run the next Fixit tool to check the issue.
Your CD or DVD drive cannot read or write media
Method 2:
Step 1: Uninstall and reinstall the Device Manager DVD player. Automatically search for updates to the firmware on the manufacturer's Web site.
a. click the Start button and then right click on my computer. Click on properties.
b. click on the Hardware tab, and then click Device Manager.
c. now, expand the CD-ROM/DVD-ROM drives, click with the right button on the CD-ROM/DVD-ROM drive and click on uninstall.
d. restart the computer.
Step 2: Log on to the manufacturer of the DVD drive and install the latest updates from driver\firmware.
-
I'm reinstalling CS3 Web Standard after a hard disk crash. I have my serial number, but after entering, it does a check of upgrade, and at the time wherever I upgraded I also did a single switch from Mac to PC - so my product calling (Studio MX Mac) has a serial number starting with "WSM600"... ", while the upgrade check won't accept - Studio MX Mac is not yet available as an option. This happened once before and I had to work with a representative by phone (I had to click in a hidden part of one of the activation screens to get a number, if I remember correctly), but apparently Adobe that does more. And the page of "www.adobe.com/go/activiation" the phone message directs you to doesn't seem to have an option for this problem either.
So, what is the replacement or her work around this problem now?
Hi lkauffman,
The serial number must be unlocked for activation.
Please contact customer support.
You can even join our chat support at http://adobe.ly/yxj0t6
Kind regards
Rave
-
In Lightroom, DEVELOP the image does not copy from before to after after using the buttons below.
Is it possible LR shows before and after the same?
By clicking on a button with >< can="" make="" them="" the="">
You can reset the State 'Before' of a picture by right-clicking on a stage in the history panel.
Press the [\] to also get a 'before '.
-
I have try the Lightroom CC and all my development tools have disappeared. When I select develop, the sidebar is completely blank - black - nothing. Where all the settings go and how can I get back them?
If the Panel is visible and completely empty, open the window-> Menu panels and put checkmarks in the ones you want to display. Alternatively, you can simply press Ctrl/1-6 to bring the different sections. If the sidebar is gone, click the midway small gray triangle on the right side of your screen. Click twice to make it remain displayed.
-
When I use the library function node call in real time, is loaded only once for all or load the DLL every time when it is called?
I have a critical application in real time, in which I use a piece of DLL function developed in C++. It is ok? Make sure any senior developer?
Thank you in advance.
The user interface thread is the thread that is used to update the user interface. It's slow. And it's supposed to be that way because humans are slow.
The call library function node can be configured as this thread to use in the configuration for it dialog box. Please visit the LabVIEW documentation on how to do this.
-
At the time where Firefox has become very slow, I've been running Chrome for several versions. I'm now trying to switch to Firefox. I have 27.0.1. My bookmarks toolbar disappears whenever I close the browser. I tried all the recommendations I can find including zero and change of name, etc, but nothing has failed to cure the problem. Restore default used to work for the current session, but now it does not work. I'm about to give up and stick with Chrome as the bookmarks toolbar problem is driving me crazy. I'm about to try to uninstall and reinstall Firefox. And this will be my last resort, unless you get a solution to this problem. I've been a Firefox user since its inception up to 16 or 17 years old and like Firefox, but this seems to be common according to my research. BOOKMARKS BAR IS A MUST HAVE!
If you like Advanced SystemCare software that could restore some files from older versions to protect these files based on changes, and then check the settings or uninstall this software.
You can also check the problems caused by a corrupt localstore.rdf file.
-
I moved some pictures album and I 'cmd + dele' pictures, but it disappears completely even in the album "All photos", how can I find those back?
Restore the photos removed from the album "recently deleted".
The key combination command + delete ⌘⌫ will actually delete the photos from your library, not only to remove it from the album. If you want to remove photos from albums, only the delete key, not use command-DELETE.
-
Support - all my messages saved in my personal files have disappeared (some back to the year 2000). Obviously, very devastating to lose 10 years of memories - some ' one can help me to recover?
I wouldn't have the slightest idea of Hotmail. I'm sorry. Ask the experts from Hotmail here.Windows Live Solution Center Hotmail Forum
http://windowslivehelp.com/forums.aspx?ProductID=1 -
Every thing seems to have returned to normal, but when I restarted my computer all the icons on my desktop and all programs in the start menu disappeared, and yet, all programs are always in: Control Panel > add/REMOVE programs. I can still connect to the internet by going to-Start > my computer > view > go to the desktop. How to get back all the icons on my desktop. I tried a click right, but in vain. I also ran a scan complete antivirus. any help would be appreciated.
Thank you Joel S. Before I could put your advice into practice a friend recommended a friend who advised me to do a system restore, which he said he would be happy to do it for me. So look like an easy option, I agreed. It seems to have solved the problem (fingers crossed), but now it seems that I need to format the hard drive. If you have any tips on formatting hard drive I'm all ears. Thank you Owen
-
Original title: help & support files
Windows 7 Professional. All help & support files have disappeared. I get the message "help and support files cannot start."
How to return please?.
Perhaps. An analysis of file system I think would replace the help files, but you can try it, there is no harm. Just open a command prompt (right click and "open as Administrator"), and then type sfc/scannow
-
My panels disappeared. How to make a comeback? (I all checked, including the histogram, layers, actions and adjustments)
Hello madeline,
Please check that you have selected the correct "essentials" workspace, if you already correct then try to restore Photoshop preferences to default.
Steps to follow:
To re-create the Photoshop preferences files, start the application while pressing Ctrl + Alt + Shift (Windows) or command + Option + shift (Mac OS). Then, click Yes to the message, "Delete the Adobe Photoshop settings file?"
Let us know if that helps.
Kind regards
~ Mohit
-
Hello. I have download CC and installed the trail of the illustrator of last week, but it disappears all of a sudden, I couldn't find it on my computer and I try to reinstall, but it says that Setup could not initialize. I use win8
Please see the links below.
- Error: "cannot install initialize | Windows
- The Adobe CC Installer failed to initialize [windows OS]
- Creative cloud Installer fails to boot into OSX Mavericks
Hope this will help you.
Kind regards
Hervé Khare
-
After importing the files in lightroom cc2015.2, they are all less exposed and developing the show exposure to-5. Help, please.
Hi paulw,
For existing images whose exposure have changed, please select all and go to photo > develop settings > reset.
For import next session, please follow the steps below:
- Turn off apply tone auto under Lightroom preferences settings
- Restart Lightroom and then import images again.
Concerning
Sarika
Maybe you are looking for
-
How can I get my outgoing mail to work using Network Solutions e-mail
I use Network Solutions to host my URL and email. I got the job of Thunderbird, but my computer crashed. I had to reinstall Windows and Thunderbird. Now, I can't have my outgoing email to work. I tried different settings of SSL and Port authenticatio
-
Can I use a brush Sansu on my iPad 2 Air?
Can I use on my iPad Air2 Sansu brush?
-
Original title: is it a scam? This morning a guy called me and said he was from California United States Windows, our computer was strange before he help us. We the have not called before, he's a hacker? One name was John Rex
-
Is there a site that has a variety of useful accessories of rocket?
I love my rocket and use it all the time, but I would like something like a wake-up call or any other accessory pregnant) that I could plug my rocket (which is made especially for the "rocket", not something that uses only a 3.5 mm headphone jack). T
-
Activation of Windows Vista without an Internet connection
I had Windows Vista Starter on my computer for over 3 years. Recently I had to change my computer motherboard. Since it was a hardware change Windows started to asking me to activate Windows. I entered the product key and it says I need to connect to