Java 如何在JTable的行中设置文本?
我试图用JavaJFrame制作一个带有表的程序。 我想保存行中的文本。 我把文本放在一个文本文件中,这是有效的 但是我想把文本文件中的文本放到表中。 我试过很多东西,但都不管用。Java 如何在JTable的行中设置文本?,java,swing,jtable,Java,Swing,Jtable,我试图用JavaJFrame制作一个带有表的程序。 我想保存行中的文本。 我把文本放在一个文本文件中,这是有效的 但是我想把文本文件中的文本放到表中。 我试过很多东西,但都不管用。 有人能帮忙吗?如果您将文本逐行存储在文件中,例如: tester.txt: Star Wars Star Trek The Lord of The Rings 然后,您可以逐行阅读,当您阅读了足够多的行后,向表中添加一行。为了向现有表中添加一行,我相信您确实需要使用模型,或者如果是从新创建的,则需要事先准备数据,然
有人能帮忙吗?如果您将文本逐行存储在文件中,例如: tester.txt:
Star Wars
Star Trek
The Lord of The Rings
然后,您可以逐行阅读,当您阅读了足够多的行后,向表中添加一行。为了向现有表中添加一行,我相信您确实需要使用模型,或者如果是从新创建的,则需要事先准备数据,然后创建。下面是使用上述txt文件的粗略示例:
public class SO {
public static void main(String[] args) {
//Desktop
Path path = Paths.get(System.getProperty("user.home"), "Desktop", "tester.txt");
//Reader
try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))){
Vector<String> row = new Vector<String>();
//Add lines of file
int numOfCellsInRow = 3; //Num of cells we want
int count = 0;
while (count < numOfCellsInRow){
row.addElement(reader.readLine());
count++;
}
//Column names
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Column One");
columnNames.addElement("Column Two");
columnNames.addElement("Column Three");
Vector<Vector<String>> rowData = new Vector<Vector<String>>();
rowData.addElement(row);
//Make table
JTable table = new JTable(rowData, columnNames);
//How you could add another row by drawing more text from the file,
//here I have just used Strings
//Source: http://stackoverflow.com/questions/3549206/how-to-add-row-in-jtable
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Darth Vader", "Khan", "Sauron"});
//Make JFrame and add table to it, then display JFrame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后,通过单击“保存”按钮,更改您的侦听器方法,以获取基于您创建的文件的文本输入:
btnGetFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//This prevents exception
if(!Files.exists(path)){//If no file
JOptionPane.showMessageDialog(null, "File does not exist!");//MSg
return;//End method
}
/*changed this bit so that it reads the data and
* should then add the rows
*/
try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))){
String line;
while((line = reader.readLine()) != null){//Chek for data, reafLine gets first line
Vector<String> row = new Vector<String>();//New Row
row.addElement(line);//Add first line
int numOfCellsInRow = 3; //Num of cells we want
int count = 0;
//We only want 2 because we got the first element at loop start
while (count < (numOfCellsInRow - 1)){
row.addElement(reader.readLine());
count++;
}
model.addRow(row);//Add rows from file
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnGetFile.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
//这可以防止异常
如果(!Files.exists(path)){//如果没有文件
JOptionPane.showMessageDialog(null,“文件不存在!”);//MSg
return;//结束方法
}
/*更改此位,以便它读取数据并
*然后应该添加行
*/
try(BufferedReader=new BufferedReader(new FileReader(path.toFile())){
弦线;
当((line=reader.readLine())!=null){//Chek查找数据时,reafLine得到第一行
向量行=新向量();//新行
row.addElement(行);//添加第一行
int numOfCellsInRow=3;//需要的单元格数
整数计数=0;
//我们只需要2,因为我们在循环开始时得到了第一个元素
而(计数<(numOfCellsInRow-1)){
row.addElement(reader.readLine());
计数++;
}
model.addRow(row);//从文件中添加行
}
}捕获(IOE1异常){
e1.printStackTrace();
}
}
});
添加注释,尝试解释正在发生的事情
它通过将文件中的行添加到JTable来为我的工作。希望它现在也能为你工作
我已经尝试了很多东西
在一个具体的例子中告诉我们到目前为止您尝试了什么。因为如果不是的话,这个问题太宽泛了,很快就要结束了。你能展示一下你的尝试吗?把文本文件中的文本转换成任何东西都是一项复杂的任务,在确定将文本读入JTable的最佳方式之前,可能需要有关文本如何存储在文本文件中的信息。您可能需要查看DefaultTableModel中的“有人可以帮助吗?”帮助我们帮助您!发布一些代码,这样我们就可以看到你哪里出错了。如果你拒绝,那就看一看
btnGetFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//This prevents exception
if(!Files.exists(path)){//If no file
JOptionPane.showMessageDialog(null, "File does not exist!");//MSg
return;//End method
}
/*changed this bit so that it reads the data and
* should then add the rows
*/
try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))){
String line;
while((line = reader.readLine()) != null){//Chek for data, reafLine gets first line
Vector<String> row = new Vector<String>();//New Row
row.addElement(line);//Add first line
int numOfCellsInRow = 3; //Num of cells we want
int count = 0;
//We only want 2 because we got the first element at loop start
while (count < (numOfCellsInRow - 1)){
row.addElement(reader.readLine());
count++;
}
model.addRow(row);//Add rows from file
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});