How to display data in a jTable using linked lists - java

I have a table from my SQL server and I have connected to and and stored all the data in a linked list. I now want to take that data from the linked list created (everything works fine) but when I try to store it in my table named customerTable it doesn't recognize my table. (My GUI is on a different class, so is my linked list and my customer object that I use to initialize get, set methods, so I think it has something to do with that)
Can anybody help me understand how I do this?
I use this code:
DefaultTableModel model = (DefaultTableModel) customerTable.getModel();

You should implement interface TableModel, to use your existing list as the back of the table model.
See How to Use Tables of the Java Tutorials, on creating table models.

so is my linked list and my customer object that I use to initialize get, set methods
If you are storing the data in a custom Object, then you need to create a custom TableModel for that Object.
Check out Row Table Model for a step-by-step example on how to create this custom TableModel for your object.

Related

Why do we have to use a TableModel for a JTable?

If I want to use a JTable in Java it seems to me for adding rows and doing alters from behind a button or so I always have to use a TableModel (this could be the default one or one created by your own) But my question is: Why do we have to use this. I can't find this in any of the posts I saw. Can someone explain how this works and why it is necessary? And why we can't just add rows to the JTable without a model.
It seems to me that if you want to just show a few records but at creation you don't know all the rows yet it would be easier to just do something like a table.add() to add the row.
You can create the table with data inside without a model attached to it. So why not add data?
Or am I just wrong and can you add also data without a model?
The TableModel interface defines the minimum methods needed by a JTable (view) to render its content (model). AbstractTableModel is an abstract implementation that provides the event plumbing and leaves just three methods that must be overridden. DefaultTableModel goes on to include an internal data model based on Vector and convenient methods to alter that internal model. See Creating a Table Model for a comparison and these contrasting examples.

How to output objects into my JTable so that editing a field affects the state of the original object?

I have a LinkedHashSet containing book objects. Everytime the application is opened it reads in the objects into the LinkedHashSet from a serialized file.
I was using an iterator to run through the LinkedHashSet and output the data of each objects fields into a multidimensional array to populate my JTable, but what happens is that if I click the editable Boolean column it is updating the array and not the original object?
How can I populate the JTable so that selecting the check Box changes the field of the actual object and then fire the table to update? (the table will then need to pull in the new data as that boolean field of the object sets another field.
I recommend to implement a custom table model eg. based on a List.
Here is a List based read-only table model sample I once wrote:
http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup
Follow the tutorial to implement a writable table model:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

Dynamic Table Creation in Java with No DB link

In my Java application I want to create a table whose data field will be updated dynamically. The table is not linked with any database. The tables' data fields are ArrayList or List type data so I want to update the fields when they are changing. I was looking into the jtable but was unable to find the way to do that. Anyone knows the solution is highly appreciated..
I think you should do the following
Create a class for each table
to create dynamic fields implement a map which stores name of the field as key and its value as the value of the map
create one object for each row and store them, in List(or map if you want to do indexing)
Implement search functions
Update it as and when required.
Jtable is the component for UI, You can easily use it and if there is some problem in displaying the data, you are always welcome to ask.

Related to Auto-Update of JTable

I have two radiobuttons(Say rbtn_Asia,rbtn_Europe)and one JTable. When I select rbtn_Asia, table must contains Asia's data. Similarly when I select rbtn_Europe, table must contains Europe's data. (Asia's data and Europe's data is in same database which will be updated periodically). I have implemented upto this.
My problem is like this: Consider the following case: I have selected rbtn_Asia and obviously table will contain Asia's data. Now let database has got two new tuples of Asia, how can I update the JTable dynamically without selecting the rbtn_Asia once again (because rbtn_Asia is already in selected state).
In your button handler, update your implementation of TableModel, which should then fire the appropriate event. A structure that supports clear() such as Map, shown here, is convenient. More examples may be found here.

Java Databinding: how to display data in a JTable?

I'm a .net developer that would like to try develop some simple apps in Java. I would like to know how to do databinding in Java.
How can I show a query result in a JTable?
GustlyWind's comment is the best place to look on how to use a JTable. The key for the data is getting the items in a model. You can use the DefaultTableModel which would require that your table results are put into a 2D Array or Vector. Or you could implement your own model that uses other custom objects from your application or a different underlying data structure.
Either way, as you loop through the ResultSet of your query you are going to have to pull the relevant data and stick it in some sort of Collection.

Categories