Programmatically Making the Selected OutlineView Cell Editable

When you're using the OutlineView and you use the Tab key to move through its cells, the cells are shown to be selected, as below:

However, until you press the Space key in the selected cell, or until you click the mouse within it, you cannot edit it.

That's extremely annoying when you're creating a data-entry application. Your user would like to begin editing a cell as soon as they have tabbed into it. Needing to press Space first, or click the mouse in the cell first, is a cumbersome additional step that completely destroys your work flow. Below, you can see that an editable cell looks very different to one that is merely selected:

I.e., now I can type and the text changes. How to set up the OutlineView so that the Tab key makes the selected cell editable? Here's the constructor of the TopComponent you see above:

public ViewerTopComponent() {

    initComponents();

    setName(Bundle.CTL_ViewerTopComponent());
    setToolTipText(Bundle.HINT_ViewerTopComponent());

    setLayout(new BorderLayout());

    OutlineView ov = new OutlineView();

    final Outline outline = ov.getOutline();

    outline.setRootVisible(false);

    //When column selection changes, e.g., via Tab key,
    //programmatically start editing the cell:
    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int row = outline.getSelectedRow();
            int column = outline.getSelectedColumn();
            //Ignore the node column:
            if (row > -1 && row > -1) {
                outline.editCellAt(row, column);
            }
        }
    };
    outline.getColumnModel().getSelectionModel().addListSelectionListener(listSelectionListener);

    ov.setPropertyColumns(
            "city", "City", "state", "State");

    add(ov, BorderLayout.CENTER);

    em.setRootContext(
            new AbstractNode(Children.create(new CustomerChildFactory(), true)));

    associateLookup(ExplorerUtils.createLookup(em, getActionMap()));

}
Comments:

Thank You for the example !

But Geertjan is it possible for You to prepare small introductory tutorial/blog post about using ETable/OutlineView components ? For me (as a beginner) there is not enough information about it. I'm using nb sources for the lesson part, but easy and comprehensive tutorial would be great!

Posted by guest on April 02, 2012 at 09:49 PM PDT #

Here it is: https://blogs.oracle.com/geertjan/entry/simple_example_for_an_outlineview

Posted by Geertjan on April 02, 2012 at 10:28 PM PDT #

thanks a lot!, i'm digging through Your blog but right now i'm still at the beginning of the 2011 :)

Posted by slawek on April 03, 2012 at 02:06 AM PDT #

For me the issue is to do more advanced stuff like:
1) Not using beans
2) Custom rendering of a column value

Posted by Javier Ortiz on April 03, 2012 at 03:35 AM PDT #

Instead of entering always into edit mode in the most of the cases you want to do it only when user does something, e.g. presses a key.
As Outline is based on JTable you can use JTable.autostartsEdit client property.(see http://forums.netbeans.org/viewtopic.php?t=45966)
Unfortunately this solution is not really perfect now, although it is quite elegant. You have to press the first character twice. (see http://netbeans.org/bugzilla/show_bug.cgi?id=207202)

Posted by Gábor Tóth on April 04, 2012 at 07:00 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
24
25
26
27
28
29
30
   
       
Today