import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable(5, 5);
JTableHeader header = table.getTableHeader();
header.addMouseListener(new ColumnHeaderListener());
}
}
class ColumnHeaderListener extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
JTable table = ((JTableHeader) evt.getSource()).getTable();
TableColumnModel colModel = table.getColumnModel();
int index = colModel.getColumnIndexAtX(evt.getX());
if (index == -1) {
return;
}
Rectangle headerRect = table.getTableHeader().getHeaderRect(index);
if (index == 0) {
headerRect.width -= 10;
} else {
headerRect.grow(-10, 0);
}
if (!headerRect.contains(evt.getX(), evt.getY())) {
int vLeftColIndex = index;
if (evt.getX() < headerRect.x) {
vLeftColIndex--;
}
}
}
}