我的要求是在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的行数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54453465/