问题
Hi i am reading a text file into JTable, but here is what my JTable Looks Like Now:
How can I format it correctly and allow JTable to be editable by users?
My Text File: File Name(people.txt)
COLUMN_NAME COLUMN_TYPE IS_NULLABLE COLUMN_KEY COLUMN_DEFAULT EXTRA
Names VARCHAR(500) NO
Address VARCHAR(500) NO
Coding so far:
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);
}
}
Thank You So much.
回答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();
}
}
Just add one count variable for header count, and add the empty string into the data vector where tokenizer returns null.
回答2:
Try this If you are using NetBean, where you can bind List of Object to table and Bind Column with respective value empMList is list of 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);
回答3:
Why don't you use the DefaultTableModel to represent the data in your table? It will take care of all the Swing-related stuff for you, and you can easily add columns or rows to it, using addColumn(Object) and addRow(Object[]), respectively.
First, you would create a DefaultTableModel object.
Then, instead of doing creating a Vector columns, you'd just fill the model by doing
model.addColumn(st1.nextToken()); // (for adding a column)
or
Vector row = new Vector();
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens()) {
row.add(st2.nextToken());
}
result.addRow(row); // for adding a row
来源:https://stackoverflow.com/questions/16052821/format-the-jtable-as-shown-in-text-file