2010-09-17 45 views
3

我使用的是像JSpinner的表格單元格的編輯器,我有一個惱人的問題:使用的JSpinner像JTable中單元格編輯器

該單元保持在非編輯模式,直到我點擊進去,對於不可編輯的我意味着我不能寫入它(它沒有聚焦,所以它不接受輸入鍵盤),但我可以用上下箭頭(鍵盤)來改變數值。

所以,我必須做什麼來集中我的表格單元格,只要我按下鍵時它被選中?

除了這個問題我的SpinnerEditor類工作得很好。

謝謝大家。

回答

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

我有同樣的問題,但您的解決方案不適合我。 focusGain方法永遠不會被調用。我精確地複製了您的代碼並創建了一個名爲「editor」的字段,並將其分配給您要使用的值。代碼的其餘部分是用來設置JTable和CellEditor的? – Jason 2012-01-27 03:14:03

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(); 
     } 
    } 
} 
相關問題