Google Tag Manager

2011/02/28

Highlight entire JTree row on selection

Code

class RowSelectionTree extends JTree {
  private static final Color SELC = new Color(100, 150, 200);
  private Handler handler;

  @Override protected void paintComponent(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    if (getSelectionCount() > 0) {
      g.setColor(SELC);
      for (int i : getSelectionRows()) {
        Rectangle r = getRowBounds(i);
        g.fillRect(0, r.y, getWidth(), r.height);
      }
    }
    super.paintComponent(g);
    if (getLeadSelectionPath() != null) {
      Rectangle r = getRowBounds(getRowForPath(getLeadSelectionPath()));
      g.setColor(hasFocus() ? SELC.darker() : SELC);
      g.drawRect(0, r.y, getWidth() - 1, r.height - 1);
    }
  }

  @Override public void updateUI() {
    removeFocusListener(handler);
    super.updateUI();
    setUI(new BasicTreeUI() {
      @Override public Rectangle getPathBounds(JTree tree, TreePath path) {
        if (tree != null && treeState != null) {
          return getPathBounds(path, tree.getInsets(), new Rectangle());
        }
        return null;
      }

      private Rectangle getPathBounds(
          TreePath path, Insets insets, Rectangle bounds) {
        Rectangle rect = treeState.getBounds(path, bounds);
        if (rect != null) {
          rect.width = tree.getWidth();
          rect.y += insets.top;
        }
        return rect;
      }
    });
    handler = new Handler();
    addFocusListener(handler);
    setCellRenderer(handler);
    setOpaque(false);
  }

  static class Handler extends DefaultTreeCellRenderer implements FocusListener {
    @Override public Component getTreeCellRendererComponent(
        JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
      JLabel l = (JLabel) super.getTreeCellRendererComponent(
          tree, value, selected, expanded, leaf, row, hasFocus);
      l.setBackground(selected ? SELC : tree.getBackground());
      l.setOpaque(true);
      return l;
    }

    @Override public void focusGained(FocusEvent e) {
      e.getComponent().repaint();
    }

    @Override public void focusLost(FocusEvent e) {
      e.getComponent().repaint();
    }
  }
}

References

2011/02/03

Translucent JInternalFrame (Nimbus)

Code

JPanel p1 = new JPanel();
p1.setOpaque(false);

JPanel p2 = new JPanel() {
  @Override protected void paintComponent(Graphics g) {
    // super.paintComponent(g);
    g.setColor(new Color(100, 50, 50, 100));
    g.fillRect(0, 0, getWidth(), getHeight());
  }
};

JPanel p3 = new JPanel() {
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    // g2.setPaint(new Color(100, 120, 100, 100));
    g2.setPaint(texture);
    g2.fillRect(0, 0, getWidth(), getHeight());
  }
};

protected JInternalFrame createFrame(JPanel panel) {
  MyInternalFrame frame = new MyInternalFrame();
  if (panel != null) {
    frame.setContentPane(panel);
    // JButton button = new JButton("button");
    // button.setOpaque(false);
    panel.add(new JLabel("label"));
    panel.add(new JButton("button"));
  }
  desktop.add(frame);
  frame.setOpaque(false);
  frame.setVisible(true);
  // ...

References