3

I have a JTable in my application. I have a custom renderer setup on the table (a JTextArea, with line-wrapping enabled) which allows for multi-line content. The contents of the JTable cells are expected to overflow the bounds of the cell in some cases. I want to do the following:

Instead of making the user drag the row border to resize the cell, I want the user to be able to double-click the row border, so when I detect this, I can automatically resize the height of the cell (height of the row) to show the entire contents of the cell.

My question is, what is the best way to detect a double-click on the row border? I have gotten this to work by setting up a MouseListener on the JTable with a mouseClicked method that looks like this:

public class MouseButtonInputListener extends MouseInputAdapter
{
    private JTable fTable;

    public MouseButtonInputListener(JTable parentTable)
    {
       fTable = parentTable;
    } 

    @Override
    public void mouseClicked(MouseEvent e)
    {

        // Some pre-processing here...

        if(e.getClickCount() == 2 && 
           fTable.getCursor().getType() == Cursor.N_RESIZE_CURSOR)
        {
            // Auto-adjust row height to fit contents...            
        }

    }
}

While this works, i'm not very happy with the line:

fTable.getCursor().getType() == Cursor.N_RESIZE_CURSOR

Any suggestions on a better way to do this? Is the above a reliable approach on all platforms?

eternaln00b
  • 1,043
  • 9
  • 18
  • Would something like [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513) be an alternative? – trashgod Feb 04 '12 at 20:14
  • @trashgod, not quite sure how that would help. i've already figured out the "resize row to correct size" part. I'm really only interested in knowing if my technique for checking that the user has clicked on the boundary of the cell (the resize cursor shows up when hovering over the row boundary) is a correct / robust technique. Can I expect the cursor on ALL platforms to be Cursor.N_RESIZE_CURSOR when hovering over a row boundary? The javadoc gives no indication otherwise, but I want a confirmation. – eternaln00b Feb 04 '12 at 21:46
  • Right, it wouldn't help; I posit @camickr's example as a potential alternative. Re `Cursor.N_RESIZE_CURSOR`, I guess you could post an [sscce](http://sscce.org/) and ask people to report their findings. – trashgod Feb 05 '12 at 00:46

1 Answers1

2

After some searching online, I found the following page:

http://java.sun.com/products/jlf/ed2/book/HIG.Behavior.html

amongst other things, it specifies mouse cursor behavior under different circumstances. According to the section on "Pointer Feedback", the cursor switches between a N_RESIZE_CURSOR and S_RESIZE_CURSOR depending on whether it is the upper or lower boundary of a component that is being hovered over. It's interesting to note that on 2 out of 3 platforms (Mac and Windows), these cursors are exactly the same. Anyway, it follows that my code should therefore read:

@Override
public void mouseClicked(MouseEvent e)
{
    // Some pre-processing here. Determine row at mouse click location
    int cursorType = fTable.getCursor().getType();
    if(e.getClickCount == 2 && 
      (cursorType == Cursor.N_RESIZE_CURSOR || cursorType == CURSOR.S_RESIZE_CURSOR))
    {
        // Resize row appropriately.
    }
}

This will work on all platforms. Thanks for the inputs and comments everyone.

eternaln00b
  • 1,043
  • 9
  • 18