我想将一些数据添加到JTable
并写入某个文件。数据未添加到表中,但会正确写入文件。
以下是我所指的代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Layout extends JFrame {
DefaultTableModel dtm;
public Layout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel(new BorderLayout());
p2 = new JPanel(new BorderLayout());
p3 = new JPanel(new GridLayout(4, 2));
p4 = new JPanel(new FlowLayout());
p5 = new JPanel(new BorderLayout());
l1 = new JLabel("Item");
l2 = new JLabel("Price");
l3 = new JLabel("Quantity");
l4 = new JLabel("ReorderLevel");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
p3.add(l1);
p3.add(tf1);
p3.add(l2);
p3.add(tf2);
p3.add(l3);
p3.add(tf3);
p3.add(l4);
p3.add(tf4);
b1 = new JButton("Add");
b2 = new JButton("Update");
b3 = new JButton("Delete");
b4 = new JButton("List");
p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
t = new JTable();
t.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
//{null, null, null, null},
//{null, null, null, null},
//{null, null, null, null}
},
new String[]{
"Item", "Price", "Quantity", "Reorder"
}
));
sc = new JScrollPane(t);
p5.add(sc, BorderLayout.CENTER);
p2.add(p3, BorderLayout.NORTH);
p2.add(p4, BorderLayout.SOUTH);
p1.add(p2, BorderLayout.NORTH);
p1.add(p5, BorderLayout.CENTER);
add(p1);
pack();
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnaddActionPerformed(evt);
}
}
);
} `add button action`
private void btnaddActionPerformed(java.awt.event.ActionEvent evt)
{
String item = tf1.getText();
String price = tf2.getText();
String quantity=tf3.getText();
String reorder=tf4.getText();
try {
String filename= "C:\\WINNT\\PROFILES\\palanisamyp\\workspace\\InventoryDBInfile\\Myfile.txt";
FileWriter myfile = new FileWriter(filename,true);
PrintWriter outStream = new PrintWriter(myfile);
outStream.print(item);
outStream.print("|");
outStream.print(price);
outStream.print("|");
outStream.print(quantity);
outStream.print("|");
outStream.print(reorder);
outStream.print("\r\n");
outStream.close();
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
myfile.close();
readFile();
} catch (Exception e) {
e.printStackTrace();}
}
private void readFile()
{
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\WINNT\\PROFILES\\palanisamyp\\workspace\\InventoryDBInfile\\Myfile.txt"));
if (dtm.getRowCount() > 0) {
for (int i = dtm.getRowCount() - 1; i > -1; i--) {
dtm.removeRow(i);
}
}
while ((sCurrentLine = br.readLine()) != null) {
String item1=sCurrentLine.toString();
String regexp = "[|]+";
String[] lines = item1.split(regexp);
Vector v = new Vector();
String item2;
String price2;
String quantity2;
String reorder2;
item2=lines[0].toString();
price2=lines[1].toString();
quantity2=lines[2].toString();
reorder2=lines[3].toString();
v.add(item2);
v.add(price2);
v.add(quantity2);
v.add(reorder2);
dtm.toString();
dtm.addRow(v);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Layout().setVisible(true);
}
private JPanel p1, p2, p3, p4, p5;
private JLabel l1, l2, l3, l4;
private JTextField tf1, tf2, tf3, tf4;
private JButton b1, b2, b3, b4;
private JScrollPane sc;
private JTable t;
}
答案 0 :(得分:0)
请检查Oracle Docs以了解如何在Java表中使用模型,渲染器和编辑器。
答案 1 :(得分:0)
因为您将表的dateModel分配给新的DefaultTableModel
t.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
//{null, null, null, null},
//{null, null, null, null},
//{null, null, null, null}
},
new String[]{
"Item", "Price", "Quantity", "Reorder"
}
));
而不是使用您的字段dtm
DefaultTableModel dtm;
试试这个:
dtm = new javax.swing.table.DefaultTableModel(
new Object[][]{
//{null, null, null, null},
//{null, null, null, null},
//{null, null, null, null}
},
new String[]{
"Item", "Price", "Quantity", "Reorder"
}
));
t.setModel(dtm);