我正在创建一个表格,其中数据在用户提供输入时动态更新。我不希望表格包含超过 10 行。即我想将用户输入限制为 10 行。对此的任何提示表示赞赏。
这是我的代码
class TablePanel extends JPanel {
private String[] COLUMNS = {"FAMILY MEMBERS", "STAR (NAKSHATRA)"};
private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0){
public boolean isCellEditable(int row, int column) {
return false;
}
};
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(){
public void setHorizontalAlignment(int alignment) {
alignment = (int) CENTER_ALIGNMENT;
};
};
private JTable table = new JTable(model){
public void setDefaultRenderer(java.lang.Class<?> columnClass, TableCellRenderer renderer) {
columnClass= String.class;
renderer=centerRenderer;
};
public void setBackground(Color bg) {
bg = Color.yellow;
};
};
public TablePanel() {
setLayout(new BorderLayout());
setBackground(new Color(135, 206, 235));
add(new JScrollPane(table));
}
public void addRow(String memb, String star) {
Object[] row = new Object[2];
row[0]=memb;
row[1]=star;
model.addRow(row);
}
}