TreeLineselection

Aus Byte-Welt Wiki
Version vom 14. Dezember 2009, 20:51 Uhr von AndreUhres (Diskussion | Beiträge) (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…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen

<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 line in a JTree.
*/

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*;

public class TreeLineselection extends JFrame {

   final private JTree tree;
   public TreeLineselection() {
       super("Tree Lineselection");
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       setSize(400, 300);
       setLocationRelativeTo(null);
       tree = new MyTree();
       add(new JScrollPane(tree));
   }
   public static void main(final String args[]) throws Exception {
       new TreeLineselection().setVisible(true);
   }

}

class MyTree 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 MyTree() {
       //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() {
           public void valueChanged(final TreeSelectionEvent e) {
               paintSelectionRect();
           }
       });
       //Add the TreeExpansionListener:
       addTreeExpansionListener(new TreeExpansionListener() {
           public void treeCollapsed(final TreeExpansionEvent event) {
               paintSelectionRect();
               expand = true;
           }
           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());
   }

} </code=java>