[英]How do I split each piece of data in a text file and place them back in their respective text fields on a Java GUI form?
因此,用户将数据输入到表单上的文本字段中,然后将所有内容保存在文本文件中的单行中。 例如
铅笔,100,2,600.00
接下来,用户希望将存储在此文本文件中的内容加载回表单(在相应的字段中首先输入它们的方式)。 我不确定如何做到这一点,但我有一些代码开始。
public void loadRecord()
{
try
{
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
ArrayList<String> records = new ArrayList<String>();
String line;
while((line = br.readLine()) != null)
{
records.add(line);
}
//Goes through each line in arraylist and removes empty lines
for(int j = 0; j < records.size(); j++)
{
if(records.get(j).trim().length() == 0)
{
records.remove(j);
}
}
//Splits each record after a comma and stores each piece in separate indexes
for(int i = 0; i < records.size(); i++)
{
String[] array = records.get(i).split(",");
String name = array[0].trim();
String number = array[1].trim();
String cost = array[2].trim();
String amnt = array[3].trim();
//Display each record piece in its designated textfield
txtItem.setText(""); //temporary, this where item value would go for example
txtNumber.setText(""); //temporary
txtCost.setText(""); //temporary
txtAmount.setText(""); //temporary
}
}
catch (IOException ioe)
{
System.out.println("Something went wrong");//temporary
}
}
我想我知道该怎么做,但不知道如何准确编码:1。为每个数据片段创建一个数组(适合其特定的文本字段)2。将分割值存储在指定的数组中3 。对于设置文本字段值,循环通过适当的数组并使用数组索引值。
欢迎对我的想法进行任何调整/改进。
如果需要多行数据,请考虑使用JTextArea
而不是JTextField
。 然后,您可以只追加每一行。
while ((line = br.readLine()) != null){
textArea.append(line + "\n");
}
或者更好的是 ,要想拥有更加结构化的外观,请考虑使用JTable
。 请参见如何使用表
这是一个可以使用JTable
运行的示例
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TestTable {
String[] colNames = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
JTextArea textArea = new JTextArea(7, 30);
JButton button = new JButton("Show Table");
JFrame frame = new JFrame("Test Table");
public TestTable() {
textArea.setText(getTextForTextArea());
textArea.setEditable(false);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
TableDialog dialog = new TableDialog(frame, true);
}
});
frame.add(new JScrollPane(textArea));
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private String getTextForTextArea() {
String line = "1, 2, 3, 4, 5";
String textForArea = "";
for (int i = 0; i < 10; i++) {
textForArea += line + "\n";
}
return textForArea;
}
class TableDialog extends JDialog {
public TableDialog(final JFrame frame, boolean modal) {
super(frame, modal);
add(new JScrollPane(createTable()));
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
private JTable createTable() {
String text = textArea.getText();
DefaultTableModel model = new DefaultTableModel(colNames, 0);
Scanner scanner = new Scanner(text);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] rowData = line.split("[\\s+,]+");
model.addRow(rowData);
}
return new JTable(model);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
TestTable testTable = new TestTable();
}
});
}
}
假设文件可能包含多行,则需要更多字段
for(int i = 0; i < records.size(); i++)
{
String[] array = records.get(i).split(",");
String name = array[0].trim();
String number = array[1].trim();
String cost = array[2].trim();
String amnt = array[3].trim();
//Display each record piece in its designated textfield
JTextField txtItem = new JTextField(name);
JTextField txtNumber = new JTextField(number);
JTextField txtCost = new JTextField(cost);
JTextField txtAmount = new JTextField(amnt);
add(txtItem);
add(txtNumber);
add(txtCost);
add(txtAmount);
}
现在,这可能会让你的用户界面变得有点难看......它也会让你的数据在夜间检索和关联......
相反,你可以创建一个简单的编辑器类......
public class ItemEditor extends JPanel {
private JTextField txtItem = new JTextField();
private JTextField txtNumber = new JTextField();
private JTextField txtCost = new JTextField();
private JTextField txtAmount = new JTextField();
public ItemEditor(String name, String number, String cost, String amt) {
txtItem.setText(name)
txtNumber.setText(number)
txtCost.setText(cost)
txtAmount.setText(amt)
add(txtItem);
add(txtNumber);
add(txtCost);
add(txtAmount);
}
// Getters to get the value of each text field...
}
然后你可以使用类似List
东西来维护编辑器的每个实例,从而更容易从中获取所需的所有信息......
// Declared previously...
private List<ItemEditor> editors;
//...
editors = new ArrayList<>(records.size());
for(int i = 0; i < records.size(); i++)
{
String[] array = records.get(i).split(",");
String name = array[0].trim();
String number = array[1].trim();
String cost = array[2].trim();
String amnt = array[3].trim();
ItemEditor editor = new ItemEditor(name, number, cost, amnt);
editors.add(editor);
add(editor);
}
现在有些人在玩GridBagLayout
或GridLayout
这样的布局,你应该能够得到一些GridLayout
东西......
或者您可以使用专为此任务设计的JTable
尝试更改您的代码,如::
txtItem.setText(name);
txtNumber.setText(number);
txtCost.setText(cost);
txtAmount.setText(amnt);
当您通过setText("");
将文本框设置为null时setText("");
你需要在setText(name);
提供要在文本框中设置的变量setText(name);
为了调试目的,您可以通过系统输出来查看代码中获取的值,如::
System.out.println("Name =" + name);
System.out.println("Quantity = " + number);
System.out.println("Cost = " + cost);
System.out.println("Amount = " + amnt);
嗯,我希望我理解这个问题,但基本上,假装我有4个字段:名称,数字,成本,金额,你希望它保存到本地某处的文本文件中,所以当你打开它时,它们会显示在文本字段中?
为什么你实际上没有在代码中设置名称? 像txtItem.setText(“”); 等等?
假设您始终知道文件中保存值的顺序,您可以将值加载到字符串变量中,然后使用JTextField.setText('put string variable here')。
由于您将所有值存储在一行上,因此您只能读取一行(假设您没有保存多行)。 然后,您需要将包含“,”所需的所有值的一个大字符串拆分。
编辑你需要改变它
String line;
while((line = br.readLine()) != null){
records.add(line);
}
对此
String line = br.readLine();
String[] output = line.split(", ");
然后为每个文本字段
JTextField.setText(output[i]) //where output[i] is the proper string for the the text field
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.