我需要帮助才能使文件中的数据从JTable中出现。是的,在我提出问题之前,我已经搜索了答案,不幸的是,我的问题仍然存在。 以下是一个示例代码,供您查看发生的情况:
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Inventory extends JFrame {
public JLabel jLabel2 = new JLabel();
public JLabel jlabel3 = new JLabel();
public static int i = 0;
public static void main(String[] args) {
Inventory invent = new Inventory();
}//main
public Inventory() {
//super("Inventory");
getContentPane().setBackground(Color.WHITE);
getContentPane().setLayout(null);
setSize(400, 350);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2,
dim.height/2-this.getSize().height/2);
jLabel2.setFont(new Font("Tahoma", 0, 24)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 0, 153));
jLabel2.setText("Our Products:");
getContentPane().add(jLabel2);
jLabel2.setBounds(120, 10, 240, 20);
String[] inventoryHeading = {"Item ID", "Item Name",
"Item Description", "Item Price", "Item Stock"};
String inventoryArr[][] = new String[100][5];
try {
File items = new File("inventory.dat");
Scanner itemScanner = new Scanner(items);
while(itemScanner.hasNext()) {
String line = itemScanner.nextLine();
String[] inventoryFile = line.split("; ");
String itemID = inventoryFile[0];
String itemName = inventoryFile[1];
String itemDescr = inventoryFile[2];
String itemPrice = inventoryFile[3];
String itemStock = inventoryFile[4];
for(int col = 0; col > 5; col++) {
inventoryArr[i][col] = itemID;
col = col+1;
inventoryArr[i][col] = itemName;
col = col+1;
inventoryArr[i][col] = itemDescr;
col = col+1;
inventoryArr[i][col] = itemPrice;
col = col+1;
inventoryArr[i][col] = itemStock;
i++;
}//for
}//while
}//try
catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null, "Error! File: inventory.dat not found!",
"Error", JOptionPane.ERROR_MESSAGE);
}//catch
JTable table = new JTable(inventoryArr, inventoryHeading);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollTable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollTable.setViewportView(table);
table.setFillsViewportHeight(true);
jlabel3.add(scrollTable);
getContentPane().add(jlabel3);
getContentPane().add(scrollTable);
scrollTable.setBounds(0, 40, 400, 260);
jlabel3.setBounds(0, 40, 400, 260);
scrollTable.setVisible(true);
table.setVisible(true);
}//Inventory()
}//inventory class
我希望有人可以提供帮助。谢谢。
答案 0 :(得分:2)
(没有考验但似乎有问题)
i++;
,你在for
循环中有它。这意味着您为每个放置值的列单元格增加了行。您可能希望移动i++
循环的for
一侧,以便每个行 i
(行)增加(但是再次,您的整个for循环功能失效 - 最好摆脱它)
while(itemScanner.hasNext())
。我会使用hasNextLine()
除非你确定100行,否则我会使用更动态的东西。为此,我们需要处理TableModel
,在使用表格时,您应该这样做。例如,使用DefaultTableModel
DefaultTableModel model = new DefaultTableModel(inventoryHeading, 0);
while (itemScanner.hasNextLine()) {
String line = lineScanner.nextLine().trim();
// split and apply varaibles and values
Object[] row = {itemId, itemName, itemDesc, itemPrice, itemStock};
...
model.addRow(row);
}
JTable table = new JTable(model);
答案 1 :(得分:1)
您的问题出在for
循环中 - 它永远不会被执行。具体做法是:
for (int col = 0; col > 5; col++)
这很糟糕,因为您要求col
大于 5,而不是小于 5。所以,改变那个标志。
其次,你不应该在col
循环中增加for
- 循环已经为你做了。
最后,不要在i
循环的每次迭代中递增for
,在for循环之外这样做。