Google Tag Manager

2009/02/27

aterai

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

2009/02/26

aterai

Use JScrollBar as JSlider

Code

int step   = 5;
int extent = 20;
int min    = 0;
int max    = extent*10; //200
int value  = 50;
final JScrollBar scrollbar = new JScrollBar(
            JScrollBar.HORIZONTAL, value, extent, min, max+extent);
scrollbar.setUnitIncrement(step);
scrollbar.getModel().addChangeListener(new ChangeListener(){
  public void stateChanged(javax.swing.event.ChangeEvent e) {
    BoundedRangeModel m = (BoundedRangeModel)e.getSource();
    spinner.setValue(m.getValue());
  }
});
final JSpinner spinner = new JSpinner(
            new SpinnerNumberModel(value, min, max, step));
//...

References

2009/02/16

aterai

JTableHeader CheckBox

Code

enum Status { SELECTED, DESELECTED, INDETERMINATE }
class HeaderRenderer extends JCheckBox implements TableCellRenderer {
  private final JLabel label = new JLabel("Check All");
  private int targetColumnIndex;
  public HeaderRenderer(JTableHeader header, int index) {
    super((String)null);
    this.targetColumnIndex = index;
    setOpaque(false);
    setFont(header.getFont());
    header.addMouseListener(new MouseAdapter() {
      @Override public void mouseClicked(MouseEvent e) {
        JTableHeader header = (JTableHeader)e.getSource();
        JTable table = header.getTable();
        TableColumnModel columnModel = table.getColumnModel();
        int vci = columnModel.getColumnIndexAtX(e.getX());
        int mci = table.convertColumnIndexToModel(vci);
        if(mci == targetColumnIndex) {
          TableColumn column = columnModel.getColumn(vci);
          Object v = column.getHeaderValue();
          boolean b = Status.DESELECTED.equals(v)?true:false;
          TableModel m = table.getModel();
          for(int i=0; i < m.getRowCount(); i++) m.setValueAt(b, i, mci);
          column.setHeaderValue(b?Status.SELECTED:Status.DESELECTED);
          //header.repaint();
        }
      }
    });
  }
  @Override public Component getTableCellRendererComponent(
      JTable tbl, Object val, boolean isS, boolean hasF, int row, int col) {
    TableCellRenderer r = tbl.getTableHeader().getDefaultRenderer();
    JLabel l =(JLabel)r.getTableCellRendererComponent(tbl,val,isS,hasF,row,col);
    if(targetColumnIndex==tbl.convertColumnIndexToModel(col)) {
      if(val instanceof Status) {
        switch((Status)val) {
          case SELECTED:    setSelected(true);  setEnabled(true);  break;
          case DESELECTED:  setSelected(false); setEnabled(true);  break;
          case INDETERMINATE: setSelected(true);  setEnabled(false); break;
        }
      }else{
        setSelected(true); setEnabled(false);
      }
      label.setIcon(new ComponentIcon(this));
      l.setIcon(new ComponentIcon(label));
      l.setText(null); //XXX: Nimbus???
    }
    return l;
  }
}
class CheckBoxIcon implements Icon{
  private final JCheckBox check;
  public CheckBoxIcon(JCheckBox check) {
    this.check = check;
  }
  @Override public int getIconWidth() {
    return check.getPreferredSize().width;
  }
  @Override public int getIconHeight() {
    return check.getPreferredSize().height;
  }
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    SwingUtilities.paintComponent(
        g, check, (Container)c, x, y, getIconWidth(), getIconHeight());
  }
}
model.addTableModelListener(new TableModelListener() {
  @Override public void tableChanged(TableModelEvent e) {
    if(e.getType()==TableModelEvent.UPDATE && e.getColumn()==targetColumnIndex) {
      int vci = table.convertColumnIndexToView(targetColumnIndex);
      TableColumn column = table.getColumnModel().getColumn(vci);
      if(!Status.INDETERMINATE.equals(column.getHeaderValue())) {
        column.setHeaderValue(Status.INDETERMINATE);
      }else{
        boolean selected = true, deselected = true;
        TableModel m = table.getModel();
        for(int i=0; i < m.getRowCount(); i++) {
          Boolean b = (Boolean)m.getValueAt(i, targetColumnIndex);
          selected &= b; deselected &= !b;
          if(selected==deselected) return;
        }
        if(selected) {
          column.setHeaderValue(Status.SELECTED);
        }else if(deselected) {
          column.setHeaderValue(Status.DESELECTED);
        }else{
          return;
        }
      }
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
});

References