views:

312

answers:

1

In Outlook I can remove a table column if I drag the column header out of the table. How can I do the same in Java with a Swing JTable?

A default drag & drop operation is not possible because this feature is independent of the target position. It depends only from the drag source.

+2  A: 

For this answer I used the SimpleTableDemo. I simply add a MouseListener to the table. Here the MouseListener:

class MyMouseListener implements MouseListener {
  public void mouseClicked(MouseEvent arg0) {}
  public void mouseEntered(MouseEvent arg0) {}
  public void mouseExited(MouseEvent arg0) {}
  public void mousePressed(MouseEvent arg0) {}
  public void mouseReleased(MouseEvent m) {
    JTableHeader tableHeader = (JTableHeader)m.getComponent();
    JTable table = tableHeader.getTable();
    if (!table.getBounds().contains(m.getPoint())) {
      table.removeColumn(table.getColumnModel().getColumn(
          tableHeader.columnAtPoint(m.getPoint())));
    }
  }
}

This is a really basic way, there are no exception handled or wathever. But at least it works.

Jérôme