gpt4 book ai didi

java - 从 txt 文件获取数据并将其添加到 JTable 时,如何对哪些数据进入哪列或行进行排序

转载 作者:行者123 更新时间:2023-12-01 11:05:00 25 4
gpt4 key购买 nike

对于我的任务,我被告知要制作一个可以跟踪契约(Contract)的应用程序。我的 JTable 有 5 行:ID、契约(Contract)名称、描述、截止日期和联系人。应用程序中的其他一切都工作正常,例如将数据添加到 JTable 并将该数据保存到 txt 文件,但问题是打开应用程序并读取 txt 文件中的数据并将其加载到 JTable。

问题是,当我单击“加载”按钮时,所有数据都会放入一列中,且未排序。如何以某种方式对其进行排序,以便 ID 进入 id 列,描述进入描述列,等等。

这是代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;


public class Main {

// Set up the size of the GUI window
private static final int FRAME_WIDTH = 900;
private static final int FRAME_HEIGHT = 400;
static JTable contracts = new JTable();
static int ID = 0;
static String line;

public static void main(String[] args) {

// Set up the user interface
final JFrame frame = new JFrame();
final JPanel buttonPanel = new JPanel();
frame.add(buttonPanel);
// I need this to be able to put the buttons where I want
buttonPanel.setLayout(null);

// Set up Add button and its location
final JButton buttonAdd = new JButton(" Add ");
buttonAdd.setBounds(50, 325, 100, 20);
buttonPanel.add(buttonAdd);


// Set up Exit button and its location
final JButton buttonExit = new JButton("Exit");
buttonExit.setBounds(200, 325, 100, 20);
buttonPanel.add(buttonExit);

// Method for exit button
buttonExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

System.exit(0);
}
});

// Set up Save button and its location
final JButton buttonSave = new JButton("Save");
buttonSave.setBounds(350, 325, 100, 20);
buttonPanel.add(buttonSave);

// Set up Save button method
buttonSave.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent e) {
try{

System.out.println("This works!");

BufferedWriter bfw = new BufferedWriter(new FileWriter("Contract_data.txt"));

for (int i = 0 ; i < contracts.getRowCount(); i++) {



for(int j = 0 ; j < contracts.getColumnCount();j++)
{

bfw.newLine();
bfw.write((String)(contracts.getValueAt(i,j)));
bfw.write("\t");;
}
}


bfw.close();
}catch(Exception ex) {

ex.printStackTrace();
}

}
});
// Set up Load button and its location
final JButton buttonLoad = new JButton("Load");
buttonLoad.setBounds(500, 325, 100, 20);
buttonPanel.add(buttonLoad);


// Set up Labels for contracts, description, etc...
final JLabel lblname = new JLabel("Enter contract name: ");
lblname.setBounds(20, 5, 150, 100);
buttonPanel.add(lblname);

final JLabel lbldesc = new JLabel("Enter description: ");
lbldesc.setBounds(20, 60, 150, 100);
buttonPanel.add(lbldesc);

final JLabel lbldeadline = new JLabel("Enter deadline: ");
lbldeadline.setBounds(20, 115, 150, 100);
buttonPanel.add(lbldeadline);

final JLabel lblcontact = new JLabel("Enter contact(s):");
lblcontact.setBounds(20, 170, 150, 100);
buttonPanel.add(lblcontact);


// Set up textboxes for all expected inputs
final JTextField txtname = new JTextField();
txtname.setBounds(180, 40, 100, 25);
buttonPanel.add(txtname);

final JTextField txtdesc = new JTextField();
txtdesc.setBounds(180, 95, 100, 25);
buttonPanel.add(txtdesc);

final JTextField txtdeadline = new JTextField();
txtdeadline.setBounds(180, 150, 100, 25);
buttonPanel.add(txtdeadline);

final JTextField txtcontact = new JTextField();
txtcontact.setBounds(180, 210, 100, 25);
buttonPanel.add(txtcontact);


// Set up of columns in the table
String[] columns = { "ID", "Contract", "Description", "Deadline", "Contact(s)" };
// Set up of the table with the appropriate column headers
final DefaultTableModel model = new DefaultTableModel(null, columns);

contracts.setModel(model);
JScrollPane scrollPane = new JScrollPane(contracts);
scrollPane.setBounds(300, 20, 550, 300);
buttonPanel.add(scrollPane);


// Save button methods, including validation checking
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (txtname.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Error: no contract name");
return;
}

if (txtdesc.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Error: no contract description");
return;
}


// Add an ID number to each entry and add the entry to the table
ID++;
model.addRow(new Object[] { String.valueOf(ID),
txtname.getText(), txtdesc.getText(),
txtdeadline.getText(), txtcontact.getText() });


// Once entry is added to the table, the text fields are cleared for the next entry
txtname.setText("");
txtdesc.setText("");
txtdeadline.setText("");
txtcontact.setText("");



}
});
// Method for load button
buttonLoad.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

BufferedReader reader;
try{

reader = new BufferedReader(new FileReader("Contract_data.txt"));
while((line = reader.readLine()) != null)
{
model.addRow(new Object[] {line});

}
reader.close();
}
catch(Exception ex){

ex.printStackTrace();

}

}
});
/*
* This sets the size of the window along with the title and it sets up
* the exit on close X button to close the window when the X is clicked.
*/

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Contract Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

请帮忙。我非常感谢并提供反馈。

最佳答案

when I click on the 'load' button, all of the data gets put into one column,

查看“添加”按钮的代码。您向数组添加 5 条数据。

查看“加载”按钮的代码。您添加1条数据。

How can I sort it in a way so that the ID goes into the id column, description goes into the description column

您似乎正在尝试使用制表符作为分隔符来存储数据。如果是这种情况那么当你读取一行数据时,你需要将该行分割成5条数据。您可以使用以下方法来做到这一点:

//model.addRow(new Object[] {line});
String[] row = line.split("\t");
model.addRow( row );

关于java - 从 txt 文件获取数据并将其添加到 JTable 时,如何对哪些数据进入哪列或行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047289/

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