How to set a custom object in a JTable row

Instead of splitting the User object up before you create the model, add it directly to the model and allow the model to do the work for you… For example public class VstTableItemModel extends AbstractTableModel { private List<User> users; public VstTableItemModel(List<User> users) { this.users = new ArrayList<User>(users); } @Override public int getRowCount() { return users.size(); … Read more

Working with several custom table models avoiding repetitive code

Like other Swing models (i.e.: DefaultComboBoxModel, DefaultListModel) we can use Generics in order to create a flexible and reusable table model, also providing an API to work with user-defined POJO’s. This table model will have the following special features: It extends from AbstractTableModel to take advantage of table model events handling. Unlike CustomerTableModel shown above, … Read more

How to refresh data in JTable I am using TableModel

You’ve done it the hard way. First of all, you’ve implemented directly from TableModel and secondly you’ve failed to implement the listener requirements… Instead, try extending from the AbstractTableModel instead, which already includes the implementations of the listener registration and notification. You will need to provide a method that will allow you to add a … Read more

Java, change a cell content as a function of another cell in the same row

I’ve revised your sscce to show the alternate approach suggested here. Note the alternate ways to get a Double constant. I’ve also re-factored the String constrants. Addendum: In helpful comments, @kleopatra observes that querying the model directly will always produce the correct result, but a TableModelListener will only see changes to column 0, not column … Read more

How to implement dynamic GUI in swing

only example, everything is hardcoded, for good understanding EDIT: as kleopatra’s noticed, moved JTable#fireTableDataChanged() from ActionListener to the TableModel, amended all ClassNames start with lowerCase import java.awt.*; import java.awt.event.*; import java.util.EventObject; import javax.swing.*; import javax.swing.table.*; public class ComponentTableTest { private JFrame frame; private JTable CompTable = null; private CompTableModel CompModel = null; private JButton addButton … Read more

JTable How to refresh table model after insert delete or update the data.

If you want to notify your JTable about changes of your data, use tableModel.fireTableDataChanged() From the documentation: Notifies all listeners that all cell values in the table’s rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in … Read more