TreeLineselection: Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Die Seite wurde neu angelegt: „<code=java>/* * TreeLineselection.java * * Normally, when a node is selected, the tree marks only the text. * This shows one way to mark the whole selected li…“
 
KKeine Bearbeitungszusammenfassung
 
(2 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
<code=java>/*
Gewöhnlich wenn ein Knoten selektiert ist, hebt JTree nur den Text hervor:
* TreeLineselection.java
 
*
[[Datei:TreeSelection.jpg]]
* Normally, when a node is selected, the tree marks only the text.
 
* This shows one way to mark the whole selected line in a JTree.
Hier wird eine Möglichkeit gezeigt, die ganze selektierte Zeile in einem JTree hervorzuheben:
*/
 
[[Datei:TreeLineselection.jpg]]


import java.awt.*;
<syntaxhighlight lang="java">
import java.awt.event.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;


public class TreeLineselection extends JFrame {
public class LineTreeDemo {


     final private JTree tree;
     final private JTree tree;


     public TreeLineselection() {
     public LineTreeDemo() {
         super("Tree Lineselection");
         JFrame frame = new JFrame("Tree Lineselection");
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         setSize(400, 300);
         frame.setSize(400, 300);
         setLocationRelativeTo(null);
         frame.setLocationRelativeTo(null);
         tree = new MyTree();
         tree = new LineTree();
         add(new JScrollPane(tree));
         frame.add(new JScrollPane(tree));
        frame.setVisible(true);
     }
     }


     public static void main(final String args[]) throws Exception {
     public static void main(final String args[]) throws Exception {
         new TreeLineselection().setVisible(true);
         new LineTreeDemo();
     }
     }
}
}
</syntaxhighlight>


class MyTree extends JTree {
<syntaxhighlight lang="java">
 
/*
* LineTree.java
*
* Normally, when a node is selected, the tree marks only the text.
* This shows one way to mark the whole selected line in a JTree.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class LineTree extends JTree {
     final private Color SELECTION_COLOR = new Color(180, 210, 230, 90);
     final private Color SELECTION_COLOR = new Color(180, 210, 230, 90);
     private Rectangle selectedRowBounds;
     private Rectangle selectedRowBounds;
     private boolean expand;//allows the MouseListener to detect "handles" action (->no row selection)
     private boolean expand;//allows the MouseListener to detect "handles" action (->no row selection)
 
     public MyTree() {
     public LineTree() {
         //Set the selection mode:
         //Set the selection mode:
         selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
         selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
Zeile 46: Zeile 61:
         //Add the TreeSelectionListener:
         //Add the TreeSelectionListener:
         selectionModel.addTreeSelectionListener(new TreeSelectionListener() {
         selectionModel.addTreeSelectionListener(new TreeSelectionListener() {
 
            @Override
             public void valueChanged(final TreeSelectionEvent e) {
             public void valueChanged(final TreeSelectionEvent e) {
                 paintSelectionRect();
                 paintSelectionRect();
Zeile 53: Zeile 69:
         //Add the TreeExpansionListener:
         //Add the TreeExpansionListener:
         addTreeExpansionListener(new TreeExpansionListener() {
         addTreeExpansionListener(new TreeExpansionListener() {
 
            @Override
             public void treeCollapsed(final TreeExpansionEvent event) {
             public void treeCollapsed(final TreeExpansionEvent event) {
                 paintSelectionRect();
                 paintSelectionRect();
                 expand = true;
                 expand = true;
             }
             }
 
            @Override
             public void treeExpanded(final TreeExpansionEvent event) {
             public void treeExpanded(final TreeExpansionEvent event) {
                 paintSelectionRect();
                 paintSelectionRect();
Zeile 66: Zeile 84:
         //Add MouseListener if you want to listen to whole line width:
         //Add MouseListener if you want to listen to whole line width:
         addMouseListener(new MouseAdapter() {
         addMouseListener(new MouseAdapter() {
 
             @Override
             @Override
             public void mousePressed(final MouseEvent e) {
             public void mousePressed(final MouseEvent e) {
Zeile 84: Zeile 102:
             }
             }
         });
         });
 
     }
     }
     //This method is called in valueChanged, treeCollapsed and treeExpanded
     //This method is called in valueChanged, treeCollapsed and treeExpanded
     //to paint the selection rectangle:
     //to paint the selection rectangle:
 
     private void paintSelectionRect() {
     private void paintSelectionRect() {
         //Get selected row bounds:
         //Get selected row bounds:
Zeile 99: Zeile 117:
         repaint();
         repaint();
     }
     }
 
     //Override paintComponent of JTree:
     //Override paintComponent of JTree:
     @Override
     @Override
Zeile 113: Zeile 131:
     }
     }
}
}
</code=java>
</syntaxhighlight>
[[Kategorie:Java]]
[[Kategorie:Java]]
[[Kategorie:Swing]]
[[Kategorie:Swing]]
[[Kategorie:Java-Codeschnipsel]]
[[Kategorie:Java-Codeschnipsel]]

Aktuelle Version vom 8. März 2020, 14:56 Uhr

Gewöhnlich wenn ein Knoten selektiert ist, hebt JTree nur den Text hervor:

Hier wird eine Möglichkeit gezeigt, die ganze selektierte Zeile in einem JTree hervorzuheben:

import javax.swing.*;

public class LineTreeDemo {

    final private JTree tree;

    public LineTreeDemo() {
        JFrame frame = new JFrame("Tree Lineselection");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        tree = new LineTree();
        frame.add(new JScrollPane(tree));
        frame.setVisible(true);
    }

    public static void main(final String args[]) throws Exception {
        new LineTreeDemo();
    }
}
/*
 * LineTree.java
 *
 * Normally, when a node is selected, the tree marks only the text.
 * This shows one way to mark the whole selected line in a JTree.
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
 
 
public class LineTree extends JTree {
 
    final private Color SELECTION_COLOR = new Color(180, 210, 230, 90);
    private Rectangle selectedRowBounds;
    private boolean expand;//allows the MouseListener to detect "handles" action (->no row selection)
 
    public LineTree() {
        //Set the selection mode:
        selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //Adapt the default selection colors:
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) getCellRenderer();
        renderer.setTextSelectionColor(Color.BLACK);
        renderer.setBorderSelectionColor(Color.WHITE);
        renderer.setBackgroundSelectionColor(Color.WHITE);
        //Add the TreeSelectionListener:
        selectionModel.addTreeSelectionListener(new TreeSelectionListener() {
 
            @Override
            public void valueChanged(final TreeSelectionEvent e) {
                paintSelectionRect();
            }
        });
        //Add the TreeExpansionListener:
        addTreeExpansionListener(new TreeExpansionListener() {
 
            @Override
            public void treeCollapsed(final TreeExpansionEvent event) {
                paintSelectionRect();
                expand = true;
            }
 
            @Override
            public void treeExpanded(final TreeExpansionEvent event) {
                paintSelectionRect();
                expand = true;
            }
        });
        //Add MouseListener if you want to listen to whole line width:
        addMouseListener(new MouseAdapter() {
 
            @Override
            public void mousePressed(final MouseEvent e) {
                if (!expand) {
                    int row = getClosestRowForLocation(e.getX(), e.getY());
                    if (e.getClickCount() == 2) {
                        if (isCollapsed(row)) {
                            expandRow(row);
                        } else {
                            collapseRow(row);
                        }
                    } else {
                        setSelectionRow(row);
                    }
                }
                expand = false;
            }
        });
 
    }
    //This method is called in valueChanged, treeCollapsed and treeExpanded
    //to paint the selection rectangle:
 
    private void paintSelectionRect() {
        //Get selected row bounds:
        int[] rows = getSelectionRows();
        if (rows == null) {
            return;
        }
        selectedRowBounds = getRowBounds(rows[0]);
        //Repaint the JTree:
        repaint();
    }
 
    //Override paintComponent of JTree:
    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        if (selectedRowBounds == null) {
            return;
        }
        //Set selection Color:
        g.setColor(SELECTION_COLOR);
        //Draw selection rectangle using the width of JTree:
        g.fillRect(0, (int) selectedRowBounds.getY(), getWidth(), (int) selectedRowBounds.getHeight());
    }
}