Google Tag Manager

2009/02/27

HyperLink in JTable Cell

Code

class URLRenderer extends DefaultTableCellRenderer implements MouseListener, MouseMotionListener {
  private int row = -1;
  private int col = -1;
  private boolean isRollover = false;
  public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus,
                           int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
    if(!table.isEditing() && this.row==row && this.col==column && this.isRollover) {
      setText("< html > < u > < font color='blue' >"+value.toString());
    }else if(hasFocus) {
      setText("< html > < font color='blue' >"+value.toString());
    }else{
      setText(value.toString());
    }
    return this;
  }
  private static boolean isURLColumn(JTable table, int column) {
    return column>=0 && table.getColumnClass(column).equals(URL.class);
  }
  @Override public void mouseMoved(MouseEvent e) {
    JTable table = (JTable)e.getSource();
    Point pt = e.getPoint();
    int prev_row = row;
    int prev_col = col;
    boolean prev_ro = isRollover;
    row = table.rowAtPoint(pt);
    col = table.columnAtPoint(pt);
    isRollover = isURLColumn(table, col); // && pointInsidePrefSize(table, pt);
    if((row==prev_row && col==prev_col && Boolean.valueOf(isRollover).equals(prev_ro)) ||
       (!isRollover && !prev_ro)) {
      return;
    }
//*/ @see SwingSet3: HyperlinkCellRenderer.java
    Rectangle repaintRect;
    if(isRollover) {
      Rectangle r = table.getCellRect(row, col, false);
      repaintRect = prev_ro ? r.union(table.getCellRect(prev_row, prev_col, false)) : r;
    }else{ //if(prev_ro) {
      repaintRect = table.getCellRect(prev_row, prev_col, false);
    }
    table.repaint(repaintRect);
/*/
    //TODO: asks JIDE to show benchmark results.
    table.repaint();
//*/
  }
  @Override public void mouseExited(MouseEvent e)  {
    JTable table = (JTable)e.getSource();
    if(isURLColumn(table, col)) {
      table.repaint(table.getCellRect(row, col, false));
      row = -1;
      col = -1;
      isRollover = false;
    }
  }
  @Override public void mouseClicked(MouseEvent e) {
    JTable table = (JTable)e.getSource();
    Point pt = e.getPoint();
    int ccol = table.columnAtPoint(pt);
    if(isURLColumn(table, ccol)) { // && pointInsidePrefSize(table, pt)) {
      int crow = table.rowAtPoint(pt);
      URL url = (URL)table.getValueAt(crow, ccol);
      System.out.println(url);
      try{
        //Web Start
        //BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
        //bs.showDocument(url);
        if(Desktop.isDesktopSupported()) { // JDK 1.6.0
          Desktop.getDesktop().browse(url.toURI());
        }
      }catch(Exception ex) {
        ex.printStackTrace();
      }
    }
  }
//......

References

1 comment:

  1. add comment: ServiceManager.lookup("javax.jnlp.BasicService")

    ReplyDelete