1

嗨,我正在将一个文本文件读入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);
         }
      }

太感谢了。

4

3 回答 3

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();
}
}

只需为标头计数添加一个计数变量,并将空字符串添加到标记器返回 null 的数据向量中。

于 2013-04-17T06:25:04.477 回答
0

试试这个如果您使用的是 NetBean,您可以在其中将对象列表绑定到表,并使用相应的值empMList绑定列是员工列表。

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);
于 2013-04-17T06:28:19.700 回答
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
于 2013-04-17T06:19:29.957 回答