我正在创建一个表,其中数据在用户提供输入时动态更新。我不希望表包含超过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);
}
}
答案 0 :(得分:1)
您可以在代码中放入addRow方法:
if( model.getRowCount() < YOUR_ROW_LIMIT ){
the code you already have goes here ...
}