Reputation: 1
I need help to add an event when clicking with the mouse on the columns identifiers, I'm using DefaultTableModel
public class TableData extends JTable{
private final DefaultTableModel model;
private final ObjectIterable objI;
public TableData(ObjectIterable s){
objI = s
model = new DefaultTableModel();
initTable();
loadTable();
addTableEvent();
}
private void initTable(){
model.setColumnIdentifiers(new String []{"Name","Type"});
setModel(model);
setEnabled(false);
}
private void loadTable(){
for(ObjI s : objI){
String type = s.getType();
String value = s.value();
insertRow(value, type);
}
}
public void insertRow(String param, String type){
model.insertRow(0, new String[]{param,type});
}
private void addTableEvent(){
JTable table = this;
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// super.mouseClicked(e);
if(e.getClickCount() == 2){
if(table.rowAtPoint(e.getPoint()) < 0){
System.out.println("clicket");
}
}
}
});
}
}
I need to know in which column the click was made. Thanks for your help.
Upvotes: -1
Views: 357
Reputation: 324187
I need help to add an event when clicking with the mouse on the columns identifiers,
You need to add the listener to the table header, not the table.
I need to know in which column the click was made.
Something like:
table.getTableHeader().addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
JTableHeader header = (JTableHeader)e.getSource();
int column = header.columnAtPoint( e.getPoint() );
System.out.println( column );
}
});
Upvotes: 0