Problem mouseListener and JTable

2

I have a class in which I am implementing the events of a frame and I want to make every time that double-clicking on a table opens another window and shows me the data of that object, I am implementing the mouseListener interface to the class and add it to Jtable so that I can run the event.

This is the class where I implement the interfaces and add the mouseListener interface to the table. EYE : I will only post the events implemented by mouseListener because they are of interest in this case:

public class ControladorEstudiante implements ActionListener, KeyListener, MouseListener{

private DBConnection dbcon;

EstudianteAdmin estAdmin= new EstudianteAdmin();
MostrarEstudiante estMostrar= new MostrarEstudiante();
EstudianteDAOImpl estDAO= new EstudianteDAOImpl(dbcon);

public ControladorEstudiante(EstudianteAdmin estAdmin, MostrarEstudiante estMostrar, EstudianteDAOImpl estDAO){
    this.estDAO= estDAO;
    this.estAdmin= estAdmin;
    this.estMostrar= estMostrar;
    this.estAdmin.getTablaEstudiante().addMouseListener(this);    }

public void InicializarCrud(){

}

@Override
public void keyReleased(KeyEvent ke) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseClicked(MouseEvent me) {
        if(me.getClickCount()== 2){

        int fila= estAdmin.getTablaEstudiante().getSelectedRow();

        if(fila>-1){
            estMostrar.getTxtNombre().setText(estAdmin.getTablaEstudiante().getValueAt(fila, 0).toString());
            estMostrar.getTxtMatricula().setText(estAdmin.getTablaEstudiante().getValueAt(fila, 1).toString());
            estMostrar.getTxtNota().setText(estAdmin.getTablaEstudiante().getValueAt(fila, 3).toString());
        }
    }
}

@Override
public void mousePressed(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseReleased(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseEntered(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseExited(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

As you can know the method I have for when two click on the table is the mouseClicked , however, when loading the data from the Database into the table I immediately move the mouse over the jtable, I get an error, then I leave the stacktrace :

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at comm.estudiante.dao.controlador.ControladorEstudiante.mouseEntered(ControladorEstudiante.java:425)
at java.awt.AWTEventMulticaster.mouseEntered(AWTEventMulticaster.java:300)
at java.awt.Component.processMouseEvent(Component.java:6544)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.retargetMouseEnterExit(Container.java:4676)
at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4654)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4505)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Another thing is that when I implement the mouseListener interface on the Jtable, it is not selected, I do not know why.

    
asked by David Calderon 09.09.2016 в 13:01
source

2 answers

2

That error is given to you because you yourself have indicated that when it happens "mouseEntered" throw that exception. Delete the method or implement it; -)

@Override
public void mouseEntered(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

If you notice, your IDE had to default to that implementation.

    
answered by 09.09.2016 / 13:37
source
1

One of the drawbacks of implementing the interface java.awt.event.MouseListener in a new class is that you have to define all your methods in it.

So the class % has also been included in the JDK java.awt.event.MouseAdapter , which you extend and overwrite only the desired methods.

For example, if you are interested in double clicking, the following code could give you an idea:

public static void main(String[] args) {
    Object[][] data = new Object[][] { { "VGA", "640x480" }, { "SVGA", "800x600" } };
    Object[] columnNames = new Object[] { "Name", "Value" };
    JTable table = new JTable(new DefaultTableModel(data, columnNames) {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    });
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt) {
            if (evt.getClickCount() > 1) {
                JTable table = (JTable) evt.getSource();
                int row = table.rowAtPoint(evt.getPoint());
                TableModel model = table.getModel();
                JOptionPane.showMessageDialog(table.getParent(), model.getValueAt(row, 0));
            }
        }
    });
    JScrollPane pane = new JScrollPane(table);
    JOptionPane.showMessageDialog(null, pane);

}

    
answered by 09.09.2016 в 16:18