JTable - detecting double click

i'm trying to detect double click on a row in a JTable and then find out the row number. i can get it to detect the double click with the table disabled but the row always comes back as -1. if i try setEnabled(false) it doesn't detect the double click at all.

I think your table should be enabled. Put a mouse listener on the table like:
myJTable.addMouseListener(new ClickMouseListener());
class ClickMouseListener extends MouseAdapter
    public void mouseClicked(MouseEvent e)
      if (e.getClickCount() == 2)
        Point pt = e.getPoint();
        int row = myJTable.rowAtPoint(pt);
        System.out.println("Mouse clicked on row " + row);
}If you also have selection enabled then the row will also be the selected one so that would be another way of determiing the row number.

Similar Messages

  • JTable & mouse double-click?

    hi,
    When I double-clicked a row in a JTable ,how can i get one column's value in that row.how can i do with the mouse event.
    some examples are preferred.
    thankx in advance !
    rain

    When you say "one column's value in that row" do you mean that you want the value that the user double-clicked?
    When you receive a mouse event, get the selected rows and columns from the table, then get the value from the table model using the getValueAt() method.
    int[] selectedRows = table.getSelectedRows();
    int[] selectedCols = table.getSelectedColumns();
    String selectedElement = (String)model.getValueAt(selectedRows[0], selectedCols[0]);
    System.out.println(selectedElement + " selected");

  • Detecting double-click in the action of a JButton

    I have a similar problem to the poster of http://forum.java.sun.com/thread.jsp?forum=52&thread=93004 and I'm wondering if anyone has a better solution.
    A fuller description of my problem.
    My JButtons represents certain pallette items. Clicking a pallete item puts the button in a selected state (changes its border to show it's selected). The action on this button sets a mode state on the canvas to indicate what was selected. I then click on a canvas workspace to place an instance of that item onto the canvas based on the current mode.
    The selected JButton then pops up and the user needs to select a new pallette icon.
    What I want as an option so that if a pallette button is double-clicked it remains selected even after the item has been placed on the canvas allowing the user to place several copies around the canvas before selecting another item.
    I'd like if possible to determine this from the buttons action rather than registering a listener to the button because parts of the framework I am using pass just the actions around.
    I could do this by looking at the time between actions being triggered but this seems like a messy solution (can I pickup the operating system settings for double-click delay).
    Any other ideas?

    thebobster,
    This doesn't answer your original question, so if you're determined to use d-clicks on a JButton, don't bother reading further.
    I've always found double-clicking on a JButton non-intuitive, not to mention messy to implement. I prefer CTRL-click, etc, to indicate a sticky click.
    Perhaps you could use an image to indicate your palette button: you'll need just 2 images--up-state and down-state--and d-clicks will be easy to implement.
    --A                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Incorrect detection of double-clicks in layers and channels windows. (Mac OS X 10.6.8)

    Photoshop will detect a double-click in the layers and channels panels even when there are different modifier keys held down on the first click vs. the second click.
    I have never intended to double-click and accidentally changed the modifiers I was holding down between clicks.  I often perform an action requiring modifier keys and immediately follow it with an action that requires no modifiers (or different modifiers).
    I realize that the conventions for what constitutes a double-click are platform dependent and not particularly well documented, but I do expect Photoshop to work the way that my operating system does (Mac OS X 10.6.8)).  I know older versions of Windows detected double-clicks even when modifiers were changed between clicks, but I believe the latest versions behave similarly to OS X.
    Examples:
    Start by having a document open with multiple alpha channels and layers in it.
    Select an alpha channel by clicking on it's thumbnail in the channels panel.
    Quickly hold down the control key and click again.
    Result: this brings up the "Channel Options" window. (as if you had double-clicked)
    Expected result: should bring up the channel's contextual menu.
    Bonus extra nitpick:  The window opened on mouse-down, not on mouse-up.
    Create a non-blank alpha channel.
    Hold down the command key and click on that alpha channel's thumbnail. (loads the channel into the selection)
    Quickly release the command key and press the mouse button.
    Result: Brings up the "Channel Options" window. (as if you had double-clicked)
    Expected result: Start a drag operation on the channel.
    Click on a layer in the layers panel to select it.
    Move the mouse pointer to a different layer.
    Hold down the shift key and click to select multiple layers.
    Quickly release the shift key and press the mouse button.
    Result: Brings up the "Layer Style" window.  (as if you had double-clicked)
    Expected result: Begin a drag operation.
    Bonus extra nitpick:  The window opened on mouse-down, not on mouse-up.
    Interestingly the layers panel does the correct thing when control is pressed between clicks, but the channels panel does not.
    The options windows opening on mouse-down instead of mouse-up has never bothered me, but I believe it goes against convention.
    Thanks.

    Very detailed report, Jay. I was able to see exactly what you're talking about, and reproduce this using my trackpad. I'm wondering if this is an OS issue, or something that can be tweaked in Photoshop. Even with files in Finder, you can activate a double-click even with a slight hesitation, which is similar to the behavior in Photoshop.

  • Slow Start-Up / Slow Detection to double click

    Hi,
    My ibook has been ok until my friend installed a 1 GB fonts into my com, and this resulted in a slow start up to my ibook, even though ive already uninstalled it.
    Secondly, the com also seems to be slower in detecting double clicking.
    Anyone can solve the problem???
    Angela An

    Hi, Angela. Welcome to Apple Discussions.
    Have you tried resetting the PMU?

  • Double click detection

    Hello,
    I have a JTree and I want to detect mouse double click over some nodes. I am using the following code in my JTree (which implements MouseListener) :
    public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 2)          
    // MY ACTION
    the snag is that it also detect double clicks when it does not occured over a node. In that case I would like no action to be launched.
    How can I do it ??
    Thank you.
    Sylvain.

    Hi,
    I achived the following way to get the things working like you would to. Take a look:
    public void mouseClicked(MouseEvent evt) {
      if ( evt.getClickCount() == 2 ) {
       TreeNode node = (TreeNode)ree.getLastSelectedPathComponent();
       if ( node.isLeaf() ) {
        // Go ahead
    I hope this help you.
    Regards,
    Ualter Junior.

  • A better way to Double Click

    I have been wondering if there is a better way to detect single clicks Vs double clikcs than the model currently implemented. I am sure all of you have run into the problem of even though an event listener detects a double click eventually, it detects the single click first, so often you have to compromise the quality of your software to account for that. I have considered writing my own custom handler using threads and such, but it seems to me there must be a better way out there that I just have not been able to find. I need a way to detect double clicks, and fool the machine into believing no single click has occured. Must I re-invent the wheel?
    AC

    I agree that GUIs should have some uniformity to
    them..especially as we enter an age where computers
    are ubiquitous. But there are some situations where
    customized behavior is preferrable. If you have ever
    worked with movie special effects software, some art
    software, music software, especially, you know that to
    make a GUI intuitive, sometimes liberties must be
    taken with the 'standard' gui design.The application determines the standard not the OS nor the GUI.
    And to say 'Your code is not standard, therefore the
    fact that java cannont accurately distinguish between
    single and double clicks is your problem' smacks of
    nonsense to me.
    We all know that Java is a great
    language.Java is not just a language it is a platform and a philosophy.
    It is quicker and easier to program in,
    than c++, but it is not at full maturity. Every c++
    development environment I have ever used had the
    ability built in to distinguish between a true single
    click and a double click.As does Swing..
    I love Java, but most
    programmers could fill a page with the enhancements
    that it needs. Not all enhancements are practical.
    Like many I balked when I first started with Java at
    the fact that it did not have true multiple
    inheritence. I now realize that
    I never really needed it anyway. But some things, eg
    templates, I definitely could still use. That being,
    said, thanks for the dialog. Templates are a complicated horror, unless you really need your software to be truly adaptable you will never use them. Even MS, Sun and IBM disposed of that need a long time ago.

  • Double click for JList items selection

    I would like to select JList items only by double clicking on them. I can write a mouse listener for detecting double clicks by my own (and handle it afterwards), by please tell me how to turn the default behavior off (selecting items by clicking one time).
    Thanks in advance
    Marek

    I would like to select JList items only by double clicking on them. Well the standard is to select or highlight on a mouseClick or by using the arrow keys on the keyboard.
    Then once you have a selected item you can perform and Action on the item by double clicking or by using the enter key. Remember you should always be able to use either the keyboard or the mouse to perform any given function.
    This posting shows my solution for the above scenario:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=626866

  • Detecting mouse clicks in editable cell of JTable

    Hi everyone :)
    I thought that this question might have been asked before, but I have searched the forums extensively and have not been able to find the solution.
    What I want to achieve is to detect single and double mouse clicks on JTable cells (that are editable).
    For example, I have a JTable and there exists within it an editable cell. If the user clicks on it once then that cell goes into edit mode, and the user can type directly into the cell. I have already successfully implemented this.
    However, what I also want to do is detect a double-click so that I can pop up a dilaog that shows a list of default values that the user can select.
    So here is what I want;
    1. User clicks on the cell once.
    2. Cell moves into edit mode.
    3. If the user clicks again within a certain time interval then cancel edit mode and pop up a dialog containing values that the user can select from.
    I think that to do this I need to be able to detect mouse clicks on the cell that is currently being edited. So far I have been unable to discover how this is done. I have even tried extending JTextField to get what I want, but with no luck.
    Any help would be greatly appreciated.
    Kind regards,
    Ben Deany

    Thanks for the reply.
    Unfortunately, it is not possible to call 'AddMouseListener()' on a cell editor. You are only able to call 'addCellEditorListener()' and that only allows two events to the broadcast (edit cancel, and edit stop).
    Ben

  • Double click on a JTable row.

    I got and run a sample about double click on a JTable and it works fine. This sample defines a TableModel as shown at the end this note.
    On the other hand, I have an application in which I have defined a JTable
    using the DefaultTableModel as follows :
    DefaultTableModel dtm = new DefaultTableModel(data, names);
    JTable table  = new JTable(dtm);  where data and names are String arrays.
    Of course the mouse listener stuffs have been also specified.
    table.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent e){
          if (e.getClickCount() == 2){
             System.out.println(" double click" );
         } );Because the difference with the sample was the table model,
    I changed it with the DefaultTableModel class. At this point, the Double click does not work anymore.
    So I gues it should be an option which prevents double click to work.
    I thought of using mousePress() instead of mouseClick(), but it's very dangerous (I tried). . If by error the user clicks twice (instead of only once) the mousePress method is invoked twice for the same entry
    My question is now simple, may I use double click on a JTable with the default table model. If so, what I have to do ?
    Thanks a lot
    Gege
    TableModel dataModel = new AbstractTableModel() {
         public int getColumnCount() { return names.length; }
         public int getRowCount() { return data.length;}
         public Object getValueAt(int row, int col) {return data[row][col];}
         public String getColumnName(int column) {return names[column];}
         public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
         public void setValueAt(Object aValue, int row, int column) {
           data[row][column] = aValue;
         };

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class ClickIt extends MouseAdapter
        public void mousePressed(MouseEvent e)
            JTable table = (JTable)e.getSource();
            Point p = e.getPoint();
            if(e.getClickCount() == 2)
                System.out.println("table.isEditing = " + table.isEditing());
            int row = table.rowAtPoint(p);
            int col = table.columnAtPoint(p);
            String value = (String)table.getValueAt(row,col);
            System.out.println(value);
        private JTable getLeftTable()
            final String[] names = { "column 1", "column 2", "column 3", "column 4" };
            final Object[][] data = getData("left");
            TableModel dataModel = new AbstractTableModel() {
                public int getColumnCount() { return names.length; }
                public int getRowCount() { return data.length;}
                public Object getValueAt(int row, int col) {return data[row][col];}
                public String getColumnName(int column) {return names[column];}
                public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
                public void setValueAt(Object aValue, int row, int column) {
                    data[row][column] = aValue;
            JTable table = new JTable(dataModel);
            return configure(table);
        private JTable getRightTable()
            String[] colNames = { "column 1", "column 2", "column 3", "column 4" };
            JTable table = new JTable(new DefaultTableModel(getData("right"), colNames));
            return configure(table);
        private Object[][] getData(String s)
            int rows = 4, cols = 4;
            Object[][] data = new Object[rows][cols];
            for(int row = 0; row < rows; row++)
                for(int col = 0; col < cols; col++)
                    data[row][col] = s + " " + (row*cols + col + 1);
            return data;
        private JTable configure(JTable table)
            table.setColumnSelectionAllowed(true);
            table.setCellSelectionEnabled(true);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            table.addMouseListener(this);
            return table;
        private JPanel getContent()
            JPanel panel = new JPanel(new GridLayout(1,0,0,5));
            panel.add(new JScrollPane(getLeftTable()));
            panel.add(new JScrollPane(getRightTable()));
            return panel;
        public static void main(String[] args)
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(new ClickIt().getContent());
            f.pack();
            f.setVisible(true);
    }

  • Resize JTable columns by double-clicking header

    I want to implement column resizing in a JTable so that when the user double clicks on the column header with the mouse in the "resize zone" the column automatically adjusts to the width of the longest text in the column. this is how many applications behave (e.g. windows explorer) but I don't think I've ever seen a swing app that does this. I've looked in the API docs for JTable and JTableHeader and I can't see any way of finding out if the mouse is over the edge of a column header. does anyone know of a way to do this?

    Is this laf independant?import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One","Two","Three"};
        String[][] data = {{"R1-C1","R1-C2","R1-C3"},
                {"R2-C1","R2-C2","R2-C3"},
                {"R3-C1","R3-C2","R3-C3"}};
        JTable jt = new JTable(data, head);
        JScrollPane jsp = new JScrollPane(jt);
        content.add(jsp, BorderLayout.CENTER);
        JTableHeader jth = jt.getTableHeader();
        jth.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent me) {
            JTableHeader jth = (JTableHeader)me.getSource();
            int col = jth.columnAtPoint(me.getPoint());
            Rectangle r = jth.getHeaderRect(col);
            if (jth.getCursor().equals(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR))) {
              System.out.println("Cursor, col="+col);
        setSize(300, 300);
        setVisible(true);
      public static void main(String args[]) { new Test(); }
    }

  • JComboBox in JTable to activate when double-click instead of single-click

    I like to know if it is possible to change the default behaviour of JComboBox within the JTable,
    such that instead of single click on the JTable cell for the combo box's popup to appear, I double click on it instead.
    If it is possible please give some advice.
    Thanks.

    I couldn't get to settings because I couldn't scroll and don't know where that is located. I just saw a similar question from someone else. Three clicks on home button worked to turn off voiceover !! Phew!

  • JTable select row by double click, single click

    Hi all,
    I would like to double click on the row of the JTable and pop up a window for adding stuff, a single click for selecting the row and then in the menu bar I can choose to etither add, change or delete stuff. I try to use the ListSelectionModel but does not seems to distinguish between double or single click.
    Any idea, doc, samples?
    or I should not use ListselectionModel at all?
    thanks
    andrew

    Hi. I used an inner class. By using MouseAdapter u dont have to implement all methods in the interface..
    MouseListener mouseListener = new MouseAdapter()
    public void mouseClicked(MouseEvent e)
    if(SwingUtilities.isLeftMouseButton(e))// if left mouse button
    if (e.getClickCount() == 2) // if doubleclick
    //do something
    U also need to add mouselistener to the table:
    table.addMouseListener(mouseListener);
    As I said, this is how I did it.. There are probably alot of ways to solve this, as usual :). Hope it helps

  • JTable: double clicking triggers some action

    Hi there,
    A double click on a row of a JTable makes usually the underlying cell editable. This is the default behaviour of a JTable.
    In my application, the JTable only shows data, without giving users editing rights. Beside that, I'd like that a double click on a row, triggers other actions, like for example copying the selected row to the clipboard or sending it per mail....
    How do I have to configure the JTable for these purposes? Do I have to override the default ListSelectionListener? Did anybody make experiences with that?
    Thanks for your help,
    Best regards,
    edar

    like for example copying the selected row to the clipboardCtrl-C is the default KeyStroke combination for this.
    Beside that, I'd like that a double click on a row, triggers other actions...Then you need to add a MouseListener to the table. Then use:
    rowAtPoint(...);
    columnAtPoint(...);to determine which cell was clicked

  • How to start a function when double-clicked in a JTable

    hi there,
    i got a problem, i want that my JTable reacts when i make a double-click on any row... so that i can start a method that does the things i want to...
    how can i realize that?
    thx anyway
    Errraddicator

    i got a problem, i want that my JTable reacts when i
    make a double-click on any row... so that i can start
    a method that does the things i want to...
    how can i realize that?Easy, just put a mouse listener on it the same way you would a button! In the listener method, you can use
    if ( event.getClickCount()>1 ){
    doWhatever();
    doug

Maybe you are looking for

  • Downloading (and installing) LR4 in to new computer?

    I cant find a link, from where to download install file for LR4 in to my new computer. I have understood , that I do not need to make new purchase, that I could use the same activation code.

  • Error message Ajax submit failed: error =403, Forbidden

    I am trying to sign up for my eprintcenter email address but it will not allow and keeps saying in a yellow bar across the top  Ajax submit failed: error =403, Forbidden

  • Data usage when streaming from iTunes library to Apple TV?

    I'm trying to manage my GB data usage through my ISP and was curious if, while streaming between my iTunes library and Apple TV television shows I have previously downloaded, is that using any data whatsoever? I know I used data to actually download

  • Pdf opening problems

    Hi, Since a couple of month, i have problem downloading pdf from internet, in fact i can't download them (i download them with my phone et put them on dropbox...) I looked on different communities to find a solution but nothing helps me. Some talk ab

  • Problems with slow RD120 and Windows 2003 R2 Enterprise (32bit)

    Hello, I just installed Windows R2 2003 Enterprise SP2 to a RD120 with 10GB RAM. Everything went fine installing Windows and VMware Server. After copying a vm-guest from another Windows-host and opening the vm-console on the RD120 the whole system ne