我需要从.txt文件中读取的日期并将其放入GUI中的JTable中,然后修改一些记录或删除一些行并将新数据保存到该文件中。我可以阅读并显示JTable,但如何使用GUI编辑日期或删除JTable中的某些行,然后将其保存到文件中?
但是如何编辑
您需要重写返回的isCellEditable(...)方法。DefaultTableModel``true
isCellEditable(...)
DefaultTableModel``true
使用GUI删除JTable中的某些行
您需要知道选择了哪一行,然后使用的removeRow(...)方法DefaultTableModel删除该行。
removeRow(...)
DefaultTableModel
例如:
import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.swing.*; import javax.swing.table.*; public class ItemDeletion extends JPanel { private JList<String> list; private JTable table; public ItemDeletion() { setLayout( new BorderLayout(5, 5) ); String[] items = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }; // Add the list DefaultListModel<String> listModel = new DefaultListModel<String>(); for (String item: items) listModel.addElement( item ); list = new JList<String>( listModel ); JButton listDelete = new JButton( "Delete From List" ); listDelete.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DefaultListModel model = (DefaultListModel)list.getModel(); int row = list.getSelectedIndex(); while (row != -1) { model.removeElementAt( row ); row = list.getSelectedIndex(); } } }); JPanel listPanel = new JPanel( new BorderLayout(5, 5) ); listPanel.add(new JScrollPane( list ), BorderLayout.CENTER); listPanel.add(listDelete, BorderLayout.PAGE_END); // Add the table DefaultTableModel tableModel = new DefaultTableModel(0, 1); List<String> tableItems = Arrays.asList( items ); Collections.shuffle( tableItems ); for (String item: tableItems) { System.out.println( item ); tableModel.addRow( new String[]{item} ); } table = new JTable( tableModel ); table.setAutoCreateRowSorter(true); ((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0); JButton tableDelete = new JButton( "Delete From Table" ); tableDelete.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DefaultTableModel model = (DefaultTableModel)table.getModel(); int row = table.getSelectedRow(); while (row != -1) { int modelRow = table.convertRowIndexToModel( row ); model.removeRow( modelRow ); row = table.getSelectedRow(); } } }); JPanel tablePanel = new JPanel( new BorderLayout(5, 5) ); tablePanel.add(new JScrollPane( table ), BorderLayout.CENTER); tablePanel.add(tableDelete, BorderLayout.PAGE_END); add(listPanel, BorderLayout.LINE_START); add(tablePanel, BorderLayout.LINE_END); } private static void createAndShowGUI() { JFrame frame = new JFrame("Multiple Item Deletion"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new ItemDeletion(), BorderLayout.NORTH); frame.setLocationByPlatform( true ); frame.pack(); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
保存到文件?
您需要从中获取数据DefaultTableModel。您可以使用该getValueAt(...)方法检索每个值。
getValueAt(...)
因此,您需要创建类似以下的代码:
for (each row); { StringBuilder sb = new StringBuilder(); for ( each column) { Object data = model.getValueAt(...); sb.append( data.toString() ); } write the sb.toString() to the file }