gpt4 book ai didi

java - 当 jtable 中存在空值时,如何分隔要动态添加到 jtable 中的行数据?

转载 作者:行者123 更新时间:2023-12-01 19:43:31 25 4
gpt4 key购买 nike

我的要求是在 JTable 中动态添加行和列。

我尝试了一个简单的代码,但我陷入了一个地方,当行数据中存在空值时,我不知道如何分隔。我尝试使用“\s+”分隔符分割行数据,但是当行数据中有空值时这不起作用。我的代码仅在行数据有值时才有效。

请在下面找到我的代码

<小时/>
String[] resultLines = data.split("\n", 0);

parsedResult = new String[resultLines.length][];
String cols[] = resultLines[1].split("\\s+");
String[] columnsParsed = new String[cols.length];
for (int i = 0; i < cols.length; i++) {
String colName = cols[i];
columnsParsed[i] = colName;
}
parsedResult[0] = columnsParsed;

int k = 1;
for (int j = 3; j < parsedResult.length; j++) {
parsedResult[k] = resultLines[j].trim().split("\\s+");
k++;
}

String[] columns = parsedResult[0];
String[][] data1 = new String[parsedResult.length - 1][];
for (int i = 1; i < parsedResult.length; i++) {
data1[i - 1] = parsedResult[i];
}

DefaultTableModel model = new DefaultTableModel(data1, columns);
jTable1.setModel(model);

请引用以下数据

Handles   NPM(K)    PM(K)      WS(K)     CPU(s)   Id  SI  ProcessName                                                  
------- ------ ----- ----- ------ -- -- -----------
322 17 3308 13800 2572 0 armsvc
171 10 6304 11224 0.11 3836 0 audiodg
591 31 11864 42480 1.39 3660 9 backgroundTaskHost
1488 55 28736 61776 1892 0 CcmExec
42 4 2332 3816 0.05 10720 9 cmd
74 6 5704 4452 0.09 12032 9 cmd
58 5 4312 6028 0.25 12188 9 cmd
42 4 2324 3800 0.06 13760 9 cmd
235 14 3004 10092 1896 0 CmRcService
216 12 2520 13128 12980 0 CompatTelRunner
88 5 916 4060 14084 0 CompatTelRunner

你们能帮我解决这个问题吗?

最佳答案

如果您利用数据右对齐(最后一列除外)的事实,则可以尝试下面我演示的方法,而不是“分隔符”方法。

由于数据对齐是精确的,因此我为每列获取精确的字符数。尝试一下,看看它是否适合您。

(我从您代码中的 resultLines 开始。)

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class ParseTableData {

public static String[] parseRow(String line) {
String[] row = new String[8];
row[0] = line.substring(0, 7).trim();
row[1] = line.substring(7, 15).trim();
row[2] = line.substring(15, 24).trim();
row[3] = line.substring(24, 35).trim();
row[4] = line.substring(35, 46).trim();
row[5] = line.substring(46, 53).trim();
row[6] = line.substring(53, 57).trim();
row[7] = line.substring(57).trim();
return row;
}

public static void main(String[] args)
{
String[] resultLines = new String[] {
"Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ",
"------- ------ ----- ----- ------ -- -- ----------- ",
" 322 17 3308 13800 2572 0 armsvc ",
" 171 10 6304 11224 0.11 3836 0 audiodg ",
" 591 31 11864 42480 1.39 3660 9 backgroundTaskHost ",
" 1488 55 28736 61776 1892 0 CcmExec ",
" 42 4 2332 3816 0.05 10720 9 cmd "
};

String[] columns = resultLines[0].split("\\s+");

String[][] data = new String[resultLines.length - 2][];
for (int i = 2; i < resultLines.length; i++) {
data[i-2] = parseRow(resultLines[i]);
}

DefaultTableModel tableModel = new DefaultTableModel(data, columns);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new JTable(tableModel)));
frame.pack();
frame.setVisible(true);
}
}

版本 2

尝试我的程序的这个版本。这里,列数和列宽是动态计算的。

(calculateColumnWidths() 中使用的算法可能会得到增强。我只是想演示该方法。)

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class ParseTableData {

public static int findFirstRowWithNoEmptyCells(String[] resultLines) {
String[] columnNames = resultLines[0].split("\\s+");
for (int i = 2; i < resultLines.length; i++) {
String[] row = resultLines[i].trim().split("\\s+");

if (columnNames.length == row.length) {
return i;
}
}
throw new IllegalStateException("Not a single complete row.");
}

public static int[] calculateColumnWidths(String rowWithNoEmptyCells) {
int columnCount = rowWithNoEmptyCells.trim().split("\\s+").length;
int[] columnWidths = new int[columnCount];

int currentColumnIndex = 0;
int currentWidth = 0;
boolean nonSpaceCharFound = false;
for (int i = 0; i < rowWithNoEmptyCells.length(); i++) {
char c = rowWithNoEmptyCells.charAt(i);
if (c == ' ') {
if (!nonSpaceCharFound) {
currentWidth++;
}
else {
columnWidths[currentColumnIndex] = i;
currentWidth = 1;
nonSpaceCharFound = false;
currentColumnIndex++;
}
}
else {
nonSpaceCharFound = true;
currentWidth++;
}
}
return columnWidths;
}

public static String[] parseRow(String line, int[] columnWidths) {
String[] row = new String[columnWidths.length];
for (int i = 0; i < row.length; i++) {
if (i == 0) {
row[i] = line.substring(0, columnWidths[i]).trim();
}
else if (i == row.length - 1) {
row[i] = line.substring(columnWidths[i-1]).trim();
}
else {
row[i] = line.substring(columnWidths[i-1], columnWidths[i]).trim();
}
}

return row;
}

public static void main(String[] args)
{
String[] resultLines = new String[] {
"Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ",
"------- ------ ----- ----- ------ -- -- ----------- ",
" 322 17 3308 13800 2572 0 armsvc ",
" 171 10 6304 11224 0.11 3836 0 audiodg ",
" 591 31 11864 42480 1.39 3660 9 backgroundTaskHost ",
" 1488 55 28736 61776 1892 0 CcmExec ",
" 42 4 2332 3816 0.05 10720 9 cmd "
};

int indexOfFirstRowWithNoEmptyCells = findFirstRowWithNoEmptyCells(resultLines);
int[] columnWidths = calculateColumnWidths(resultLines[indexOfFirstRowWithNoEmptyCells]);

String[] columns = resultLines[0].split("\\s+");

String[][] data = new String[resultLines.length - 2][];
for (int i = 2; i < resultLines.length; i++) {
data[i-2] = parseRow(resultLines[i], columnWidths);
}

DefaultTableModel tableModel = new DefaultTableModel(data, columns);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new JTable(tableModel)));
frame.pack();
frame.setVisible(true);
}
}

关于java - 当 jtable 中存在空值时,如何分隔要动态添加到 jtable 中的行数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453465/

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