Data from database to JList - java

I created a database, in which one of the table name is Location which has 2 columns named Stadium and City. I want that data in each column should get retrieved in 2 JList, but I don't know how. Can you guys tell me how to do it?

The normal way to display a table from a database is of course a JTable. But if you want to opt for 2 JLists instead, it is basically the same principle.
Create a model based on the contents of the database (in this case a ListModel). You can probably just extend from AbstractListModel. Once you have your model, it is trivial to create a JList for it by just passing the model in the constructor.
See the How to use lists tutorial and pay special attention on the Creating a model part of that tutorial

Related

JTable based on a list of Objects, like TableView's items list

I have an application that was using a JavaFX TableView, but I need to switch to using a JTable because of certain issues I was running into.
TableViews take in a list of objects and each column is a field in each object, for example, a Person object with columns for First Name, Last Name, and email. If you edited the email field in the table, it would go out and edit the email in that Person object. If I selected rows in the table, I could get the Person objects from those selected indices.
Is there a way to do this in JTable?
You need to create a custom TableModel for your Person object.
Check out Row Table Model which gives a step by step example how you might do this.
It also provide a generic class to make the implementation easier and include additional generic methods for accessing the data in the TableModel.
You can also follow the BeanTableModel link for a TableModel you can use without any custom code.

Use TableModel to save and handle data or let it just show Objects (contents)

I am writing an application that uses a JTable containing a model (DefaultTableModel). Now after working on it for some time I am not really sure if it's the best Idea to store all the data in the model.
What benefits are there if all the data is stored in Objects (in an ArrayList for example) and the model only shows the data from the objects of the list?
This came into my mind particularly after I need to write some sorting algorithms which would be way easier to handle on some kind of list. Also saving the "model" --> the list of objects (then) would be easier than saving a table model.
Is it more efficient to stay with my current concept (save data in model | serialize model to save table | sort model itself) or is it better to save everything in Objects each Object representing one row, which are stored in a list?
I need to write some sorting algorithms
JTable supports sorting of the data in the TableModel directly. Read the section from the Swing tutorial on Sorting and Filtering for more information. So this can be handled directly by the table so you don't need external code.
each Object representing one row, which are stored in a list?
Yes this is a valid approach, but the data must still reside in the TableModel. That is the List containing your custom Objects that represent a row in the table must be defined in the TableModel. If for some reason you need to access the List you add helper methods to the TableModel. Don't keep the data in two places.
For example, check out Row Table Model which was designed for this concept. The JButtonTableModel.java code shows what you would need to code for each custom object you want to store in a table.

How to get the value from Jtable

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

How do I make the data passed in a table is no longer displayed in the other

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

JTable auto-update help

Greetings,
I have been understanding how DefaultTableModel will work on my problem.
I had this JTable which already has the data from a query from my data using JDBC - MySQL.
I the role is that the index0 of the JTable always displays the primary key.
And there will always be an empty row at the end two-dimensional object array where the data of the cell will be retrieved.
As of now, I included in the model a setValueAt method which triggers a fireCellUpdated().
It worked but the downside is that the data would display on the JTable but is changed on the array.
How could I resolve this?
Your response is highly appreciated.
Thanks.
When I check the array, the changes were there. But it won't display on the JTable
You should never update the array directly. The array is used to populate the data in the DefaultTableModel when you create the model. The DefaultTableModel stores the data in a Vector of Vectors so all the data is copied from the Array to the model. After that all updates must to done directly to the TableModel.
If I understand correctly, your data model contains additional information that's not intended for display. DefaultTableModel may be a little awkward for this. In the alternative, your might want to consider AbstractTableModel, which should make it easier to expose only certain portions of the data to your JTable. There are examples in the article How to Use Tables: Creating a Table Model and here.

Categories

Resources