有几件事情浮出水面。
您似乎已经有了一个代表您要显示的数据的对象。而不是必须在表模型之间来回转换它,您应该创建一个能够管理/保存原始对象的表模型。
String[][] loginTableLogins = new String[100][1];
被宣布错误有两个原因。首先,如果您的行数多于或少于 100 行会怎样?其次,您只声明了一个列,这显然不是您想要的。您已经拥有对象列表中包含的元素数量,因此您应该能够创建一个已经包含正确行数的数组
就个人而言,我会避免DefaultTableModel
使用AbstractTableModel
.
例如...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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;
import javax.swing.table.DefaultTableCellRenderer;
public class SimpleTable {
public static void main(String[] args) {
new SimpleTable();
}
public SimpleTable() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
LoginTableModel model = new LoginTableModel();
model.add(new Login("Elma Thud", "Shh, I'm hunting rabbits".toCharArray()));
JTable table = new JTable(model);
table.getColumnModel().getColumn(1).setCellRenderer(new PasswordCellRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PasswordCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof char[]) {
char[] pass = (char[]) value;
StringBuilder sb = new StringBuilder(pass.length);
while (sb.length() < pass.length) {
sb.append("*");
}
value = sb.toString();
}
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return this;
}
}
public class Login {
private String userName;
private char[] password;
public Login(String userName, char[] password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public char[] getPassword() {
return password;
}
}
public class LoginTableModel extends AbstractTableModel {
private List<Login> logins;
public LoginTableModel() {
logins = new ArrayList<>(25);
}
public void add(Login... login) {
int startAt = getRowCount();
logins.addAll(Arrays.asList(login));
int endAt = getRowCount();
fireTableRowsInserted(startAt, endAt - 1);
}
@Override
public int getRowCount() {
return logins.size();
}
@Override
public String getColumnName(int column) {
return column == 0 ? "Username" : "Password";
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Login login = logins.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = login.getUserName();
break;
case 1:
value = login.getPassword();
break;
}
return value;
}
}
}
查看如何使用表格了解更多详情