So I wrote a code that uses MouseListener to select/highlight data in a JTable. I would like to crop the highlighted data. For example, I would highlight the first column and 3rd column with 20 rows down, click a button and make a new JTable with the highlighted data. This is my MouseListener method that highlights data:
public void DataSelection(JTable table) {
JTable Table = table;
JTableHeader columnHeader = Table.getTableHeader();
//***Row Selection***//
Table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
Table.setColumnSelectionAllowed(true);
Table.setRowSelectionAllowed(true);
if (Table.isCellSelected(Table.getSelectedRow(), 0)) {
Table.setColumnSelectionAllowed(false);
Table.setRowSelectionAllowed(true);
}
}
});
//***Column Selection***//
columnHeader.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
int columnPoint = columnHeader.columnAtPoint(mouseEvent.getPoint());
Object columnCursorType = columnHeader.getCursor().getType();
if (columnCursorType == Cursor.E_RESIZE_CURSOR)
mouseEvent.consume();
else {
if (columnPoint == 0)
Table.selectAll();
else {
Table.setColumnSelectionAllowed(true);
Table.setRowSelectionAllowed(false);
Table.clearSelection();
Table.setColumnSelectionInterval(columnPoint, columnPoint);
}
}
}
});
}