java - JTable reload data -


i have following jtable in code:

string[] columnnames = {"name", "category", "seller", "sell price", "current bid", "end date", "open"}; object[][] data = getauctiontabledata(); final jtable auctiontable = new jtable(data, columnnames);  

i have button on form , wish reload data table on click of button. how do this? need use abstracttablemodel?

edit

here code button:

    refreshauctionbutton.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)     {         object[][] data = getauctiontabledata();         auctiontable = new jtable(data, columnnames);     }}); 

this code not work jtable declared final. have final because accessed inner classes (buttons, above).

it depends on want button change table.

  • if button triggers updates in objects within data, need use auctiontable.update (or that).

  • if triggers insertion, useful add row inserted directly auctiontable. use ((defaulttablemodel) auctiontable.getmodel).addrow(...);

  • if triggers deletion, need index of row deleted. use ((defaulttablemodel) auctiontable.getmodel).removerow(...);

otherwise, i'll need more information button's action, or can make call getauctiontabledata() each time button pressed reload whole jtable's information (something might not desirable @ all).

edit

to reload whole contents while keeping final modifier auctiontable, use setdatavector method instead of re-setting table in button's action listener.

refreshauctionbutton.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)     {         object[][] data = getauctiontabledata();         ((defaulttablemodel) auctiontable.getmodel()).setdatavector(data, columnnames);     } }); 

you'll have set columnnames final well, though.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -