概述
主要知识点
- SwingNode类
- javacsv-2.0.jar: 用于通过文件地址读取csv文件,并可以进行一系列操作.尽管2008年之后就不再更新,但操作个csv文件也够用了。
- FileChoose类
- CsvReader类
- JTable类:创建一个JTable实例做csv文件打开后的展示,需要注意参数的顺序,表格内容是一个二维数组,表头是一个一维数组
JTable table = new JTable(表格内容,表头);
f. 把一位数组存进一维数组:
String[][] arr = new String[10][];//开辟一个10行的二维数组
String[] row1 = {"id","name","sex","age"};
arr[0] = row1;//存进二维数组
g. JTable不显示表头:需要把JTable对象放进一个Pane里
JTable table = new JTable(表内容,表头);
JScrollPane jScrollPane = new JScrollPane(table);
SwingNode swingNode = new SwingNode();
swingNode.setContent(jScrollPane);//使用swingNode封装swing组件,就可以在Javafx中用了
CsvReader的主要方法
- new CsvReader(String filePath)
- boolean readHeaders()
- String[] getHeaders()
也就是这样:
CsvReader reader = new CsvReader("xxx.csv");
reader.readHeaders(); //没有这句话,执行下面会报错
String[] head = reader.getHeaders();
- boolean readRecord()
- String getRawRecord()
while (reader.readRecord()){
System.out.println(reader.getRawRecord());//输出一行内容
}
实例 - 读取本地桌面的一个csv文件
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("文件选择器");
primaryStage.setHeight(600);
primaryStage.setWidth(800);
final FileChooser fileChooser = new FileChooser();
//设置打开资源管理器后的文件过滤
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("All Images","*.*"),
new FileChooser.ExtensionFilter("PNG","*.png"),
new FileChooser.ExtensionFilter("MP4","*.mp4"),
new FileChooser.ExtensionFilter("CSV","*.csv")
);
final Button open = new Button("打开文件");
final GridPane inputGridPane = new GridPane();//创建格子布局面板
GridPane.setConstraints(open,0,0);//第0行0列
inputGridPane.setHgap(6.0);//设置水平间距
inputGridPane.setVgap(6.0);//设置垂直间距
inputGridPane.getChildren().addAll(open);//添加按钮
final Pane rootGroup = new VBox(12);//创建一个垂直盒子布局器
rootGroup.getChildren().addAll(inputGridPane);//把格子面板放进来
rootGroup.setPadding(new Insets(12,12,12,12));
primaryStage.setScene(new Scene(rootGroup));
primaryStage.show();
//设置点击-打开文件-的动作事件
open.setOnAction(event -> {
File file = fileChooser.showOpenDialog(primaryStage);//在当前窗口打开文件选择器
if (file != null){
try {
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream stream = new BufferedInputStream(inputStream);
String fileName = file.getName();
String filePath = file.getAbsolutePath();
System.out.println("文件路径 = "+filePath);
try {
CSVDemo.read(filePath);
} catch (IOException e) {
e.printStackTrace();
}
//封装JTable,使得JTable和Javafx嵌套在一起
SwingNode swingNode = new SwingNode();
try {
JTable table = read(filePath);
JScrollPane jScrollPane = new JScrollPane(table);
swingNode.setContent(jScrollPane);
} catch (IOException e) {
e.printStackTrace();
}
//设置JTable打开后表格的相对位置
GridPane.setConstraints(swingNode,0,1);
inputGridPane.getChildren().add(swingNode);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
//读取csv文件并把它读取到JTable中返回
public static JTable read(String filePath) throws IOException {
CsvReader reader = new CsvReader(filePath);
reader.readHeaders();//跳过表头
String[] head = reader.getHeaders();
List<String []> list = new ArrayList<>();
String s = reader.getRawRecord();
System.out.println("表头 "+s);
String[] r1 = dataToArray(s);
// list.add(r1);
while (reader.readRecord()) {
System.out.println(reader.getRawRecord());
list.add(dataToArray(reader.getRawRecord()));
}
String[][] data = new String[list.size()][];
System.out.println("一共"+list.size()+"行数据");
for (int i = 0; i < data.length; i++) {
data[i] = list.get(i);
}
JTable table = new JTable(data,head);
return table;
}
//将每一行的数据从String转为String数组
public static String[] dataToArray(String row){
String[] res = row.split(",");
return res;
}
效果展示
JScrollPane封装JTable,SwingNode封装JScrollPane