- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更改单元格值后,我的 JTable
不刷新。我可以双击单元格并更改其值,但是当我按“确定”或单击单元格外时,该值将重置为前一个值,但不会更新表格。这是我的自定义表模型的代码,我不知道如何更新数据库,因为当我更改单元格的值时,该表是从数据库中获取的。
package adisys.server.strumenti;
import java.sql.*;
import java.util.Formatter;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import adisys.server.boundary.EditorPatologie;
import adisys.server.data.Database;
import adisys.server.data.Patologie;
import adisys.server.entity.Patologia;
/**
* @author Francesco
*
*/
public class ADISysTableModel extends AbstractTableModel implements TableModel {
ResultSet dati;
public ADISysTableModel(ResultSet nuoviDati)
{
dati=nuoviDati;
try {
//Trace
System.out.println("- Creazione modello tabella: \""+ dati.getMetaData().getTableName(1) +"\"");
} catch (SQLException e1) {
e1.printStackTrace();
}
}
@Override
public int getColumnCount() {
try {
return dati.getMetaData().getColumnCount();
}
catch (SQLException e) {
e.printStackTrace();
System.out.println("ERRORE: Calcolo del numero di colonne errato.");
return 0;
}
}
@Override
public int getRowCount() {
try {
//Seleziona l'ultimo elemento
dati.last();
//Restituisce l'indice dell'elemento
return (dati.getRow());
} catch (SQLException e) {
e.printStackTrace();
System.out.println("ERRORE: Calcolo del numero di righe errato. (metodo getRowCount() )");
return 0;
}
}
@Override
public Object getValueAt(int riga, int colonna) {
try {
//Sposta il cursore alla riga desiderata (con sfasamento di 1)
dati.absolute(riga+1);
//Estrae il valore nella colonna specificata e lo restituisce (con sfasamento di 1)
return dati.getObject(colonna+1);
} catch (SQLException e) {
// In caso di errore restituisce un oggetto vuoto
e.printStackTrace();
//Trace
System.out.println("ERRORE: Valore dell'elemento della tabella non valido.");
return null;
}
}
@Override
public boolean isCellEditable(int rIndex, int cIndex)
{
return true;
}
@Override
public String getColumnName(int col) {
try {
return dati.getMetaData().getColumnName(col+1);
} catch (SQLException e) {
// Eccezione
e.printStackTrace();
return "?";
}
}
public Integer getID(int riga)
{
//Ricerca colonna ID
for(int i=0; i<=getColumnCount(); i++)
if(getColumnName(i).equals("ID"))
return i;
return null;
}
/**
* Restituisce l'indice della colonna a partire dal nome della colonna ricercata
* <b>N.B. L'indice della prima colonna è 0, l'ultimo è numeroColonne-1.</b>
* @param Nome - Stringa con il nome della colonna
* @return -1 se la colonna non e' stata trovata, altrimenti l'indice della colonna
*/
public int getColumnIndex(String Nome)
{
for (int i=0; i<getColumnCount();i++)
if( getColumnName(i)==Nome) return i;
return -1;
}
}
如果您需要其他东西,例如数据库或表编辑器的代码,请告诉我;)感谢您的帮助:)
--编辑--好吧,我已经实现了 setValueAt 方法,但是当我单击单元格时,它给了我这样的错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at adisys.server.strumenti.ADISysTableModel.setValueAt(ADISysTableModel.java:149)
at javax.swing.JTable.setValueAt(Unknown Source)
at javax.swing.JTable.editingStopped(Unknown Source)
at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source)
at javax.swing.JTable$GenericEditor.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(Unknown Source)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我的 setValueAt 方法是这样的:
Vector<Paziente> content;
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex)
{
Paziente row = this.content.elementAt(rowIndex);
String strValue = (String)value;
int IDValue = (int)value;
if(columnIndex == 0)
{
row.setID(IDValue);
}
else if(columnIndex == 1)
{
row.setNome(strValue);
}
else if(columnIndex == 2)
{
row.setCognome(strValue);
}
fireTableCellUpdated(rowIndex, columnIndex);
}
请您再次帮我解决这个问题吗? :) 我对这个功能感到非常着迷,我知道对于很多人来说这并不困难,但对我来说却是......感谢您的回答,祝您有美好的一天! :D
更新:我已经用这段代码解决了这个问题:[代码]@覆盖 公共(public)无效setValueAt(对象值,int rowIndex,int columnIndex) {字符串aValue =(字符串)值; 字符串格式 = “更新 PAZIENTI SET NOME =” + “'” + aValue + “'” + “WHERE ID =” + rowIndex + “;”; Database.esegui(formatoIstruzione.toUpperCase()); Pianificatore.agiornaTabelle(); }}[/代码]
换句话说,我启动 sql 指令来更新表,然后刷新它的 View ,以便它几乎显示新值。谢谢大家的帮助和给我的灵感,我们终于成功了! *_*
最佳答案
我在您的 TableModel
中没有看到 setValueAt()
的任何实现。如果没有它,你怎么能期望这些事情发生呢?尝试实现该方法,看看是否做得更好。
更新:
ADISysTableModel 中的第 149 行有一个空引用:
at adisys.server.strumenti.ADISysTableModel.setValueAt(ADISysTableModel.java:149)
打开文本编辑器,转到该行号,然后检查对象引用。在调试器中运行以查看哪一个为空。修复起来很容易。
看到这一行了吗?
Vector<Paziente> content;
这会将引用设置为 null,因为您没有调用 new 将其设置为新引用。
将其更改为这样,NPE 就会消失:
Vector<Paziente> content = new Vector<Paziente>();
这可能不是您想要的,但它不会为空。
我认为你应该处理底层表模型,而不是这个随机 vector 。你知道你的代码,但我认为这也不会让你高兴。
我强烈建议您在尝试真正的问题之前简化事情并阅读 JTable 教程:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
关于java - isCellEditable 和数据库不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310580/
我想要的是能够在输入获得焦点或失去焦点时执行某些操作(两个事件)。 我尝试了以下方法,但这按事件单独工作(单独编码时):仅在焦点上,或仅在失去焦点时。 另外,我希望它尽可能跨平台(包括触摸设备),这是
我分别研究了TableView的Filtering和Pagination。 过滤: this帖子帮助我满足了我的需要 分页: this , this帖子也帮助了我 我想像这样将它们组合在一起: 详情-
我是 TDD 方法的新手,所以我想知道是否有人经历过这种机智可以启发我一点。我想获得一些关于如何一起使用 UML 和 TDD 方法的线索。 我已经习惯了:用 UML 设计 --> 生成骨架类(然后保持
我尝试使用入口点和 cmd 设置 Docker。 FROM debian:stretch RUN apt-get update && \ apt install gnupg ca-certificat
我想要一个 Class 对象,但我想强制它所代表的任何类扩展类 A 并实现接口(interface) B。 我能做到: Class 或者: Class 但我不能两者兼得。有办法做到这一点吗? 最佳答案
我是 Rubymine 的长期用户。 Rubymine 非常适合基于 html 的 Rails 应用程序,但我现在正在做更多的 SPA 客户端工作(例如 javascript/react)。我发现我真
我注意到我使用的某个脚本依赖于原型(prototype)。 (Lightbox 2) 它会与 jQuery 在同一页面上一起工作吗?有没有办法确保它们不冲突? 最佳答案 可以,但你需要采取 speci
我需要对表中显示的数据进行分页并通过 ajax 调用获取它 - 这是我通过使用具有以下配置的 dataTables 插件来完成的 - bServerSide : true; sAjaxSource :
我是 gtk 新手,所以想知道在 C 语言中归档和 gtk 是否可以一起使用?例如,我可以从 .txt 文件中读取,然后在相同的代码中使用 gtk 在标签或其他内容中显示它吗?如果是,怎么办? 谢谢!
有没有人设法得到Bck2Brwsr最近与 Java 8/JavaFX 8 一起工作?有没有兼容的机会?我找不到太多关于它的信息,也没有一个好的起点。使用给定的 Maven archetype我遇到了几
在我的应用程序中,用户通过 openid(与 stackoverflow 相同)登录/注销。 我想通过 oauth 向第三方应用程序开放我的应用程序。 如何创建我的 openid-consumer 应
我在启动和运行 Hibernate 和 Spring 时遇到一些问题。我有一个网络服务器项目,它使用了其他几个具有持久实体的项目。我遇到的问题是,对于存储在 WEB-INF/libs 内的另一个 ja
我有 @ControllerAdvice 类,它处理一组异常。我们还有一些其他异常,这些异常用 @ResponseStatus 注释进行注释。为了结合这两种方法,我们使用博客文章中描述的技术:http
我想在屏幕上使用进度条而不是 progressDialog。 我在我的 XML View 文件中插入了一个进度条,我想让它在加载时显示并在不加载时禁用它。 所以我使用的是可见的,但它发生了,所以其余的
CREATE TABLE `users` ( `id` int(11) AUTO_INCREMENT, `academicdegree` varchar(255),
IN() 中使用的查询返回:1, 2。然而,整个查询返回 0 行,这是不可能的,因为它们存在。我在这里做错了什么? SELECT DISTINCT li.auto_id FROM links
亲们, 我如何在使用 Jade 生成的表单上实现 jQuery 样式?我想做的是美化 表单并使它们可点击。我在 UI 方面很糟糕。期间。 我如何在表单上实现这个可选择的方法? http://jquer
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我可以: auto o1 = new Content; 但不能: std::shared_ptr o1(new Content); std::unique_ptr o1(new Content); 我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!