我正在摆弄 JTable,我想制作一个表,其中字母 A-P 作为行名称,数字 1-24 作为列名称(无论它们是什么数据类型)。这些行和列名称是在表内部还是作为外部轴(以更简单的为准)并不重要。
我当前的尝试是为行和列名称命名一个String[]
,并为实际内容命名一个String[][]
。但我不知道如何将其合并为创建 JTable 的有效格式。我当前的代码(不起作用)如下:
// Create table
String[] columnNames = {"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"
};
String[] rowNames = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"};
String[][] compoundNames = new String[25][16];
Object[][] plateData = {rowNames, compoundNames};
table = new JTable(plateData, columnNames);
table.setBorder(new LineBorder(new Color(0, 0, 0)));
table.setBounds(10, 11, 564, 440);
contentPane.add(table);
建议的解决方案不必遵循这个概念思想。如前所述,如果能用最简单的方法来制作我需要的东西,我将不胜感激。
最佳答案
String[][]compoundNames = new String[25][16];
应为String[][]compoundNames = new String[16][25];
(columnName
和rowNames
的元素数量)基本信息请阅读Oracle教程How to use Tables
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.TableModel;
public class TableWithTimer {
private JFrame frame = new JFrame();
private JScrollPane scroll = new JScrollPane();
private JTable myTable;
private String[] columnNames = {"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};
private String[] rowNames = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z"};
private Object[][] compoundNames = new String[26][25];
private int count = 1;
private javax.swing.Timer timer = null;
public TableWithTimer() {
myTable = new JTable(compoundNames, columnNames);
myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
myTable.setPreferredScrollableViewportSize(new Dimension(400, 300));
myTable.setShowHorizontalLines(true);
myTable.setShowVerticalLines(true);
scroll.setViewportView(myTable);
frame.add(scroll, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
start();
}
private void start() {
timer = new javax.swing.Timer(2500, updateCol());
timer.start();
}
public Action updateCol() {
return new AbstractAction("text load action") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("updating row " + (count + 1));
TableModel model = myTable.getModel();
int row = model.getRowCount() - 1;// -1 == leave the cell empty at [0, 0]
for (int j = 0; j < row; j++) {
myTable.changeSelection(row, 0, false, false);
Object value = rowNames[j];
model.setValueAt(value, count, 0);
count++;
if (count >= myTable.getRowCount()) {
myTable.changeSelection(0, 0, false, false);
timer.stop();
System.out.println("update cycle completed");
myTable.clearSelection();
}
}
}
};
}
public static void main(String args[]) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
//System.out.println(info.getName());
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
TableWithTimer tableWithTimer = new TableWithTimer();
}
}
关于java - 带轴 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967045/