0

I have a few text fields, A JTable and a Button (ADD).

Now, every time the button it gets the JTextFields data and adds a row in JTable. And it adds the unlimited number of rows. I want to add only 10 rows.

public void setItemDetailInTable() {
    String free = freetext.getText();
    Object row[] = new Object[20];


    row[0] = itemcombo.getSelectedItem().toString();
    row[1] = mfgtext.getText();
    row[2] = sachsntext.getText();
    if (free.isEmpty()) {
        row[3] = qtytext.getText();
    } else {
        row[3] = qtytext.getText() + "+" + freetext.getText();
    }
    row[4] = pricetext.getText();
    row[5] = taxtext.getText() ;
    row[6] = discounttext.getText() ;
    row[7] = total.getText();

    model_table.addRow(row);
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Karan Malhotra
  • 125
  • 3
  • 11
  • Please be aware of the Java naming convention. You have a variable called `model_table`. Variable names should be camelCase, which would be `modelTable`. Also try to make your types as specific as possible. If you know that `row` will only contain `String`s, declare it as `String row[]`. Which brings me to the next point.. Why does `row` have the size 20? You never fill those all 20 places in the array..? – Neuron Sep 13 '18 at 14:30

2 Answers2

0

Just add a counter ?

if (table.getRowCount < 10) {
     //doStuff...
} else {
    //doSomethingElse...
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Phreneticus
  • 349
  • 2
  • 11
  • well, its simple that it require a decision statement... but what to put in doStuff ? – Karan Malhotra Sep 13 '18 at 12:20
  • *"but what to put in doStuff ?"* One thing you might do is pop a dialog or change a label text to inform the user the table is full, then disable the text fields and either the button or its `Action`. Have a go at doing that, then if you hit a problem, post a [mcve]. – Andrew Thompson Sep 13 '18 at 13:18
0

okay, I got it..

if (table.getRowCount > 10){
    model_table.setRowCount(10)
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Karan Malhotra
  • 125
  • 3
  • 11