How to reliably get row index in JTable from MouseEvent?

How can I find out which row in a JTable the user just clicked?

Asked by: Kristian212 | Posted: 21-01-2022

Answer 1

Try this:


Answered by: Arnold214 | Posted: 22-02-2022

Answer 2

If you only ever care about listening to selections on the JTable:

jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
        int sel = jTable.getSelectedRow();

Answered by: Tess884 | Posted: 22-02-2022

Similar questions

java - MouseEvent weirdness

I'm trying to create a MouseEvent with certain modifiers for UnitTesting. I'm using J2SE and the following code fails to pass: public void testMouseEventProblem() { MouseEvent event = new MouseEvent(new JPanel(), 1, System.currentTimeMillis(), InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK, 1,1, 0, false); assertEquals(InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK, event.getModifiers(...

mouseevent - java: activating a mouse click event

I have a java method activated by a mouse click on a button private void backButtonMouseClicked(java.awt.event.MouseEvent evt) { do stuff } is there some way to virtually use this method from another method without clicking the mouse on the button?

Going crazy with these mouseEvent methods in Java

A week into it, and I am just banging my head up against this problem. It should be a simple excersise: I have a rectangle. I click and drag the rectangle on an x axis. The rectangle should only move within a set bounding area (the canvas), so if the canvas is 200 px wide, then the x coordinate should only go from 0 to getWidth()-RECTANGLE_WIDTH. Simple enough, but I just can not get the dar...

mouseevent - Java Robot key activity seems to stop working while certain software is running

I'm writing a Java application to automate character actions in an online game overnight (specifically, it catches fish in Final Fantasy XI). The app makes heavy use of java's Robot class both for emulating user keyboard input and for detecting color changes on certain parts of the screen. It also uses multithreading and a swing GUI. The application seems to work perfectly when I test it without the game running,...

java - Detecting Shift modifiers on MouseEvent generated from click in swing

I'm handling some MouseEvent in a GUI application using Java Swing. Since now i was analyzing mouse events inside mousePressed method, only to determine if a left or right click happened. My code was: public void mousePressed(MouseEvent me) { if (me.getModifiers == InputEvent.BUTTON1_DOWN_MASK){ //left click }else if (me.getModifiers == InputEvent.BUTTON3_DOWN_MASK){ //rig...

java - Why does the getX() getY() of MouseEvent seem to offset from the real coordinate?

I have a JPanel embedded inside a JFrame. JPanel is added at CENTER of BorderLayout. I am using the following code to draw on it but the MouseEvent's getX() and getY() seem to offset the real coordinate. Why? The relevant code is:- private Image backBuffer = createImage(getWidth(), getHeight()); ...

java - How to immediately change text of jLabel inside MouseEvent handler?

I have problem with refreshing swing components inside Mouse action event handler. Problem is that all jLabels which are changed in this function that their changes are visible after jButton1MouseClicked() is done. Here is my function: private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { int cycles = Integer.parseInt(c.settings.get("cycles")); sta...

swing - Java MouseEvent, check if pressed down

I have a class that implements MouseListener (JPanel). When I click on the panel something happens. What I want is some kind of while-loop that loops as long as left mousebutton is pressed down. @Override public void mousePressed(MouseEvent e) { while(e.isPressedDownD) { // <-- //DO SOMETHING } } This obviously doesn't work, but I hope you understand what I'm trying...

mouseevent - Detecting mouse movement for a 3D first person world in Java

I am working on a first person game in Java, and I am trying to get the 3D movement working. My problem is I would like to capture mouse movement, yet keep the mouse inside the window. After I capture the mouse movement, I figure the best way to keep the mouse in my window is to center the mouse in the window after moving, using Robot.moveMouse(x,y). This works fine, however the movement from the Robot triggers a...

java - Border of JPanel with mouseEvent handling

How to create a border of JPanel, which will be able to handle MouseEvents? I tried to do something like that: abstract public class MyBorder extends LineBorder implements MouseListener But after implementing virtual methods I cannot assign mouseListener to my class. I guess, that I have to assign it into some JComponent. So, how can I create some sort of border with mouseL...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)