Now the position in a table and open a jpanel
I’m programing in Java Netbeans and I'm making an application for a touch screen. I have a table, I would like if it’s possible to click in a cell of the table and go to another jpanel? Look the exam开发者_如何学运维ple:
If a click in the John’s line and the Form complete column, I need to open the jpanel where is the John’s form.
Is it possible to do that?
Yes it is possible. Add a mouse listener to the table like this:
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
int row = table.rowAtPoint(event.getPoint());
int column = table.columnAtPoint(event.getPoint());
if (row >= 0 && column >= 0) {
// Cell has been clicked
}
}
});
You could do it also like this:
...
jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
...
int row = jTable1.getSelectedRow();
int col = jTable1.getSelectedColumn());
if (evt.getClickCount() > 1) { // double-click etc...
...
精彩评论