Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JTree vergleich klappt nicht...

Empfohlene Antworten

Hallo...

Ich hätte da nochmal eine kleine Swing Frage und

hoffe jemand kann mir helfen.

Wenn ich bei mir einen Eintrag

im TreePfad angewählt habe und dann das MenuItem des

Trees mit equals vergleiche

(auch wenn ich das Item vorher mit toString parse) schlägt der Vergleich fehl.

Ich vergleiche mit dem Namen des Eintrags(String).

Wenn ich in der if Anweisung mit Eintrag ausgewählt != null vergleiche klappt es,daran kann es also nicht

liegen.

Hat jemand noch eine andere Möglichkeit?

Vielen Dank im vorraus...

Mark

Hallo ...

Hier ist die Methode...

JTree getTestTree() {

DefaultMutableTreeNode root,mapObject;

root = new DefaultMutableTreeNode("Map");

mapObject = new DefaultMutableTreeNode("MapObject");

root.add(mapObject);

JTree tree = new JTree(root);

TreeSelectionModel tsm = new DefaultTreeSelectionModel();

tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

tree.setSelectionModel(tsm);

tree.setRootVisible(true);

tree.addTreeSelectionListener(new TreeSelectionListener()

{

public void valueChanged(TreeSelectionEvent event)

{

TreePath tp = event.getPath();

tp.toString();

if (tp.equals("MapObject")) {

System.out.println("MapObject selektiert");

}

}

});

return tree;

}

Tut mir leid,doch das funktioniert trotzdem nicht.

Aber mache ich bei meinem Codebeispiel nicht dasselbe was du meinst?

Nur das ich halt direkt mit einem TreePath-Objekt vergleiche?

public void valueChanged(TreeSelectionEvent event)

{

TreePath tp = event.getPath();

tp.toString();

if (event.getPath().toString().equals("MapObject")){

if (tp.equals("MapObject")) {

System.out.println("MapObject selektiert");

}

Hallo

Ich habe es jetzt genauso gemacht wie du geschrieben hast.

Und es klappt leider nicht...

Hallo nochmal...

Habe jetzt doch eine Möglichkeit gefunden.

Habe einfach mal den Selektierten Pfad ausgelesen und gesehen, das die Strings in [ ] gesetzt sind.

public void valueChanged(TreeSelectionEvent event)

{

TreePath tp = event.getNewLeadSelectionPath();

if (tp.toString().equals("[Map, MapObject]"))

{

System.out.println("Object selektiert: "+tp.toString());

MapObject mapObj = new MapObject();

}

} Vielen Dank nochmal für deine Hilfe

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.