Mình cần hỗ trợ thêm dữ liệu vào Table !

nguyenon2202

New Member
13/7/20
2
0
1
bac giang
Mình đang làm báo cáo đọc ghi dữ liệu ra file Text và đang mắc ở phần đọc file để hiển thị lên Table:(
try{
FileInputStream f = new FileInputStream("quanlinhanvien.txt");
Scanner scanner = new Scanner(f);
Vector data1=new Vector();
for(int i=0;scanner.nextLine()!=null;i++)
{
data1.add(scanner.next());
data1.add(scanner.next());
data1.add(scanner.next());
data1.add(scanner.next());
data1.add(scanner.next());
data1.add(scanner.next());
data.add(data1);
data1.clear();
}
scanner.close();}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Đọc file thất bại");}

file text co dữ liệu như sau:
NV01 Hung Nam BắcGiang 09xx 1tr
NV02 Nguyễn Nữ HàNội 09xx 2tr
NV03 Giang Nam CàMau 01xx 1.5tr
NV04 Nam Nam BắcNinh 03xx 5tr

data là vector để mình thêm vào Table.
Ai có cách làm khác không chỉ mình với :( mình đang muốn đọc dữ liệu từ file text hiển thị lên Table ạ.
 

Joe

Thành viên VIP
21/1/13
2,963
1,307
113
What table? I show you how to create a JTable (Java SWING):
- Read the file with java.nio is faster than FileInputStream + Scanner
- Convert the content to 2-dimensional array with split()
- Create a column array
- add the arrays to the JTable
Java:
import java.io.*;
import javax.swing.*;
import java.nio.file.*;
// Joe Nartca (C)
public class JavaTable extends JFrame {
  public JavaTable(String name) throws Exception {
    setTitle("Java SWING with JTable");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //
    File fi = new File(name); // nio: Files.readAllBytes
    String[] lines = (new String(Files.readAllBytes(fi.toPath()))).replace("\r", "").split("\n");
    String[][] data = new String[lines.length][];
    //
    int i = 0;
    for (String S : lines) data[i++] = S.split(" ");
    String[] columns = {"ID", "Name", "City", "Times", "?"};
    JTable jtable = new JTable(data, columns);
    JScrollPane sp = new JScrollPane(jtable); 
    add("Center", sp);
    pack();
    setVisible(true);
  }
  public static void main(String... args) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: java JavaTable any_File.txt");
      System.exit(0);
    }
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    new JavaTable(args[0]);
  }
}
and here is the display
javaTable.png
 
Sửa lần cuối:
  • Like
Reactions: nguyenon2202

nguyenon2202

New Member
13/7/20
2
0
1
bac giang
What table? I show you how to create a JTable (Java SWING):
- Read the file with java.nio is faster than FileInputStream + Scanner
- Convert the content to 2-dimensional array with split()
- Create a column array
- add the arrays to the JTable
Java:
import java.io.*;
import javax.swing.*;
import java.nio.file.*;
// Joe Nartca (C)
public class JavaTable extends JFrame {
  public JavaTable(String name) throws Exception {
    setTitle("Java SWING with JTable");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //
    File fi = new File(name); // nio: Files.readAllBytes
    String[] lines = (new String(Files.readAllBytes(fi.toPath()))).replace("\r", "").split("\n");
    String[][] data = new String[lines.length][];
    //
    int i = 0;
    for (String S : lines) data[i++] = S.split(" ");
    String[] columns = {"ID", "Name", "City", "Times", "?"};
    JTable jtable = new JTable(data, columns);
    JScrollPane sp = new JScrollPane(jtable);
    add("Center", sp);
    pack();
    setVisible(true);
  }
  public static void main(String... args) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: java JavaTable any_File.txt");
      System.exit(0);
    }
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    new JavaTable(args[0]);
  }
}
and here is the display
View attachment 2372
Ok, thanks you so much!