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 );
}

Similar Messages

  • 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.

  • 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

  • Capture Single-Click for BDC

    Moderator message: Do NOT offer points
    Hello Gurus,
    I am doing a BDC for transaction CM21. Everything seems to be fine but I ran into a problem. In order to dispatch planned orders, you have to do a single-click on the planned orders and click on the dispatch button.
    The big issue i have now is I don't know anyway to capture a single click on BDC.
    I know somebody could have ran into this problem before.
    Please help.
    <<text removed>>
    Thanks.
    Edited by: Matt on Nov 23, 2008 5:32 PM - read the Rules of Engagement

    Don't forget that BDC doesn't work with enjoy transaction (in fact with controls, search OSS there are some notes on this subject) , if BDC_CURSOR to put the cursor on the field is not possible or doesn't work, you may not be able to fulfil your requirement and will have to look for a BAPI to do the job. (Sometimes adding many "Enter" to the BDC enable the transaction to work but not always)
    OSS [Note 311440 - Batch input and controls|https://service.sap.com/sap/support/notes/311440]
    Regards

  • 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);
    }

  • 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?

  • How to separate double click action and single click action

    in my mouse action listerner for JTable.
    i do two actions, when user doule click column header, sort the column,
    when single clicked, select all cells in this column.
    but when user double clicked, the action attached with single click also triggered.(column sorted but also selected)
    how to separate them.

    Ok, so despite my earlier suggestion being basically the only solution offered anyway on the web it appears that Lokust is correct and that the single click operation would be performed as well as the double click one. to get round this u have to implement a Timer, i'm not going to post my whole code here because most of it is irrelevent, just add this code in the correct places and it should work fine. apologies to all format junkies for not using code tags!
    import java.util.Timer;
    import java.util.TimeTask;
    public class WhateverClassName implements MouseListener {
    private Timer t;
    private boolean doubleclick;
    myTable.addMouseListener(this);
    public void mouseClicked(MouseEvent e) {
         t = new Timer();
         if (e.getClickCount() == 2){
              doubleclick = true;
         else if(e.getClickCount()==1){
              t.schedule(new MyTimerTask(),500);
    /*You need all these methods as well */
    /* (non-Javadoc)
    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
    public void mouseEntered(MouseEvent arg0) {
    /* (non-Javadoc)
    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
    public void mouseExited(MouseEvent arg0) {
    /* (non-Javadoc)
    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
    public void mousePressed(MouseEvent arg0) {
    /* (non-Javadoc)
    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
    public void mouseReleased(MouseEvent arg0) {
    class MyTimerTask extends TimerTask{
    /* (non-Javadoc)
    * @see java.util.TimerTask#run()
         public void run() {
              if(doubleclick){
                   System.out.print("double click");
              else{
                   System.out.println("single click");
              t.cancel();
    This assumes that for it to be considered a double click user has to click twice within .5 seconds.
    Let me know if that works or not, sorry its a bit messy!

  • SetRowSelectionInterval() not working on single-click in JTable

    I have code that tries to move the highlighted row in a JTable back to the line you just edited instead of moving to the next line when you press enter after editing a cell (like you would normally see in a JTable or any spreadsheet program). If you double-click in the cell and edit the name, the code works correctly, but if you only single click the cell and enter the name, the code doesn't move the highlight - even though I know the code is being executed.
    The code is nothing more than:
    if(row == 0)
    setRowSelectionInterval(0,0);
    else
    setRowSelectionInterval(row-1,row);
    with row being the row you want to set the highlight to.
    Does anyone know why this would work for a double-click edit of a JTable but not a single-click?

    even though I know the code is being executed.How do you know that? Do you have a println immediately before the if (row == 0)of the code sample you gave us?

  • 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

  • Clicking in a cell in a JTable that autoscrolls horz

    I have a JTable inside of a JScollPane. The JTable is wider than the JScrollPane so the scroll bar becomes enabled. If I have a column that is paritally obstructed and a I click in a cel in the column the scroll panel scrolls horizontally in an attemp to make the entire cell visible. Is there a way to stop the auto scrolling without making the table disabled?
    Thanks
    Chris

    try with this.
    table.setAutoscrolls(false);

  • SharePoint JS grid paging, capturing values using single click event

    Hi 
    I need some help with getting the following work using the SharePoint JS grid, the reference articles available are not sufficient to continue work. 
    1) Setting up Paging
    2) Capturing individual field values on single click or any other relevant event.
    Sample code or references would be of great help.
    References
    http://msdn.microsoft.com/en-us/library/office/ee535898(v=office.14).aspx
    http://answers.flyppdevportal.com/categories/sharepoint2010/sharepoint2010programming.aspx?ID=d6e32632-d0e4-4337-95c3-c2d06fc3ef86
    http://answers.flyppdevportal.com/categories/sharepoint2010/sharepoint2010programming.aspx?ID=5c952c6d-b2be-4563-b805-b00e7c8136ff
    http://msdn.microsoft.com/en-us/library/ff681039.aspx
    http://social.msdn.microsoft.com/Forums/en-US/a439fcc2-42de-45ca-9f74-935498e0d246/js-grid-oncelleditcompleted-event-infinitely-fired?forum=project2010custprog  
    Thanks,
    Sharat
    Sharat Menon (Sharepoint Developer)

    Hi,
    Please try to use PagingFilter class to achieve paging in JS Grid.
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.jsgrid.pagingfilter.aspx
    Here is a similar thread for you to take a look at:
    https://go4answers.webhost4life.com/Example/sharepoint-jsgrid-2692.aspx
    If you want to implement more complex functionalities for a list, I suggest you use SPGridView to achieve it.
    Best regards
    Dennis Guo
    TechNet Community Support

  • How to detect if user clicks on another cell in a JTable while editing?

    I use a custom TableModel which extends AbstractTableModel and I serialize the TableModel to save the data in a file. Sometimes when I try to serialize, my program crashes after putting in random data in to the table. I have reduced the problem down to if a user is editing a field and clicks on another cell instead of hitting "enter", then then program will crash if that was the last action when trying to save.
    I am thinking it might be something with the custom TableModel class, like I am missing a method or something. I have getColumnName, getColumnCount, getValueAt, setValueAt, isCellEditable
    Thanks

    Clicking on another cell should not cause a problem as this will stop editing in the previous cell. However if you click on your save button directly then the editor is still being used. Use the following after creating your table:
    table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

  • How to know the row and col numbers of a cell in JTable at mouseclicked

    Hi, I'm coding an app using swing and JTable, in this table I will display the records from a database, now, each time the user has click in any cell I want to display it value, whit this method:
        private void setpuntero(JTable table, int row, int col){
            Object o = table.getValueAt(row, col);
            System.out.println("Valor de la celda: "+o.toString());
    }The mouseclick event was coded like this:
                table.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        setpuntero(table, 1, 3);
                });My question is:
    how in the mouseClicked I can pass to setpuntero() method the dynamic values of the cell that the user clicked?
    In this example, setpuntero(table, 1, 3); I have chosen the cell(1,3) to display the value, but really I don't know how to capture those values dinamically using the mouse click
    Regards
    Herbert

    perhaps:
          public void mouseClicked(MouseEvent e)
            int row = table.getSelectedRow();
            int col = table.getSelectedColumn();
            String selected = table.getValueAt(row, col).toString(); // this will need to be changed if not String data
          }

  • How to change the Background color of a cell in JTable

    hi!
    Actually i want to change the background color of few cells in JTable
    is there any method to make this change in JTable ?
    and also is it possible to have 5 rows with single column and 5 rows with 3 columns in a single JTable

    i want to change the background color of few cells in JTableDepending on your requirements for the coloring of cells it may be easier to override the prepareRenderer() method of JTable:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=610474

Maybe you are looking for

  • Error in ebs

    Hi Experts. For electronic bank statement,Now I've done all the config work when I try to import the file FF_5 (RFEBKA00), I got an error Termination in statement no 00000 of acct closing record 62F missing Message no. FV151 thank you Regards Sreedha

  • ITunes isn't allowing me to move my purchases to anther pc!

    I moved my movies to the new pc (into the proper dir) and now iTunes will not play them!  Why is iTunews not allowing me to move my purchases to the other pc?

  • IMovie '09 not allowing a me to fade a song manually?

    I have two music tracks that I want to play, one right after another, and have the first track fade out for five seconds before starting the next track (song from my iTunes music library which I selected and dragged into my project from the media bro

  • Doubt in cloning

    Dear all, Our production database is in archive log mode and we will take RMAN backup daily and cold backup weekly. After taking cold backup and creating a new instance with cold backup using rapid clone, the new instance doesnot have archive log mod

  • Profiles in location master data

    Hi guys,          There are certain profiles in location master data FR view , could you tell me how to create all this profiles? Thanks and regards Thamizhchelvan G