I have a wicket application with several pages, many of which use an AJAX table to display the data. But until now their columns have all been static, only the rows change. The new screen does the following:
The user enters various query params and clicks submit. One parameter is a date unit (hours, days, weeks or months) which alongside a from/to date specifies the number of columns to display.
the data provider gets hold of the params and refreshes the data to display
.. but how to update the columns in the table?
Looking at the DataGridView object in the table code, it looks as if the columns are unchangeable without a major reimplementation of the data table object. Does anyone have any examples of how to update both the rows and columns of a table when the underlying data changes? Is it even possible?
if you hold a reference to the List that you give the data table in the constructor, in theory you should be able to manipulate it to remove/add columns. however, i am not sure if all the code surrounding the data table takes that usecase into account.
the safest way to do this is to simply to replace the data table instance with a new one that has the correct list of columns.
so something like this
addOrReplace(new DataTable("table", ...))
whenever the columns need to change.
Related
i want to create a class from fields of dynamic form so that i can create corresponding table in sqlite dynamically.
i an generating the form based on xml file. after i capture data i want to save those data into sqlite database. But since i am not sure what fields will be there in form, i can't create database. so i thought of creating database dynamically based on form fields but for that i need a class having the form fields as data which will be passed to the method which will create the table.
here is advice, create the database before touching to any anything else along with the table with possible column you might require and keep default value to null. that way you have to only update the data in table you get from the form.
You would have to come up with a way to recognizing which columns to update, this looks cumbersome but will help you if you plan on updating your app or even while making changes
Currently im doing on a project on ordering system. Let me explain the flow of the system. In ordering class, i have 2 jtable. One of the jtables shows the list of food retrieve from the database. I just have to click on one of the food and press"add", the selected food will transfer to another jtable . After i press"proceed" i will be led to a jtable, to the next jpanel, which consists of the food i have selected. However" i allow one cell to be editable,"quantity" is there any way on how to retrieve the data input by the user and store to database?
Depending on the implementation of the TableModel, you could get the data for an individual row. This would, generally, be the preferred solution, as the data for the row would be encapsulated within a POJO which maintains all the data that you need in a single place that easily manageable.
If you've just dumped a bunch of unassociated properties into the table, you will need to use something like JTable#getValueAt or TableModel#getValueAt
See How to Use Tables for more details
I have a database query returning a large number of rows. JDBC doesn't tell you how many rows come back and there are too many rows to count them before I display it in a JTable.
So the model I'm working with is an Iterable<R>, but unfortunately, TableRowModel has to know the row count up-front.
I can imagine a few strategies which might work:
Load all rows, but do it in the background and add them to the model in batches.
Somehow detect requests to render the lower rows (via the model I guess) and dynamically load more rows if this happens.
Have a button on the screen to explicitly load more rows.
Some kind of non-JTable component which supports rendering rows without knowing the row count in advance (ideal, but I can't find one.)
I am wondering if there is a "normal" way to do this, because it isn't often that I see this sort of thing in a UI. The few JDBC tools I have checked out (I figured these would be the best bet) seem to either eagerly load the results or page them (as a user, I really dislike paging in GUI apps, so I would like to avoid that.)
Operations like "Find" over the table should ideally work in an "understandable" way though... so it seems like this is not going to be too easy.
Use a SwingWorker in your implementation of AbstractTableModel. Let the worker partition the query so that a reasonable number of records are available promptly, while the rest are read in the background. Use a PropertyChangeListener to show progress and implement a cancel button.
You have 2 possibilities:
Write query, which returns the number of rows, and then implement
table model, which tries to load rows from the database, if table
want to show the rows which are not in the model. In this case you need the third query,
which can index based load the rows (these queries are possible in
MS-SQL and Oracle). For example: you loads the first 100 rows, and
the number of results. If table model is asked for rows from
position 100 to 199 you makes new query, which loads these rows
interval (if it's possible for your database) or you tries to
iterate over your result set, and load the next data batch. But if
user presses the "End" key you definitly needs the index based
loading (or you need to iterate over the complete result set).
You write model, which shows, the first batch (for example 100
rows). If user, scrolls to view the last row (model.getValueAt(row,
col) -> col == 99) you simply asks the result set for next batch and
add the new rows to the model.
First variant is better for the user (because the user can see how large is the table and can directly scroll to last row), but the second is easier to implement because it does not need index based loading of rows.
I have a problem for a couple of hours does not resolve, is the following.
I have a Table A and Table B.
Table A is filled with a ResultSet, and Table B is being added to products available in Table A
What I am trying to achieve is: Once you click on the Add button Spend the product of Table A to Table B, and the last product is no longer displayed in Table A.
The responsibility to inform the tables that data was added and remove to and from them is completely up to you, and should be handled in each table's model. If you want to 'move' data from one to the other, you need to remove that data from the model of the first table and add it to the 2nd. Once that is done, both models need to be informed of the change via fireTableDataChanged, so they know to redraw.
I would endorse #Colby's and #GETah's suggestion to focus on the model. Also consider giving modelA and modelB access to a common data structure, say List<Product>, where each Product has a boolean attribute that allows each model to to decide what it's corresponding view can see.
Get and remove the selected element from tableA's TableModel and add it to the tableB's TableModel and then refresh both tables.
See this tutorial on using JTable and TableModel
I am using a JTable in java for listing the values from database.
I need something like, I need to list few set of values in the JTable. And when we scroll down or scroll up using scroll pane of JTable, next set of values must be loaded from database. so that instead of loading all values, i can list few values and scrolling action will retrieve next range of values.
How can I do this?
Can any one suggest me an idea for this?
You need to implement a TableModel, like subclassing DefaultTableModel. Need to track which rows you already retrieved from database and when table request the table model more rows, grab from database. This will occurs as user scrolls the table. Be careful if the total number of rows is huge, as you should discard rows retrieved and not shown currently in the table to save memory.
This question is not easy and implementations could vary. You can maintain an open connection and read from the ResultSet, you can open a new connection each time... If retrieving the rows takes too much time, the user will experience the scroll "freezes" while retrieving the new rows. Another problem is calculating the total number of rows, as table needs it for several operations (calculate the scrolling available...), implementing getRowCount() could be problematic with very large queries. You should perform a SELECT COUNT before to get the total number of rows and then perform the SELECT to start grabbing they.
Mi advice is to retrieve all rows from database and pass to the table. If the total number of rows is not too much large (<10000), JTable handles without problem such number of rows (some memory is required!) and the scroll will be perfectly smooth and only one database access is performed. If you know user already will not scroll the entire set of rows, try to adopt another technique, like limiting the total number of rows returned from database, setting a filter and getting all the rows (limited for example to 100) and show all in the table in order to avoid memory usage and database access.
In the days of Java 1.4.1 and Pentium IV (2004), we use to populate the JTable with all rows returned from database, limiting they to 10000 (WHERE ROWNUM < 10000 in Oracle) and the application needs more memory but works fine and smooth. The time required to retrieve all the rows was larger, but use a waiting dialog and let the user wait for the data (or he/she can filter the data to retrieve fewer rows).
I am not sure how to do it with the scroll action. You could have next and back buttons instead. You could initially show the first set of values. Clicking on next or back will cause the next set to be retrieved. You might need some reference to the first and last value in the displayed list.