像JTable单元格编辑器一样使用JSpinner

时间:2010-09-17 16:04:58

标签: java swing focus tablecelleditor jspinner

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

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

那么,当我选择一个按键时,我需要做些什么来集中我的表格?

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

谢谢大家。

2 个答案:

答案 0 :(得分:11)

这是一个完整的例子。它不仅仅是将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();
        }
    }
}

答案 1 :(得分:-2)

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

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