我看过DefaultCellEditor,但它只包含文本字段,组合框或复选框。
有使用转轴器的便捷方法吗?
#1 楼
...并覆盖getCellEditorValue()方法:class SpinnerEditor extends DefaultCellEditor
{
private JSpinner spinner;
public SpinnerEditor()
{
super( new JTextField() );
spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 5));
spinner.setBorder( null );
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
spinner.setValue( value );
return spinner;
}
public Object getCellEditorValue()
{
return spinner.getValue();
}
}
评论
该解决方案具有许多用户可能不希望看到的某些行为。当用户在编辑器开始编辑之前输入数字时,JTable会将焦点集中在JSpinner上,而不是JSpinner中的编辑器上。这意味着用户刚刚键入的文本不会出现在任何地方。将此与DefaultCellEditor的行为进行对比:将新键入的键插入到当前值之后的单元格中。
–詹森
2012年1月27日,下午3:57
-1无效的cellEditor实现,因为它不遵守其通知合同
– kleopatra
2012年1月28日上午11:40
#2 楼
这是一个示例,解决了我对camickr的答案发表评论的问题。这是一个完整且可编译的示例。拿走你需要的东西,抛弃不需要的东西。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();
}
}
}
#3 楼
只需扩展DefaultCellEditor
并覆盖getTableCellEditorComponent()
方法即可返回JSpinner
。评论
+1 @Uri-另外,请参见“使用其他编辑器”java.sun.com/docs/books/tutorial/uiswing/components/…
– ssakl
09年12月10日在17:32
#4 楼
杰森的答案是完美的。为了帮助可能正在寻找“时间和日期”版本的其他人,我对Jason的代码进行了修改以适合他们。希望它能像Jason的帮助我一样帮助别人。import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class SpinnerInJTable {
static String[] columnNames = {
"Name","Time & Date"
};
static Object[][] data = {
{"Date One",new Date(Long.decode("1397091313404"))},
{"Date Two", new Date(Long.decode("1397001313404"))}
};
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(new SpinnerDateModel());
editor = ((JSpinner.DateEditor)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();
}
}
}
评论
谢谢大家。我以为我可以创建一个自定义组件,只是(错误地)认为可能会有另外一种方式。