Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of Flashdaddee
Flashdaddee

MouseListener won't listen in my JTable
I have a JTable (called table) and when the user clicks on a cell from the second column (that'd be column 1, starting from column 0), a small JFrame should popup containing some information related to the task from that row.

So far, I tried to add a mouse listener to my JTable, so that it'd print (just using System.out.println) the number of the clicked row. It compiles fine, but when I run the program and I click on the JTable, nothing happens.

Any idea why?

This is the code from the mouse listener:

[code]
// mouse listener to display popup JFrame with the notes belonging
// to the Task from the selected row
MouseListener ml = new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
            if (e.isShiftDown()||e.isControlDown()||e.isAltDown())
            {
                  return;
            }
            if (e.isMetaDown())
            {
                  int row = table.rowAtPoint(e.getPoint());
                  System.out.println("The row number: " + row + " was clicked.") ;
            }
      }
};
[/code]

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of bobbit31bobbit31🇺🇸

to avoid the obvious, you did:
JTable jt = new JTable();
...
jt.addMouseListener(ml);

right?

Avatar of FlashdaddeeFlashdaddee

ASKER

And BTW, how can I display the code properly in this site?

Obviously my code tags aren't working

Avatar of Mick BarryMick Barry🇦🇺

You return is shift, control or alt is pressed.
But then you only check row if a meta key is pressed, which meta do you expect to be pressed?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Hmmm, it's working with the right button, but not the left button.

Yeap BobB, it's added, but still, it works only with the right button.

Do you know why?

Avatar of Mick BarryMick Barry🇦🇺

Your also maybe better off using a selection listener, though it depends on your requirements.

objects:

I'm not sure if I understand you, but just in case, I tried executing the action if (e.isShiftDown()||e.isControlDown()||e.isAltDown())

But, that works only when the wheel button is pressed. That is, when you press on the mouse wheel so that it behaves like a button. But, it won't work then with either the left or right button.

Also, in case it helps at all, I'm using 1.4.2

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Mick BarryMick Barry🇦🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

How'd I use a Selection Listener?

Avatar of Mick BarryMick Barry🇦🇺

And exactly what events do you want it to work for?

>> Why do you have this?

That was the example I found, and I thought it was appropiate. But it seems not to be

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Mick BarryMick Barry🇦🇺

Selection listener is fired when the table selection changes.

Avatar of Mick BarryMick Barry🇦🇺

Sorry that should have said:
Selection event is fired when the table selection changes.

Avatar of Mick BarryMick Barry🇦🇺

to add a selection listener use:
table.getSelectionModel().addListSelectionListener()

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


>> Why do you have this?

OK! That's it!

Now it works, only when the left button is clicked, just the way it should:

            // mouse listener to display popup JFrame with the notes belonging
            // to the Task from the selected row
            MouseListener ml = new MouseAdapter()
            {
                  public void mouseReleased(MouseEvent e)
                  {
                        if (e.isShiftDown()||e.isControlDown()||e.isAltDown()||(e.isMetaDown()))
                        {
                              return;
                        }
                        int row = table.rowAtPoint(e.getPoint());
                        int col = table.columnAtPoint(e.getPoint());
                        
                        if (col == 1)
                        System.out.println("The row number: " +      (row + 1) + " was clicked.") ;
                  }
            };            // end of mouse listener to display pop up JFrame

Now I just have to include the popup JFrame

And here I am again...
I've got a problem with the JFrame that I'm displaying.  It contains a String with some notes, which will be quite a few words.  But, at the same time, I wanted to limit the width of the JTextArea, so I used the method setColumns.

AND, when I run the program, it displays a single row; it does set the width to what I said, but there's no way I can display more than one row (and therefor, most of the text isn't display).

This is the code at the moment, anybody can see what's wrong?

            MouseListener ml = new MouseAdapter()
            {
                  public void mouseReleased(MouseEvent e)
                  {
                        if (e.isShiftDown()||e.isControlDown()||e.isAltDown()||(e.isMetaDown()))
                        {
                              return;
                        }
                        int row = table.rowAtPoint(e.getPoint());
                        int col = table.columnAtPoint(e.getPoint());
                        
                        if (col == 1)
                        {
                              System.out.println("The row number: " +      (row + 1) +
                                                                  " was clicked.") ;
                                                            
                              String testing = new String(pool.getNotes(row)) ;
                              JFrame popupJFrame = new JFrame();
                              // JTextArea(String text, int rows, int columns)
                              JTextArea taskTest = new JTextArea(testing) ;
                              taskTest.setColumns(30) ;
                              taskTest.setLineWrap(true) ;
                              taskTest.setWrapStyleWord(true) ;
                              taskTest.setEditable(false) ;
                              

                              popupJFrame.getContentPane().add(taskTest, "Center") ;
                              popupJFrame.setLocation(new Point(300, 300)) ;

                              popupJFrame.pack() ;
                              popupJFrame.setResizable(false) ;
                              
                              popupJFrame.show() ;
                        }
                  }
            };            // end of mouse listener to display pop up JFrame

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Mick BarryMick Barry🇦🇺

I'd suggest adding your text area to a scroll pane.
Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.