JTree aktualisieren via HashTable

Status
Nicht offen für weitere Antworten.

Nicole81

Aktives Mitglied
Huhu,

ich bin kurz vorm Wochenende mal wieder am verzweifeln :-(

Diesmal ärgere ich mich mit einen JTree rum.

Ich habe folgenden Code:

Code:
public class TeCoMaProjectExplorer extends JInternalFrame
{
	private JPanel pProjectExplorerPanel;

	private JTree trProjectExplorerTree;

	private Hashtable htTree;
	private Vector<String> vCommands;
	private Vector<String> vOutputs;

	public TeCoMaProjectExplorer()
	{
		super("Project Explorer", true, false, true, true);

		htTree = new Hashtable();
		vCommands = new Vector<String>();
		vOutputs = new Vector<String>();
		htTree.put("Outputs", vOutputs);
		htTree.put("Commands", vCommands);

		trProjectExplorerTree = new JTree(new JTree.DynamicUtilTreeNode("Project", htTree));

		pProjectExplorerPanel = new JPanel(new BorderLayout());
		pProjectExplorerPanel.add(new JScrollPane(trProjectExplorerTree), BorderLayout.CENTER);

		getContentPane().add(pProjectExplorerPanel);

		setVisible(true);
	}

	public void setBounds(Dimension d)
	{
		super.setBounds(0, 0, d.width / 4, d.height);
	}


	public void addCommandToTree(String command)
	{
		vCommands.addElement(command);
		updateUI();
	}
}

Ich erstelle einen simplen Tree via HashTable. Der Tree wird korrekt erstellt.
Nun rufe ich "addCommandToTree" auf und klappe dann den Node "Command" auf und alles wunderbar, die Einträge sind drin.

Füge ich allerding erst Einträge zu einem Node, nachem dieser aufgeklappt ist, aktualisiert sich gar nichts (mehr)?!!?!

Ich verstehs nicht :-(((

Danke Jungs, bitte rettet mir das Wochenende, sonst finde ich keine Ruhe :bahnhof:
 
habe es auch schon mit einem treemodel und der Methode "nodeStructureCHanged" versucht, aber so geht es auch nicht.
 
Also ich habe jetzt einen Weg gefunden, wie es klappt, allerding verstehe ich den Sinn dahinter nicht ganz:

Jedes mal wenn ich etwas dem Tree hinzufüge (addCommandToTree) erstelle ich eine neue Model-Instanz und weise sie dem Tree zu. Warum genügt es nicht einfach bei der existierenden Model-Instanz ein "nodesChanged" aufzurufen??

Code:
public class TeCoMaProjectExplorer extends JInternalFrame
{
	private static final long serialVersionUID = 1L;

	private JPanel pProjectExplorerPanel;

	private JTree trProjectExplorerTree;
	private DefaultTreeModel tmProjectExplorerTreeModel;
	
	private Hashtable<String, Vector<String>> htTree;
	private Vector<String> vCommands;
	private Vector<String> vOutputs;

	public TeCoMaProjectExplorer()
	{
		super("Project Explorer", true, false, true, true);

		htTree = new Hashtable<String, Vector<String>>();
		vCommands = new Vector<String>();
		vOutputs = new Vector<String>();
		htTree.put("Outputs", vOutputs);
		htTree.put("Commands", vCommands);

		trProjectExplorerTree = new JTree();
		tmProjectExplorerTreeModel = new DefaultTreeModel(new JTree.DynamicUtilTreeNode("Project", htTree));
		trProjectExplorerTree.setModel(tmProjectExplorerTreeModel);
		
		pProjectExplorerPanel = new JPanel(new BorderLayout());
		pProjectExplorerPanel.add(new JScrollPane(trProjectExplorerTree), BorderLayout.CENTER);

		getContentPane().add(pProjectExplorerPanel);

		setVisible(true);
	}

	public void setBounds(Dimension d)
	{
		super.setBounds(0, 0, d.width / 4, d.height);
	}


	public void addCommandToTree(String command)
	{
		vCommands.addElement(command);
		tmProjectExplorerTreeModel = new DefaultTreeModel(new JTree.DynamicUtilTreeNode("Project", htTree));
		trProjectExplorerTree.setModel(tmProjectExplorerTreeModel);
		trProjectExplorerTree.repaint();
		trProjectExplorerTree.updateUI();
		
		
	}
}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben