gpt4 book ai didi

java - 将 6 个 ArrayList 中的数据存入单个 JTable - Java Swing

转载 作者:行者123 更新时间:2023-12-02 05:45:30 24 4
gpt4 key购买 nike

我创建了一个 JTable,其中填充了各种数组列表,这些数组列表使用“~”进行分割从文本列表中获取数据。我遇到的问题是该表在一行上显示列表中的所有数据。例如:

Column1          Column2           Column2          Column2        Column3     Column4
1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5

When I want it to display

Column1 Column2 Column2 Column2 Column3 Column4
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3

你明白了。根据之前的建议,我认为问题可能是循环的,但我不确定。

任何建议都会很棒。

代码如下:

    private void table(){     

String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
DefaultTableModel model = new DefaultTableModel(colName,0);
for(Object item : courseList){
Object[] row = new Object[6];
// String[] row = new String[6];

row[0] = fileManage.getCourseList();
row[1] = fileManage.getNameList();
row[2] = fileManage.getModeratorList();
row[3] = fileManage.getSemesterList();
row[4] = fileManage.getProgramList();
row[5] = fileManage.getMajorList();
model.addRow(row);
textArea = new JTable(model);
}

这是具有数组列表的类:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class FileIOManagement {

private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<String> courseList = new ArrayList<String>();
private ArrayList<String> semesterList = new ArrayList<String>();
private ArrayList<String> moderatorList = new ArrayList<String>();
private ArrayList<String> programList = new ArrayList<String>();
private ArrayList<String> majorList = new ArrayList<String>();



public ArrayList<String> getNameList(){
return this.nameList;
}

public ArrayList<String> getCourseList(){
return this.courseList;
}
public ArrayList<String> getSemesterList(){
return this.semesterList;
}
public ArrayList<String> getModeratorList(){
return this.moderatorList;
}
public ArrayList<String> getProgramList(){
return this.programList;
}
public ArrayList<String> getMajorList(){
return this.majorList;
}

public void setNameList(ArrayList<String> nameList){
this.nameList = nameList;
}

public void setCourseList(ArrayList<String> courseList){
this.courseList = courseList;
}
public void setSemesterList(ArrayList<String> semesterList){
this.semesterList = semesterList;
}
public void setModeratorList(ArrayList<String> moderatorList){
this.moderatorList = moderatorList;
}

public void setProgramList(ArrayList<String> programList){
this.programList = programList;
}
public void setMajorList(ArrayList<String> majorList){
this.majorList = majorList;
}

public FileIOManagement(){
setNameList(new ArrayList<String>());
setCourseList(new ArrayList<String>());
setSemesterList(new ArrayList<String>());
setModeratorList(new ArrayList<String>());
setProgramList(new ArrayList<String>());
setMajorList(new ArrayList<String>());
readTextFile();
getNameList();
getCourseList();
}

private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));

while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();


courseList.add(course);
semesterList.add(semester);
nameList.add(examiner);
moderatorList.add(moderator);
programList.add(program);
majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);

}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}



}

这是我需要 JTable 的类:

import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

public class AllDataGUI extends JFrame{


private JButton saveCloseBtn = new JButton("Save Changes and Close");
private JButton closeButton = new JButton("Exit Without Saving");
private JFrame frame=new JFrame("Viewing All Program Details");
private final FileIOManagement fileManage = new FileIOManagement();
private ArrayList<String> nameList = new ArrayList();
private ArrayList<String> courseList = new ArrayList();
private ArrayList<String> semesterList = new ArrayList();
private ArrayList<String> moderatorList = new ArrayList();
private ArrayList<String> majorList = new ArrayList();
private ArrayList<String> programList = new ArrayList();
private JTable textArea;



public ArrayList<String> getNameList(){
return this.nameList;
}

public ArrayList<String> getCourseList(){
return this.courseList;
}
public ArrayList<String> getSemesterList(){
return this.semesterList;
}
public ArrayList<String> getModeratorList(){
return this.moderatorList;
}
public ArrayList<String> getProgramList(){
return this.programList;
}
public ArrayList<String> getMajorList(){
return this.majorList;
}

public void setNameList(ArrayList<String> nameList){
this.nameList = nameList;
}

public void setCourseList(ArrayList<String> courseList){
this.courseList = courseList;
}
public void setSemesterList(ArrayList<String> semesterList){
this.semesterList = semesterList;
}
public void setModeratorList(ArrayList<String> moderatorList){
this.moderatorList = moderatorList;
}

public void setProgramList(ArrayList<String> programList){
this.programList = programList;
}
public void setMajorList(ArrayList<String> majorList){
this.majorList = majorList;
}



public AllDataGUI(){
getData();
table();
panels();
}


public Object getValueAt(int rowIndex, int columnIndex) {
String[] token = nameList.get(rowIndex).split(",");
return token[columnIndex];

}

private void table(){


String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
DefaultTableModel model = new DefaultTableModel(colName,0);
for(Object item : courseList){
Object[] row = new Object[6];
// String[] row = new String[6];

row[0] = fileManage.getCourseList();
row[1] = fileManage.getNameList();
row[2] = fileManage.getModeratorList();
row[3] = fileManage.getSemesterList();
row[4] = fileManage.getProgramList();
row[5] = fileManage.getMajorList();
model.addRow(row);
textArea = new JTable(model);



// String END_OF_LINE = ",";
//
// String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
//// textArea.getTableHeader().setBackground(Color.WHITE);
//// textArea.getTableHeader().setForeground(Color.BLUE);
// // Font Tablefont = new Font("Details", Font.BOLD, 12);
// // textArea.getTableHeader().setFont(Tablefont);
// Object[][] object = new Object[100][100];
// int i = 0;
// if (fileManage.size() != 0) {
// for (fileManage book : fileManage) {
// object[i][0] = fileManage.getCourseList();
// object[i][1] = fileManage.getNameList();
// object[i][2] = fileManage.getModeratorList();
// object[i][3] = fileManage.getSemesterList();
// object[i][4] = fileManage.getProgramList();
// object[i][5] = fileManage.getMajorList();
//
// textArea = new JTable(object, colName);
// }
// }
}
}



public void getData(){
nameList = fileManage.getNameList();
courseList = fileManage.getCourseList();
semesterList = fileManage.getSemesterList();
moderatorList = fileManage.getModeratorList();
majorList = fileManage.getMajorList();
programList = fileManage.getProgramList();
// textArea.(write());
}

private JButton getCloseButton(){
return closeButton;
}




private void panels(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollBarForTextArea);


frame.add(panel);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
rightPanel.add(saveCloseBtn);
rightPanel.add(closeButton);
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frame.dispose();

}
});
saveCloseBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
//saveBtn();
frame.dispose();

}
});
frame.setSize(1000, 700);
frame.setVisible(true);
frame.setLocationRelativeTo(null);

}

// private void saveBtn(){
// File file = null;
// FileWriter out=null;
// try {
// file = new File("Course.txt");
// out = new FileWriter(file);
// out.write(textArea.getText());
// out.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// JOptionPane.showMessageDialog(this, "File Successfully Updated");
//
// }



}

最佳答案

Table

可能有几种方法可以实现这一点,对我来说,我会从创建自己的 TableModel 开始,这将允许我控制数据添加到其中和管理的方式。

例如

public class ColumnBasedTableModel extends AbstractTableModel {

private Map<String, List<String>> columnData;
private List<String> columnNames;

public ColumnBasedTableModel() {
columnData = new HashMap<>(25);
columnNames = new ArrayList<>(25);
}

public void addColumn(String name, String... data) {
columnNames.add(name);
List<String> colData = columnData.get(name);
if (colData == null) {
colData = new ArrayList<>(26);
columnData.put(name, colData);
}
colData.addAll(Arrays.asList(data));
}

@Override
public int getRowCount() {
int rows = 0;
for (List<String> data : columnData.values()) {
rows = Math.max(data.size(), rows);
}
return rows;
}

@Override
public int getColumnCount() {
return columnNames.size();
}

@Override
public String getColumnName(int column) {
return columnNames.get(column);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
String name = getColumnName(columnIndex);
List<String> data = columnData.get(name);
if (data != null) {

if (rowIndex < data.size()) {
value = data.get(rowIndex);
}

}
return value;
}

}

这个简单的模型在 Map 中维护列数据,并以列名称为键。 Map 值中的每个元素都是列中的一行...

可运行的示例...

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class SimpleTableExample {

public static void main(String[] args) {
new SimpleTableExample();
}

public SimpleTableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

ColumnBasedTableModel model = new ColumnBasedTableModel();
model.addColumn("Column 1", createColumn(1, 5));
model.addColumn("Column 2", createColumn(6, 9));
model.addColumn("Column 3", createColumn(11, 3));
model.addColumn("Column 4", createColumn(16, 8));
model.addColumn("Column 5", createColumn(21, 15));
model.addColumn("Column 6", createColumn(26, 7));


JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(new JTable(model)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

});
}

protected String[] createColumn(int startAt, int count) {

String[] values = new String[count];
for (int index = 0; index < count; index++) {
values[index] = Integer.toString(startAt + index);
}
return values;

}

public class ColumnBasedTableModel extends AbstractTableModel {

private Map<String, List<String>> columnData;
private List<String> columnNames;

public ColumnBasedTableModel() {
columnData = new HashMap<>(25);
columnNames = new ArrayList<>(25);
}

public void addColumn(String name, String... data) {
columnNames.add(name);
List<String> colData = columnData.get(name);
if (colData == null) {
colData = new ArrayList<>(26);
columnData.put(name, colData);
}
colData.addAll(Arrays.asList(data));
}

@Override
public int getRowCount() {
int rows = 0;
for (List<String> data : columnData.values()) {
rows = Math.max(data.size(), rows);
}
return rows;
}

@Override
public int getColumnCount() {
return columnNames.size();
}

@Override
public String getColumnName(int column) {
return columnNames.get(column);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
String name = getColumnName(columnIndex);
List<String> data = columnData.get(name);
if (data != null) {

if (rowIndex < data.size()) {
value = data.get(rowIndex);
}

}
return value;
}

}

}

FileIOManagement TableModel 示例

public class FileIOTableModel extends AbstractTableModel {

public static final String[] COLUMN_NAMES = {
"Course",
"Name",
"Moderator",
"Semester",
"Program",
"Major",};

private FileIOManagement manager;

public FileIOTableModel(FileIOManagement manager) {
this.manager = manager;
}

@Override
public int getRowCount() {
int row = manager.getCourseList().size();
row = Math.max(row, manager.getMajorList().size());
row = Math.max(row, manager.getModeratorList().size());
row = Math.max(row, manager.getNameList().size());
row = Math.max(row, manager.getProgramList().size());
row = Math.max(row, manager.getSemesterList().size());
return row;
}

@Override
public int getColumnCount() {
return 6;
}

@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

List<String> data = null;
switch (columnIndex) {
case 0:
data = manager.getCourseList();
break;
case 1:
data = manager.getNameList();
break;
case 2:
data = manager.getModeratorList();
break;
case 3:
data = manager.getSemesterList();
break;
case 4:
data = manager.getProgramList();
break;
case 5:
data = manager.getMajorList();
break;
}

Object value = null;
if (data != null) {
if (rowIndex < data.size()) {
value = data.get(rowIndex);
}
}

return value;

}

}

关于java - 将 6 个 ArrayList 中的数据存入单个 JTable - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com