gpt4 book ai didi

java - 具有输入验证功能的简单可编辑 JTable

转载 作者:行者123 更新时间:2023-12-01 12:12:30 29 4
gpt4 key购买 nike

我想写一个非常简单的JTable。

用户可以通过输入有效的 double 值来编辑单元格。如果用户的输入无效(即他们输入字符串),我希望我的表抛出无效输入,显示最后一个正确的输入,并向用户显示错误消息。

我还希望表中的数字格式如下:“###.##%”

我该如何去做这件事?

其他信息
我知道自己需要做什么,但很难将这些点联系起来。

例如,我知道为了获得正确的格式,我必须对 DefaultTableCellRenderer 进行子类化,但是我不太确定 getTableCellRendererComponent() 中我到底需要什么。方法。

我非常确定我必须将 DefaultCellEditor 子类化才能使编辑和错误检查正常工作。不过,我读过很多关于此的不同内容。我不确定检查有效输入的最佳位置在哪里。我还阅读了有关 InputVerifier 的内容,但不确定如何使用它或是否应该使用它。

一个关于这样的事情如何工作的小例子以及我必须扩展/覆盖的不同类/方法的简短解释将真正帮助我。

谢谢

最佳答案

I know that to get the formatting correct, I have to subclass DefaultTableCellRenderer however I am not really sure of exactly what I need in the getTableCellRendererComponent() method.

参见Table Format Rendering简单的方法来做到这一点。这种方法只是重写渲染器的 setValue(...) 方法。

I am pretty sure I will have to subclass DefaultCellEditor to get the editing and error checking to work properly.

这是我在论坛上找到的一些旧代码,用于创建自定义 IntegerEditor。第一列使用默认的整数编辑器,第二列使用自定义编辑器。

import java.awt.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.event.*;
public class TableIntegerEditor
{
static class IntegerEditor extends DefaultCellEditor
{
JFormattedTextField ftf;
NumberFormat integerFormat;
private Integer minimum, maximum;
private boolean DEBUG = false;

public IntegerEditor(int min, int max)
{
super(new JFormattedTextField());

setClickCountToStart(2);

ftf = (JFormattedTextField)getComponent();
ftf.setBorder(new LineBorder(Color.BLACK));
minimum = new Integer(min);
maximum = new Integer(max);

//Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);

ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

//React when the user presses Enter while the editor is
//active. (Tab is handled as specified by
//JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");

ftf.getActionMap().put("check", new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (!ftf.isEditValid()) //The text is invalid.
{
if (userSaysRevert())
{
ftf.postActionEvent(); //inform the editor
}
}
else
try
{
ftf.commitEdit(); //so use it.
ftf.postActionEvent(); //stop editing
}
catch (java.text.ParseException exc) { }
}
});
}

@Override
public boolean isCellEditable(EventObject event)
{
JTable table = (JTable)event.getSource();
return true;
}

//Override to invoke setValue on the formatted text field.
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
JFormattedTextField ftf = (JFormattedTextField)super.getTableCellEditorComponent(
table, value, isSelected, row, column);
ftf.setValue(value);

return ftf;
}

//Override to ensure that the value remains an Integer.
public Object getCellEditorValue()
{
JFormattedTextField ftf = (JFormattedTextField)getComponent();
Object o = ftf.getValue();
if (o instanceof Integer)
{
return o;
}
else if (o instanceof Number)
{
return new Integer(((Number)o).intValue());
}
else
{
try
{
return integerFormat.parseObject(o.toString());
}
catch (ParseException exc)
{
System.err.println("getCellEditorValue: can't parse o: " + o);
return null;
}
}
}

//Override to check whether the edit is valid,
//setting the value if it is and complaining if
//it isn't. If it's OK for the editor to go
//away, we need to invoke the superclass's version
//of this method so that everything gets cleaned up.
public boolean stopCellEditing()
{
JFormattedTextField ftf = (JFormattedTextField)getComponent();

if (ftf.isEditValid())
{
try
{
ftf.commitEdit();
}
catch (java.text.ParseException exc) { }

}
else
{
if (!userSaysRevert()) //user wants to edit
{
return false; //don't let the editor go away
}
}

return super.stopCellEditing();
}

/**
* Lets the user know that the text they entered is
* bad. Returns true if the user elects to revert to
* the last good value. Otherwise, returns false,
* indicating that the user wants to continue editing.
*/
protected boolean userSaysRevert() {
Toolkit.getDefaultToolkit().beep();
ftf.selectAll();
Object[] options = {"Edit",
"Revert"};
int answer = JOptionPane.showOptionDialog(
SwingUtilities.getWindowAncestor(ftf),
"The value must be an integer between "
+ minimum + " and "
+ maximum + ".\n"
+ "You can either continue editing "
+ "or revert to the last valid value.",
"Invalid Text Entered",
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
options,
options[1]);

if (answer == 1) { //Revert!
ftf.setValue(ftf.getValue());
return true;
}
return false;
}


}

private static void createAndShowGUI()
{
String[] columnNames = {"String", "Integer", "Integer2"};
Object[][] data =
{
{"a", new Integer(1), new Integer(10)},
{"b", new Integer(2), new Integer(20)},
{"c", new Integer(3), new Integer(30)}
};

JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
if (column == 0)
return String.class;
else
return Integer.class;
}

};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );

table.getColumnModel().getColumn(2).setCellEditor( new IntegerEditor(0, 200) );

DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Integer.class);
JComponent border = (JComponent)editor.getComponent();
border.setBorder( BorderFactory.createLineBorder( Color.RED ) );

JFrame frame = new JFrame("Integer Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( scrollPane );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}


}

如果您只想使用默认的 Double 编辑器,那么您只需将列的列类更改为 Double.class 即可。如果您需要特殊编辑,那么您将需要自定义 IntegerEditor 来满足您的双重要求。

关于java - 具有输入验证功能的简单可编辑 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196329/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com