【问题标题】:Use JSpinner like JTable cell editor像 JTable 单元格编辑器一样使用 JSpinner
【发布时间】:2011-04-13 19:31:12
【问题描述】:

我正在使用像表格单元格编辑器这样的 JSpinner,我有一个烦人的问题:

单元格保持在不可编辑模式,直到我点击它,对于不可编辑,我的意思是我不能写入它(它没有焦点,所以它不接受输入键盘)但我可以改变带有上下箭头(键盘)的值。

那么,当我按下一个键时,我必须做些什么来聚焦我的表格单元格?

除了那个问题,我的 SpinnerEditor 类运行良好。

谢谢大家。

【问题讨论】:

    标签: java swing focus tablecelleditor jspinner


    【解决方案1】:

    好的,我现在的解决方案是这样的:

    spinner.addFocusListener(new FocusListener() {
    
        public void focusGained(FocusEvent e) {
    
            // editor is ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
            // necessary to set focus on the text component of jspinner
            editor.requestFocus();
    
            // this if you want to select the displayed text of jspinner
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("run");
                    editor.selectAll();
                }
            });
        }
    
        public void focusLost(FocusEvent e) {}
    });
    

    【讨论】:

    • 我有同样的问题,但你的解决方案对我不起作用。 focusGain 方法永远不会被调用。我完全复制了您的代码并创建了一个名为“编辑器”的字段并将其分配给您说要使用的值。用于设置 JTable 和 CellEditor 的其余代码是什么样的?
    【解决方案2】:

    这是一个完整的例子。它不仅仅是将 JSpinner 放在表格中,因此您可以阅读并获取所需的内容。我发现 Blow 的答案不完整,它对我不起作用,所以这里有一些你可以编译和运行的东西。

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class JSpinnerInTables {
        static String[] columnNames = {
            "Name","Value"
        };
        static Object[][] data = {
            {"one",1.0},
            {"two",2.0}
        };
        public static void main( String[] args ) {
            JFrame frame = new JFrame();
            JTable table = new JTable(data,columnNames);
            //table.setSurrendersFocusOnKeystroke(true);
            TableColumnModel tcm = table.getColumnModel();
            TableColumn tc = tcm.getColumn(1);
            tc.setCellEditor(new SpinnerEditor());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(table);
            frame.pack();
            frame.setVisible(true);
        }
        public static class SpinnerEditor extends DefaultCellEditor
        {
            JSpinner spinner;
            JSpinner.DefaultEditor editor;
            JTextField textField;
            boolean valueSet;
    
            // Initializes the spinner.
            public SpinnerEditor() {
                super(new JTextField());
                spinner = new JSpinner();
                editor = ((JSpinner.DefaultEditor)spinner.getEditor());
                textField = editor.getTextField();
                textField.addFocusListener( new FocusListener() {
                    public void focusGained( FocusEvent fe ) {
                        System.err.println("Got focus");
                        //textField.setSelectionStart(0);
                        //textField.setSelectionEnd(1);
                        SwingUtilities.invokeLater( new Runnable() {
                            public void run() {
                                if ( valueSet ) {
                                    textField.setCaretPosition(1);
                                }
                            }
                        });
                    }
                    public void focusLost( FocusEvent fe ) {
                    }
                });
                textField.addActionListener( new ActionListener() {
                    public void actionPerformed( ActionEvent ae ) {
                        stopCellEditing();
                    }
                });
            }
    
            // Prepares the spinner component and returns it.
            public Component getTableCellEditorComponent(
                JTable table, Object value, boolean isSelected, int row, int column
            ) {
                if ( !valueSet ) {
                    spinner.setValue(value);
                }
                SwingUtilities.invokeLater( new Runnable() {
                    public void run() {
                        textField.requestFocus();
                    }
                });
                return spinner;
            }
    
            public boolean isCellEditable( EventObject eo ) {
                System.err.println("isCellEditable");
                if ( eo instanceof KeyEvent ) {
                    KeyEvent ke = (KeyEvent)eo;
                    System.err.println("key event: "+ke.getKeyChar());
                    textField.setText(String.valueOf(ke.getKeyChar()));
                    //textField.select(1,1);
                    //textField.setCaretPosition(1);
                    //textField.moveCaretPosition(1);
                    valueSet = true;
                } else {
                    valueSet = false;
                }
                return true;
            }
    
            // Returns the spinners current value.
            public Object getCellEditorValue() {
                return spinner.getValue();
            }
    
            public boolean stopCellEditing() {
                System.err.println("Stopping edit");
                try {
                    editor.commitEdit();
                    spinner.commitEdit();
                } catch ( java.text.ParseException e ) {
                    JOptionPane.showMessageDialog(null,
                        "Invalid value, discarding.");
                }
                return super.stopCellEditing();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2011-09-27
      • 2011-03-26
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      相关资源
      最近更新 更多