0

我正在创建一个表格,其中数据在用户提供输入时动态更新。我不希望表格包含超过 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);
       }
    }
4

1 回答 1

1

您可以在 addRow 方法中放置以下代码:

if( model.getRowCount() < YOUR_ROW_LIMIT ){
  the code you already have goes here ...
}
于 2013-10-09T09:24:32.467 回答