gpt4 book ai didi

java - JTable - 删除行问题

转载 作者:行者123 更新时间:2023-11-29 07:10:17 25 4
gpt4 key购买 nike

我有一个表,应该从文件中读取并显示,并且可以从表和文件中删除行。

但是不行!我的代码:

public class Readuser_A extends AbstractTableModel {

String[] columns = { "Fname", "Lname", "Gender", "Date", "ID" };
ArrayList<String> Listdata = new ArrayList<String>();
String[][] Arraydata;

public Readuser_A() {
try {
FileReader fr = new FileReader("AllUserRecords.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
Listdata.add(line);
}
br.close();
Arraydata = new String[Listdata.size()][];
for (int i = 0; i < Listdata.size(); i++) {
Arraydata[i] = Listdata.get(i).split(" ");
}
} catch (IOException e) {
}
}

@Override
public String getColumnName(int colu) {
return columns[colu];

}

public int getRowCount() {
if (null != Arraydata) {
return Arraydata.length;
} else {
return 0;
}
}

public int getColumnCount() {
return columns.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
return Arraydata[rowIndex][columnIndex];
}
}

我的第二堂课:

public class ReaduserM_A extends DefaultTableModel {
final JLabel myLable = new JLabel();

public ReaduserM_A() {

final Readuser_A RU = new Readuser_A();
final JTable mytable = new JTable(RU);
final JFrame Uframe = new JFrame("All Users");
JButton DellButton = new JButton("Delete User");

DellButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (mytable.getSelectedRow() != -1) {
removeRow(mytable.getSelectedRow());
RU.fireTableRowsDeleted(mytable.getSelectedRow(),
mytable.getSelectedRow());
} else {
JOptionPane.showMessageDialog(null, "No Row Selected");
return;
}

//Now, Delete from text file too
deleteFromFile();
}

});

JPanel panel = new JPanel();
JScrollPane sp = new JScrollPane(mytable);
panel.add(sp);
panel.add(DellButton);
panel.add(myLable);
Uframe.add(panel);
Uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Uframe.setSize(570, 500);
Uframe.setLocation(300, 60);
Uframe.setVisible(true);
}

public void deleteFromFile() {
File Mf = new File("AllUserRecords.txt");
File Tf = new File("Uoutput.txt");
try {
FileReader Ufr = new FileReader(Mf);
BufferedReader Ubr = new BufferedReader(Ufr);
PrintWriter Upw = new PrintWriter(new FileWriter(Tf));
String Us;
while ((Us = Ubr.readLine()) != null) {
String[] Ust = Us.split(" ");
String Unumber = Ust[4];

//How find the selected row line by it's ID and delete that row?
}
Upw.close();
Ubr.close();
Mf.delete();
Tf.renameTo(Mf);

} catch (FileNotFoundException e1) {
myLable.setText("File Not Found");
} catch (IOException ioe) {
myLable.setText("IO Error");
ioe.printStackTrace();
}
}

public static void main(String[] args) {
new ReaduserM_A();
}
}

发生此异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 0
at java.util.Vector.removeElementAt(Vector.java:554)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
at Array.ReaduserM_A$1.actionPerformed(ReaduserM_A.java:33)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我认为在第一行中,ArrayIndexOutOfBoundsException: 6 >= 0表示我选择的表格行(我选择了第六行并单击了删除按钮)你能帮我更多吗?

谢谢!

最佳答案

  1. 学习 Java 命名约定并遵守它们
  2. 永远不要默默地忽略异常
  3. 您的代码没有意义:您正在从 ReaduserM_A 类中删除一个元素,但您的 JTable 使用另一个表模型,即 Readuser_A .您混淆了两种不同的模型。我认为您实际上不应该在 ReaduserM_A
  4. 中扩展 DefaultTableModel
  5. DefaultTableModel 中,您不需要手动触发事件。对于所有默认的 API 方法,它是自动完成的。

关于java - JTable - 删除行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583939/

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