我正在验证JTable的特定列。我的要求是该列的所有单元格应该只允许使用字母表。我已将keylistener添加到Jtable的对象中,并编写了如下逻辑:
public void keyTyped ( KeyEvent e ) {
if(!(e.getKeyChar()>='a' &&e.getKeyChar()>='z')) {
getToolkit.beep();
}
}
最初当焦点在单元格上并键入内容时,键事件被触发正常,但是当我单击相同的单元格并键入时,键事件不会被触发。所以我决定在JTable上禁用鼠标点击事件。如何禁用JTable单元格上的鼠标单击事件?
答案 0 :(得分:2)
只需在该列上设置一个不允许输入错误字符的特定单元格编辑器。可以肯定的是,我还会通过正确覆盖TableModel
验证setValueAt(Object, int, int)
中的输入:
这是一个仅允许从A到Z和a到z的字符的示例:
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class TestTable {
private static final String ALPHA_ONLY_REGEXP = "\\p{Alpha}*";
private static final String NONALPHA_REGEXP = "[^\\p{Alpha}]+";
private int ALPHA_ONLY_COLUMN_INDEX = 2;
protected String validateAndFixString(String string) {
if (!string.matches(ALPHA_ONLY_REGEXP)) {
string = string.replaceAll(NONALPHA_REGEXP, "");
}
return string;
}
protected void initUI() {
final DefaultTableModel model = new DefaultTableModel() {
@Override
public void setValueAt(Object aValue, int row, int column) {
if (column == ALPHA_ONLY_COLUMN_INDEX) {
if (aValue != null) {
aValue = validateAndFixString((String) aValue);
}
}
super.setValueAt(aValue, row, column);
}
};
for (int i = 0; i < 5; i++) {
model.addColumn("Col-" + (i + 1));
}
for (int i = 0; i < 200; i++) {
Vector<Object> row = new Vector<Object>();
for (int j = 0; j < 5; j++) {
if (j == ALPHA_ONLY_COLUMN_INDEX) {
row.add("AlphabetOnly");
}
row.add("New cell - " + (j + 1));
}
model.addRow(row);
}
JTable table = new JTable(model);
JTextField alphaOnly = new JTextField();
((PlainDocument) alphaOnly.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
string = validateAndFixString(string);
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
text = validateAndFixString(text);
super.replace(fb, offset, length, text, attrs);
}
});
table.getColumnModel().getColumn(ALPHA_ONLY_COLUMN_INDEX).setCellEditor(new DefaultCellEditor(alphaOnly));
JFrame frame = new JFrame(TestTable.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTable().initUI();
}
});
}
}
答案 1 :(得分:1)
也许禁用鼠标事件并不是一个好主意。正如你没有明确说的那样,我不知道你想要达到什么目的;你可以尝试以下一些方法:
禁用表格本身:table.disable();
使单元格不可编辑(在表模型上):
public boolean isCellEditable(int row,int col)
{
返回false;
}
如果你必须禁用鼠标事件,可能会尝试添加一个什么都不做的鼠标监听器:
table.addMouseListener(java.awt.event.MouseAdapter
{
public void mouseClicked( java.awt.event.MouseEvent e)
{
// do nothing...
}
});
希望有所帮助。
答案 2 :(得分:0)
你需要将keylistener添加到你的编辑器中。
一旦编辑,就会在该编辑器中开始关注,而不再是你的jtable。