JButton in JTable

Posted on 2011-10-14
Last Modified: 2012-05-12
One of the columns in my JTable has JButtons (1 JButton in a cell).

Depending on the cell in which the button is present, I want to execute a particular function.

Please let me know how I can do this. Thanks!
Question by:dshrenik
    LVL 47

    Assisted Solution

    When you add JButtons to your Componenets in the TableCellRenderer name your JButtons differently - adn you can pass
    instances of JButton to the class which handles the ervent and then in the
    even handler check what buttoin was the source of the event

    actiopnPerformed(AcrtionEvent ae) {

    //do somthing}
    //do spemthing else

    LVL 47

    Expert Comment

    In general there is conevnient method of hadnling events from the JTable based on the location of mouseclick point
    in which cell it was clicked  - but in this case it will also dal with events  if you click in the cvell but not on the button
    LVL 47

    Accepted Solution


    this is conveninet method with MouseListener to determine which cell was clicked
    You can probebly combimne this with action listener to act only on the click on the button inside the cell
    public void mouseClicked(MouseEvent e) {
    int row = table.rowAtPoint(e.getPoint());
    int col = table.columnAtPoint(e.getPoint());
    System.out.println("Row = " + row + ", Column = " + col);

    Open in new window

    LVL 86

    Assisted Solution

    Depending on the cell in which the button is present, I want to execute a particular function.

    Please let me know how I can do this. Thanks!

    Buttons in a table behave no differently from elsewhere. Add an Action/ActionListener to the button to make it do what you want

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now