Fill and keep a JTable updated - java

I'm trying to do an MVC app with java, and i want to have a window with a JTable in it.
Apparently that's the component that can display a listbox with multicolumns, and i want to fill it with instances of different classes that implements the same interface.
The only thing i was able to do for now is to initialize my JTable with a Object[][] and an array of string for the columns name.
But this doesn't feel right. Everytime something changes in my model which contains the original list of objects, i would have to reload everything in my JTable...
Is there a way to simply bind a Vector into a JTable so that it automatically update when the Vector is modified ?
Thanks in advance for your answer.

Everytime something changes in my model
You should have a separate model. You should be changing the TableModel
Is there a way to simply bind a Vector into a JTable so that it automatically update when the Vector is modified ?
You should NOT be updating the Vector.
Instead you should be updating the TableModel. Then the TableModel will notify the JTable of the changes so the table can be repainted.
This is the way Model-View-Controller (MVC) works for all Swing components.

Related

Using JTable to display ResultSet

I'm learning about JDBC with GUI. I can do that the GUI display the data in SQL one by one. Then i want to display the data in table just like the result when i execute in SQL. I saw that many people use two Vector to add data to JTable. I wonder is there anything else apart from Vector that I can do to add data to JTable.I really want to add some images in my post but i can't so sorry if it's uncovinient for everyone. Thanks for reading.
I wonder is there anything else apart from Vector that I can do to add data to JTable.
The DefaultTableModel uses Vectors because it makes the TableModel dynamic. That is you can easily add or remove rows/columns from the model.
If you don't want to use a Vector then you can create a custom TableModel to store the data any way that you want.
Check out Row Table Model. It gives an example
of how to create a custom TableModel using an ArrayList for the data
of to write a generic TableModel that is reusable

Detecting the table of an onChanged() event with multiple JTable in a JTabbedPane

I'm currently using a JFrame to hold a JTabbedPane that contains multiple tables. In my class that extends JFrame and implements TableModelListener, I have an onChanged() method that takes a TableModelEvent as an argument. I can successfully obtain data from the event on the table that the event was fired from, but I can't determine which table it was.
From what I understand, this is not the way to do what I intend to do. I believe that I may need to write a custom TableModelListener or JTable and implement the onChanged() method there.
What do I need to do to determine which JTable was changed in the JTabbedPane? I'll need to find the table and the row that was modified.
TableModelListener and TableModelEvent won't provide information about the JTable that the model is associated with, as the model may be shared by multiple tables, in theory.
Getting the row is matter of getting the row from the event, which comes from the firstRow and lastRow properties. Once you can establish which table the model belongs to you, you can determine the view row by using JTable#convertRowIndexToView
To find the JTable you have, at least, two basic solutions
You Could...
Ask each table, stored in each JTabbedPane for their model and compare it with the model that generated the table model event
You Could...
Maintain some kind of look up between the TableModel and the JTable or JTabbedPane, depending on what it's you are ultimately after
This could be achieved by using Map of some kind, keyed to the TableModel
I believe that I may need to write a custom TableModelListener...
Check out the Table Cell Listener.
It is very similar to a TableModelListener, but you do need to specify the JTable when creating the TableCellListener, so you do have access to the table when a value is changed.

Simple Example of a Table in SWT

My last project I created a Singleton class and used Swing, to create my TableModel and then add the populated table to my base dialog class.
My new project however, I have to use SWT instead of Swing. I am afraid I know little to nothing about SWT.
I want to be able to create a Table class (SelectionsTable.java). In the table class I want to be able to create a simple table that has 4 colums and populates row values from a arraylist.
I will worry about how to pass the table to my BaseDialog class later.
Any Help is greatly appreciated.
There are really good code snippets directly from eclipse here. The one that is most interesting for you should be this one: create a table (columns, headers, lines). It shows how to create a table with multiple columns, headers and lines.
Since you seem fairy familiar with java, you should be able to figure out how to use this for your purpose.
If you want to have a proper TableViewer with ContentProvider, have a look at this excellent tutorial by Vogella.
ContentProvider is like a model that provides input for TableViewer. ( Model for Table)
LabelProvider is a class that provides image and text that you will display in a Table Cell. ( similar to getValueAt() in Swing)
create TableViwer in your dialog
create TableViewerColumn (each column) for tableviewer and set LabelProvider() on TableViewerColumn. LabelProvider.getImage() LabelProvider.getText() will be called for each row object that content provider provides on this column.
set viewer.setContentProvider()
This is how it works: TableViewer first gets input from its content provider. lets say your content provider is returning List of RowObjects. For each RowObject, the label providers on each TableViewerColumn will be invoked to dispaly image and text in that particular cell location ( like colIndex, RowIndex in Swing).

How to add columns/rows to an existing empty JTable?

I am developing a software for which I need a basic Java Swing UI. I am using Netbeans which enables me to drag and drop UI component. However there is a final result table that I need to display using code.
In my UI using trhe IDE I created a JTabbedPane, inside which I added an empty JTable (no rows nor columns) called finalOutputTable. Now I want at runtime to fill this table, let's say with columns: x, y and rows: row1, row2 & row3.
How can I do that by coding?
You need to make a custom TableModel that will allow you to support this functionality. JTable is just there to display the table on the GUI. The TableModel has all the business logic of what the data is and how each cell should act. To do this you will have to step away from the GUI-Builder and write actual code.
Tutorial: Creating a Table Model
inside which i added an empty JTable (no rows nor columns) called "finalOutputTable".
You need to add the data to the TableModel. Then you use:
table.setModel(...);
to display the data. You may also need to revalidate() the panel containing the table if you didn't reserve space for the table when you created the form.
How can i do that by coding?
See the Laying Out Components Within a Container lesson of the Java Tutorial.
You can also use a TableModel witch does all that hard work for you.
https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel

Updating JTable after inline edit

I have 2 classes, one to make a frame and the other to handle and implement the interface TableModel. When editing cells inline and updating the values in the class that implements TableModel I then need to refresh the table to show the updated data (as the table needs to auto sort thus when I inline edit a cell the rows may need to be re-ordered). The problem I'm having is after updating the data I can't figure out how to refresh the table, I've tried a hacky way of refreshing it when you click off the cell or press enter but I feel there could be a more elegant solution, any ideas?
The TableModel is responsible for invoking the fireTableCellChanged(...) method when data is changed in the model. Sorting will then happen automatically.
Read the JTable API and follow the link to the Swing tutorial on How to Use Tables for more information about TableModels and sorting.
I suggest you just use the DefaultTableModel so you don't have to worry about this since it implements all the TableModel methods.

Categories