Domanda

Ho un JTree che implementa logica di selezione più.

Questa grande opera quando faccio tutti i miei selezioni utilizzando il mouse + tasto Ctrl premuto. Se l'utente fa le selezioni con il tasto Ctrl non pressata si rompe la mia logica.

Non riesco a capire perché si rompe ma penso che una possibile soluzione è quello di indicare sempre il TreeSelectionModel che la selezione è stata fare con il tasto Ctrl premuto.

Che cosa suggerisce?

È stato utile?

Soluzione

Credo di aver trovato la soluzione

Sarà necessario estendere la JTree e DefaultTreeSelectionModel.

JTree metodi rilevanti:

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Implement selection using "adding" only logic. //
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

@Override
public void setSelectionPath(TreePath path) {

    System.out.println("MLDebugJTree: setSelectionPath(" + path + ")");

    addSelectionPath(path);

    return;
    //super.setSelectionPath(path);
}

@Override
public void setSelectionPaths(TreePath[] paths) {

    System.out.println("MLDebugJTree: setSelectionPaths(" + paths + ")");

    addSelectionPaths(paths);

    return;
}

@Override
public void setSelectionRow(int row) {

    System.out.println("MLDebugJTree: setSelectionRow(" + row + ")");

    addSelectionRow(row);

    return;
    //super.setSelectionRow(row);
}

@Override
public void setSelectionRows(int[] rows) {

    System.out.println("MLDebugJTree: setSelectionRows(" + rows + ")");

    addSelectionRows(rows);

    return;
    //super.setSelectionRows(rows);
}

DefaultSelectionModel metodi rilevanti:

package com.ml.tree2.model.impl;

import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreePath;

public class MLTreeSelectionModel extends DefaultTreeSelectionModel {
private static final long serialVersionUID = -4270031800448415780L;

@Override
public void addSelectionPath(TreePath path) {
    // Don't do overriding logic here because addSelectionPaths is ultimately called.
    super.addSelectionPath(path);
}

@Override
public void addSelectionPaths(TreePath[] paths) {
    if(paths != null) {
        for(TreePath path : paths) {

            TreePath[] toAdd = new TreePath[1];
            toAdd[0] = path;

            if (isPathSelected(path)) {
                // If path has been previously selected REMOVE THE SELECTION.
                super.removeSelectionPaths(toAdd);
            } else {
                // Else we really want to add the selection...
                super.addSelectionPaths(toAdd);
            }
        }
    }
}

HTH.

Altri suggerimenti

Un'altra soluzione sarebbe quella di estendere semplicemente BasicTreeUI e modificare il comportamento di selezione in base alle proprie esigenze:

public class MultiSelectionTreeUI extends BasicTreeUI
{

    @Override
    protected boolean isToggleSelectionEvent( MouseEvent event )
    {
        return SwingUtilities.isLeftMouseButton( event );
    }
}

E quindi impostare che ui sul JTree:

JTree tree = new JTree();
tree.setUI( new MultiSelectionTreeUI() );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top