您好我正在将文本文件读入JTable
,但这是我的JTable现在的样子:
如何正确格式化并允许用户编辑JTable
?
我的文本文件:文件名(people.txt)
COLUMN_NAME COLUMN_TYPE IS_NULLABLE COLUMN_KEY COLUMN_DEFAULT EXTRA
Names VARCHAR(500) NO
Address VARCHAR(500) NO
到目前为止的编码:
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class stackq extends AbstractTableModel {
Vector data;
Vector columns;
public stackq() {
String line;
data = new Vector();
columns = new Vector();
try {
FileInputStream fis = new FileInputStream("D:/joy/text/people.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens())
columns.addElement(st1.nextToken());
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return columns.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
public String getColumnName(int i){
return (String)columns.get(i);
}
public static void main(String s[]) {
stackq model = new stackq();
JTable table = new JTable();
table.setModel(model);
JScrollPane scrollpane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(scrollpane);
JFrame frame = new JFrame();
frame.add(panel, "Center");
frame.pack();
frame.setVisible(true);
}
}
非常感谢你。
答案 0 :(得分:1)
public Stackq() {
String line;
data = new Vector();
columns = new Vector();
int count = 0;
try {
FileInputStream fis = new FileInputStream("D:\\1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens()) {
columns.addElement(st1.nextToken());
count++;
}
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
for (int i = 0; i < count; i++) {
if (st2.hasMoreTokens())
data.addElement(st2.nextToken());
else
data.addElement("");
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
只需为标头计数添加一个计数变量,并将空字符串添加到tokenizer返回null的数据向量中。
答案 1 :(得分:0)
为什么不使用DefaultTableModel
来表示表格中的数据?它将为您处理所有与Swing相关的内容,您可以分别使用addColumn(Object)
和addRow(Object[])
轻松地向其中添加列或行。
首先,您将创建一个DefaultTableModel
对象。
然后,您只需填写Vector columns
model
。
model.addColumn(st1.nextToken()); // (for adding a column)
或
Vector row = new Vector();
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens()) {
row.add(st2.nextToken());
}
result.addRow(row); // for adding a row
答案 2 :(得分:0)
尝试此操作如果您正在使用NetBean,您可以将List of Object绑定到表,并使用相应值 empMList 将Bind列绑定到Employee列表。
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, empMList, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${address}"));
columnBinding.setColumnName("Address");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
columnBinding.setColumnName("Employee Name");
columnBinding.setColumnClass(String.class);
columnBinding.setEditable(false);
bindingGroup.addBinding(jTableBinding);