JTable with a complex editor

前端 未结 4 1911
花落未央
花落未央 2021-02-02 03:33

I have many custom editors for a JTable and it\'s an understatement to say that the usability, particularly in regard to editing with the keyboard, is lacking.

The main

4条回答
  •  遥遥无期
    2021-02-02 03:40

    I had very similar problem. In my case I had complex TableCellEditor which consists of JSpinner and some other components. The problem was that when cell editor started I wanted to transfer focus to its internal component. I fixed this by calling panel.transferFocusDownCycle() but this in turn caused keyboard events to stop working - when my internal component had focus and I pressed key up, I was expecting that component will intercept this event and change its value. Instead table changed row focus to one above... I fixed this by adding KeyListener and dispatching all key events directly to the internal component.

    This is wrapper class based on JPanel I wrote to make my life easier.

    public class ContainerPanel extends JPanel implements KeyListener, FocusListener {
    
        private JComponent component = null;
    
        public ContainerPanel(JComponent component) {
            this.component = component;
            addKeyListener(this);
            addFocusListener(this);
            setFocusCycleRoot(true);
            setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
            add(component);
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
            component.dispatchEvent(e);
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            component.dispatchEvent(e);
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            component.dispatchEvent(e);
        }
    
        @Override
        public void focusGained(FocusEvent e) {
            component.transferFocusDownCycle();
        }
    
        @Override
        public void focusLost(FocusEvent e) {
        }
    }
    

提交回复
热议问题