我有一个从arraylist更新的JTable。在GUI中,我有ADD,DELETE,MODIFY按钮。当我选择一行时,我想让我能够按下MODIFY按钮并在提示输入新值时
我已经覆盖了setValueAt和isCellEditable方法,但没有任何反应,值保持不变。
以下是可编译的测试示例。我能做的最短的。
提前谢谢!
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
public class GUI extends javax.swing.JFrame {
Test model = new Test();
public GUI() {
initComponents();
jTable.setModel(model);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();
modify = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tfName = new javax.swing.JTextField();
tfPhone = new javax.swing.JTextField();
add = new javax.swing.JButton();
delete = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable);
modify.setText("Modify");
modify.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
modifyActionPerformed(evt);
}
});
jLabel1.setText("Name");
jLabel2.setText("Phone");
add.setText("Add");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});
delete.setText("Delete");
delete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
.addComponent(delete)
.addGap(50, 50, 50)
.addComponent(modify)
.addGap(77, 77, 77))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tfPhone))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tfPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(modify)
.addComponent(add)
.addComponent(delete))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String nrtel = JOptionPane.showInputDialog("Edit phone number");
model.setValueAt(nrtel, jTable.getSelectedRow(), 3);
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
String nume = tfName.getText();
String nrtel = tfPhone.getText();
model.adaugareContact(nume, nrtel);
}
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {
int selRow = jTable.getSelectedRow();
if (selRow != -1) {
model.stergeContact(selRow);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton add;
private javax.swing.JButton delete;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable;
private javax.swing.JButton modify;
private javax.swing.JTextField tfName;
private javax.swing.JTextField tfPhone;
// End of variables declaration
}
class Test extends AbstractTableModel{
public static final int NAME = 0;
public static final int PHONE = 1;
public static List<Contact> listaContacte = new ArrayList<Contact>();
private final String[] columns = {
"Name",
"Phone"
};
@Override
public int getRowCount() {
if (listaContacte.size() <= 0)
{
return 0;
} else {
return listaContacte.size();
}
}
@Override
public int getColumnCount() {
return columns.length;
}
@Override
public String getColumnName(int col) {
return columns[col];
}
@Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return listaContacte.get(row).getName();
} else if (col == 1) {
return listaContacte.get(row).getNrTel();
}
return "Eroare";
}
@Override
public void setValueAt(Object aValue, int rowIndex, int colIndex) {
Contact abonat = listaContacte.get(rowIndex);
switch (colIndex) {
case 0:
abonat.setName((String)aValue);
break;
case 1:
abonat.setNrTel((NrTel)aValue);
break;
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
// @Override
// //olso tried this but "Object cannot be converted to string"
// public void setValueAt(Object value, int row, int column) {
//
// Contact abonat = listaContacte.get(row);
// switch (column) {
// case NAME:
// abonat.name = value;
// break;
// case PHONE:
// abonat.phone = value;
// break;
//
// }
// updateTable(row, column);
//
// }
//
// public void updateTable(int row, int column) {
// fireTableCellUpdated(row, column);
//}
@Override
public boolean isCellEditable(int row, int colNum) {
switch (colNum) {
case 2:
return false;
default:
return true;
}
}
public void adaugareContact(String nume, String tel) {
Long s = Long.valueOf(tel);
listaContacte.add(new Contact(nume, new NrTel(s)));
fireTableDataChanged();
}
public void stergeContact(int row){
listaContacte.remove(row);
fireTableDataChanged();
}
}
class Contact {
public String Name;
public NrTel phone;
public Contact(String n, NrTel nr ) {
Name = n;
phone = nr;
}
public String getName() {
return Name;
}
public NrTel getNrTel(){
return phone;
}
public void setNrTel(NrTel nrtel) {
this.phone = nrtel;
}
public void setName(String name) {
this.Name = name;
}
}
class NrTel {
private long nrtel;
public NrTel( long nr){
nrtel = nr;
}
public long getNrTel(){
return nrtel;
}
@Override
public String toString() {
String str = ""+nrtel;
nrtel = Long.valueOf(str);
return str;
}
}
编辑:
我修改了这个方法
如果a为0,则修改第一列。如果我有1我得到一些错误
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
String nrtel = JOptionPane.showInputDialog("Edit phone number");
model.setValueAt(nrtel, jTable.getSelectedRow(), 1);
}
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to NrTel
at Test.setValueAt(GUI.java:222)
at GUI.modifyActionPerformed(GUI.java:133)
at GUI.access$000(GUI.java:6)
at GUI$1.actionPerformed(GUI.java:47)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
<强>更新强>
错误在modifyActionPerformed
方法中。问题是第1列(电话列)的编辑器需要NrTel
个对象,而不是string
,因此必须将string
解析为int
,然后喂到桌子里。
以下是一种可以解决问题的更新方法:
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("HERE");
String tel = JOptionPane.showInputDialog("Edit phone number");
NrTel nrtel = new NrTel(Integer.parseInt(tel));
jTable.getModel().setValueAt(nrtel, 0, 1);
}
旧答案:
我认为错误来自modifyActionPerformed
方法中的第134行。
这是你的代码:
model.setValueAt(nrtel, jTable.getSelectedRow(), 3);
这是更新的代码:
jTable.getModel().setValueAt(nrtel, jTable.getSelectedRow(), 0);
如果模型对象与jTable具有相同的引用号,则第一部分jTable.getModel()
不是必需的,但使用访问器方法可能是个好主意。
第二部分与列的编号有关。这些列设置为数组,从0开始。由于只有2列,我认为第三个参数可以介于0和1之间。
这有帮助吗?
答案 1 :(得分:0)
打电话的时候 model.setValueAt(nrtel,jTable.getSelectedRow(),3);
第136行
没有案例&#39; 3&#39;在setValueAt()方法的开关中,所以没有任何更新
我尝试更改为model.setValueAt(nrtel, jTable.getSelectedRow(), 1);
以便
abonat.setNrTel((NrTel)aValue);
,但是无法将aValue强制转换为输入NrTel ...
猜猜你应该改为abonat.setNrTel(new NrTel(aValue))