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

Similar Messages

  • Needs double click to edit cell in JTable

    I know this is bad design but there's no other way that I could possibly meet the requirement which is to have dynamic components. Anyhow, the requirement is to have any kind of components(e.g. TextFields, Combobox, Regular expression fields, a panel with a number of checkboxes) in a table cell in the same column. It would have been easier to do this by using the the DefaultCellEditor, with the 'panel containing a number of checkboxes' I cannot use it.
    I already have an implementation for this requirement. There was no problem with it when we used Java 1.5. But when we shifted to Java 1.6, I noticed that I need to double-click on a cell so that I can edit it. I did not notice this behavior at all with 1.5. What could have changed in 1.6?
    Below, are my (trimmed-down) codes:
    //TestComponent.java
    public class TestComponent extends JPanel{
    private int componentHeight=16;
    public TestComponent(int i){
    this.setPreferredSize(new Dimension(90, this.componentHeight));
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    switch (i){
    case 0: createTextField(); break;
    case 1: createCheckBox(); break;
    case 2: createCombo(); break;
    private void createTextField(){
    String text = null;
    int textFieldMaxLength = 5;
    JTextField oText = new JTextField(textFieldMaxLength);
    // add it to this Panel:
    this.add(oText);
    // set the data:
    text = "test";
    oText.setText(text);
    // set size for this TextField:
    Dimension fieldSize = new Dimension(
    textFieldMaxLength * 13, this.componentHeight);
              oText.setPreferredSize(fieldSize);
              oText.setMinimumSize(fieldSize);
              oText.setMaximumSize(fieldSize);     
    private void createCheckBox(){
    JCheckBox chkbox = new JCheckBox();
    this.add(chkbox);
    private void createCombo(){
    JComboBox combo = new JComboBox(new String[]{"apple", "orange", "plum", "grapefruit"});
    Dimension fieldSize = new Dimension(                    5 * 13, this.componentHeight);
              combo.setPreferredSize(fieldSize);
              combo.setMinimumSize(fieldSize);
              combo.setMaximumSize(fieldSize);
    this.add(combo);
    //ComponentCellEditor.java
    public class ComponentCellEditor extends AbstractCellEditor
         implements TableCellEditor, Serializable{
    protected JComponent editorComponent = null;     
    public Component getComponent() {
    return editorComponent;
    public Object getCellEditorValue() {
    return editorComponent;
    public boolean isCellEditable(EventObject anEvent) {
    return true;
    public boolean shouldSelectCell(EventObject anEvent) {
    return true;
    public boolean stopCellEditing() {
    fireEditingStopped();
    return true;
    // Implementing the TreeCellEditor Interface
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    this.editorComponent = (TestComponent)value;
    return editorComponent;
    //ComponentCellRenderer.java
    public class ComponentCellRenderer implements TableCellRenderer
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    TestComponent oComp = (TestComponent) value;
    if (isSelected) {
    oComp.setForeground(table.getSelectionForeground());
    oComp.setBackground(table.getSelectionBackground());
    } else {
    oComp.setForeground(table.getForeground());
    oComp.setBackground(table.getBackground());     
    return oComp;
    //TestTable.java
    public class TestTable
    public static void main(String []args){
    String columns[] = {"Text", "Value"};          
    Class types[] = {String.class, TestComponent.class};
    boolean editable[] = {false, true};
    AsrTable table = new AsrTable(new Dimension(200, 150), columns, types, editable);
    table.addRow(new Object[]{"Field1", new TestComponent(0)});
    table.addRow(new Object[]{"Field2", new TestComponent(1)});
    table.addRow(new Object[]{"Field3", new TestComponent(2)});
    table.setDefaultEditor(TestComponent.class, new ComponentCellEditor());
    table.setDefaultRenderer(TestComponent.class, new ComponentCellRenderer());
    table.setShowGrid(false);
    table.setRowHeight(20);
    table.setRowSelectionAllowed(false);
    JFrame frame = new JFrame();
    frame.addWindowListener( new WindowAdapter() {
         public void windowClosing(WindowEvent e)
         Window win = e.getWindow();
         win.setVisible(false);
         win.dispose();
         System.exit(0);
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(pane);
    frame.pack();
    frame.setVisible(true);
    }

    My last post doesn't have Code Formatting.
    // TestComponent.java
    public class TestComponent extends JPanel{
         private int componentHeight=16;
         public TestComponent(int i){
              this.setPreferredSize(new Dimension(90, this.componentHeight));
              setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
              switch (i){
                   case 0: createTextField(); break;
                   case 1: createCheckBox(); break;
              case 2: createCombo(); break;
         private void createTextField(){
              String text = null;
              int textFieldMaxLength = 5;
              JTextField oText = new JTextField(textFieldMaxLength);
              // add it to this Panel:
              this.add(oText);
              // set the data:
              text = "test";
              oText.setText(text);
              // set size for this TextField:
              Dimension fieldSize = new Dimension(
                        textFieldMaxLength * 13, this.componentHeight);
              oText.setPreferredSize(fieldSize);
              oText.setMinimumSize(fieldSize);
              oText.setMaximumSize(fieldSize);
         private void createCheckBox(){
              JCheckBox chkbox = new JCheckBox();
              this.add(chkbox);
         private void createCombo(){
              JComboBox combo = new JComboBox(new String[]{"apple", "orange", "plum", "grapefruit"});
              Dimension fieldSize = new Dimension( 5 * 13, this.componentHeight);
              combo.setPreferredSize(fieldSize);
              combo.setMinimumSize(fieldSize);
              combo.setMaximumSize(fieldSize);
              this.add(combo);
    // ComponentCellEditor.java
    public class ComponentCellEditor extends AbstractCellEditor 
         implements TableCellEditor, Serializable
         protected JComponent editorComponent = null;     
         public Component getComponent() {
              return editorComponent;
         public Object getCellEditorValue() {
              return editorComponent;     
         public boolean isCellEditable(EventObject anEvent) {
              return true;
         public boolean shouldSelectCell(EventObject anEvent) {
              return true;
         public boolean stopCellEditing() {
              fireEditingStopped();
              return true;
    //  Implementing the TreeCellEditor Interface
        public Component getTableCellEditorComponent(JTable table, Object value,
              boolean isSelected, int row, int column) {
              this.editorComponent = (TestComponent)value;
              return editorComponent;
    // ComponentCellRenderer.java
    public class ComponentCellRenderer implements TableCellRenderer
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                   int column) {
              TestComponent oComp = (TestComponent) value;
              if (isSelected) {
                   oComp.setForeground(table.getSelectionForeground());
                   oComp.setBackground(table.getSelectionBackground());
              } else {
                   oComp.setForeground(table.getForeground());
                   oComp.setBackground(table.getBackground());
              return oComp;
    // TestTable.java
    public class TestTable
         public static void main(String []args){
              String columns[] = {"Text", "Value"};          
              Class types[] = {String.class, TestComponent.class};
              boolean editable[] = {false, true};
              AsrTable table = new AsrTable(new Dimension(200, 150), columns, types, editable);
              table.addRow(new Object[]{"Field1", new TestComponent(0)});
              table.addRow(new Object[]{"Field2", new TestComponent(1)});
              table.addRow(new Object[]{"Field3", new TestComponent(2)});
              table.setDefaultEditor(TestComponent.class, new ComponentCellEditor());
              table.setDefaultRenderer(TestComponent.class, new ComponentCellRenderer());
              table.setShowGrid(false);
              table.setRowHeight(20);
              table.setRowSelectionAllowed(false);
              JFrame frame = new JFrame();
              frame.addWindowListener( new WindowAdapter() {
                   public void windowClosing(WindowEvent e)
                        Window win = e.getWindow();
                        win.setVisible(false);
                        win.dispose();
                        System.exit(0);
              JScrollPane pane = new JScrollPane(table);
              frame.getContentPane().add(pane);
              frame.pack();
              frame.setVisible(true);
    }

  • Double clicking for editing JCheckBox in JTable under java 1.6?

    Hi all,
    I have a JTable with JCheckboxes in a column (and associated renderer and editor). All worked good with/until java 1.5.
    Now with java 1.6 I have to click two times on the checkbox in order to change the selection...
    Anyone has experimented a similar problem??

    I have spent some hours over this strange problem finding no way to solve it and no workaround.
    Could this be a bug introduced with java 1.6?
    Note that the behaviour is more strange as what I hade described in my last post:
    - mouse click on a cell with a checkbox: checkbox CHANGE state
    - mouse click on other celll with checkbox: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - ... and so on
    Really strange, at least for me...

  • Keypressed event for a particular  edited cell of jtable

    hi friend,
    how to write the key pressed event for the edited cell of jtable which having focus on that particular cell.
    pls help me out.

    [http://catb.org/~esr/faqs/smart-questions.html]
    [http://mindprod.com/jgloss/sscce.html]
    db

  • How to detect mouse clicks on desktop?

    Hi All,
    I need to detect mouse clicks on the desktop and find out mouse position on the screen. How to listen to mouse events if I don't have any swing components like containers, panels, windows, etc? All things like "addMouseListener" are supposed to be with such components. But my program is running in the background and it doesn't have any graphical interfaces. So, I need to detect if a mouse button is pressed at any position on the screen.
    Thanks for any help!

    Why? What program feature do you intend to offer, by knowing what a user clicks on (besides the application itself).Hi, well what I have to do is to start the application minimized in the system tray. Then the application must be listening for crtl+shift+left click in any part of the desktop or an opened application, when that happens, I have to show a window asking if the user want to take a screenshot starting in the x,y point he clicked, once done, I have to take an screenshot...so on.
    How can I do that? Can anybody give me a hint? Thanks in advance.

  • Mouse Double Click on an editable cell of JTable

    Hi Pros:
    Maybe this is an old question but no answser from Forum.
    I have a JTable with adding MouseListener. I tried to put double click behavior on nay row in the table. The problem was that this action can obly work on the uneditable cell and do not work on editable cell.
    Anyone have ideas and help me.
    Thank you!

    Hi Wang,
    I have a problem similar to the one you have some time back.
    I have a query for which I need to use PreparedStatement .The query runs likes this :-
    String str = " Select ? , ename from emp where deptno ? ";
    The values of ? need to be assigned dynamically.
    But I cannot create Prepared Statement from this query .
    If you have got answer to your questions can you inform me at
    [email protected]
    Thanks in advance

  • Unable to edit cells in JTable on single click of the cell.

    Hi,
    I am unable to edit a cell in JTable on single click of the cell. If I double click on the cell, I am able to edit it. Please help me.
    Thanks
    Subbu

    Thanks for all replies. Now, i am able to edit the cell on single click.

  • Capture single click in a cell in JTable

    I have a Table which has a column that needs to act like a URL link. I.e. when clicked it will bring up a web page. How can I get the mouse event when a specific cell is clicked as well as what cell was clicked?
    The table can not be editable as well as only row selection can be allowed. The user can select rows (not cells), but needs to be able to click a certain column to fire an event?
    Thanks

    To make your table read-only, simply assign it a TableModel which has the method:
    public boolean isCellEditable( int row, int col ){
    return false;
    }To get cell co-ordinates from a mouse click, in your MouseListener class, just do this:
    public void mouseClicked( MouseEvent e ){
    Point p = e.getPoint();
    int row = myJTable.rowAtPoint( p );
    int col = myJTable.columnAtPoint( p );
    }

  • Mouse click on Table cell

    Hi
    I am resetting the model of table on mouse clicking. And while doing that cell just flash out and dint come into the editable mode. Is there any way so that i can start edting of cell just after resetting model automatically. Please help me out
    Thanks
    rOhit

    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • Detect mouse clicks or keyboard events on desktop or everywhere

    Hi,
    What I have to do is to start the application minimized in the system tray. Then the application must be listening for crtl+shift+left mouse click in any part of the desktop or an opened application, when that happens, I have to show a window asking if the user want to take a screenshot starting in the x,y point he clicked and then take the screenshot.
    What I don't know how to do is to detect the mouse and keyboard events when the application is supposed to be running minimized in the system tray.
    How can I do that? Can anybody give me a hint?
    Thanks in advance.

    It's not possible with plain Java. You will need native code for this. Take a look at JNA.

  • How to detect mouse click event?

    Hello,
    I would like to have a vi to detect left, right and no mouse click. I mean that in the vi attached, Button2 should be 0 (no click), 1 (left c.) or 2 (right c.) depending on the event occured in i-1. cycle. My vi is the modyfied version of the one found here:
    http://www.ni.com/example/27663/en/
    Sometimes it works fine, but another time nothing happens when I click.
    I think the main problem is with the execution times at the for loop and event structure.
    Could you help me how to deal with the problem?
    Thanks you!
    Attachments:
    mouse1.vi ‏12 KB

    Hi VampireEmpire,
    Your For loop iterates twice. If an event occures during first iteration everything is fine - Button 2 refreshes during second iteration. But what happens when an event occures during second iteration? Does Button 2 have a possibility to refresh? 
    1. Do you see the problem now?
    2. And if you do - do you really need For loop? I would suggest you trying removing it and connecting shift register to the while loop.
    Bluesheep

  • Detecting mouse click on a line

    I'm learning making GUI apps in Java.
    Here is my problem. Suppose I draw a line on a JPanel. If I want to find out if the line is clicked or not, what is the best way to do it?
    I did some research, looked at API. If I suppose my line is Line2D, then since its a line it doesn't have an area, so its contains method always returns false (..from API). So i dig around some more and came up with the solution of getting the shape from the stroke and calling its contains method.
    This is a code for what i mean..
    private Line2D.Double testLine;
    //mouse clicked event
    public void mouseClicked(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            BasicStroke stroke = new BasicStroke(4);
            Shape testShape = stroke.createStrokedShape(testLine);
            if(testShape.contains(x,y)){
                System.out.println("this will be printed if clicked on line");
        }Well, the above solution works fine.
    Is it the right way of doing it or is there a better way around for this?
    Any help will be appreciated.
    Thanks.

    When trying to test if a line was pressed I usually test if the distance between the mouse coordinates and the line is smaller than some constant (usually 5 pixels).
    For this you have to write your own code to calculate the distance between a point and a line.

  • Detect mouse click in FlashPlayer ActiveX

    I'm hosting the FlashPlayer ActiveX control (Flash9f.ocx) in
    a VB 6 application. The OCX exposes the following events:
    FlashCall, OnProgress, OnReadyStateChange and FSCommand. It does
    not expose the traditional Click, MouseUp or MouseDown events (for
    example) so is there a way to receive mouse click events in the
    hosted application?
    Also, I am unable to find any documentation on the FlashCall
    event. Any ideas what it is and how it works?
    Thanks to any and all.

    jList1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    jList1_mouseClicked(e);
    public void jList1_mouseClicked(MouseEvent e){
    JOptionPane.showMessageDialog(null,"test"); }
    I got it. Thanks.
    I forgot to add the MouseListener initally

  • Jtable in cell how to single mouse click make the cell selected.

    it seems need double click make cell selected.
    thanks!!!

    Hi,
    these link will help you.
    It has code for both examples, with 1 click and with 2 clicks:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=362073&tstart=0&trange=15
    sergio.

  • How to prevent Cell from getting focus when I click on a cell in JTable

    Hi,
    I have a new problem which I did not have when using jdk1.3. I have a non editable JTable. Now whenever I select a row the row gets highlighted - which is ok but at the same time the cell on which I click ( to select the row ) also gets focus.
    Previously I used to extend JTable and override the isManagingFocus method to return false. But now it doesnt seem to work
    What should
    Thanks
    --J                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Well, I'm still using JDK1.3 and I don't get the behaviour you describe.
    When isManagingFocus is true then using the tab key will cause focus to move from cell to cell within the JTable and focus will never leave the JTable.
    When isManagingFocus is false then using the tab key will cause focus to move the the JTable to the next component on the JFrame.
    In both cases once focus is on the JTable an individual cell is always highlighted to indicate it has focus.
    The question is if your program only cares which row has been selected, why do you care if an individual cells appears to have focus?

Maybe you are looking for

  • DataProcessor generating XML containing special ampersand, equals sign etc

    Hi, Using 10.1.3.3.3 I am using the BIP API's to generate reports for an application, but have a problem where the DataProcessor is generating XML containing ampersands and equal signs '&', '=' from the database. The characters are causing the FOProc

  • MPEG-2 Encoding Results in Silent DVD

    I exported a 23.98fps video from FCP to Compressor and then encoded with the MPEG-2 6.2mbps 2-pass setting under the "DVD: Best Quality 90 Minutes" folder. The DVD I burned from that file in DVD Studio Pro is silent. No audio. What am I doing wrong?

  • CS5 using Bridge and a Nikon D750

    I have CS5 and a NIkon D750, having trouble with the DNG converter and finding a good download.  The one I found doesn't seem to be doing anything.

  • HT5312 How to reset security question on itunes

    How to reset security question

  • Error -10401 occurred at CTR Control

    i am trying to build a vi that wil count the number of digital pulses from a source between a specified start and stop trigger . in my case i have not wired the digital trigger yet i use boolean buttons to give my start and stop signals . i am not ve