How to fill data in a JTable with database?

I would recommend taking the following approach: Create a Row class to represent a row read from your ResultSet. This could be a simple wrapper around an Object[]. Create a List<Row> collection, and subclass AbstractTableModel to be backed by this collection. Use a SwingWorker to populate your List<Row> by reading from the underlying ResultSet on … Read more

JTable not showing

Invoking setModel() on the table should be sufficient, but you might call fireTableStructureChanged() on the model explicitly as a way to help sort things out. Also, verify that you are working on the event dispatch thread. Addendum: Here’s an sscce that shows the basic approach. import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; … Read more

How to add row in JTable?

The TableModel behind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use a DefaultTableModel To create the table with this model: JTable table = new JTable(new DefaultTableModel(new Object[]{“Column1”, “Column2”})); To add a row: DefaultTableModel model = (DefaultTableModel) table.getModel(); model.addRow(new Object[]{“Column … 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

How to mark JTable cell input as invalid?

The private static class JTable.GenericEditor uses introspection to catch exceptions raised by constructing specific Number subclasses with invalid String values. If you don’t need such generic behavior, consider creating PositiveIntegerCellEditor as a subclass of DefaultCellEditor. Your stopCellEditing() method would be correspondingly simpler. Addendum: Updated to use RIGHT alignment and common error code. Addendum: See also … Read more

Individual and not continuous JTable’s cell selection

If isn’t defined for JTable#setSelectionMode(ListSelectionModel.SINGLE_SELECTION), then CTRL + MOUSE_CLICK Or do you mean remember last selected? ListSelectionModel is used by both JTable and JList. import java.awt.Component; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import javax.swing.*; public class Ctrl_Down_JList { private static void createAndShowUI() { String[] items = {“Sun”, “Mon”, “Tues”, “Wed”, “Thurs”, “Fri”, “Sat”}; JList myJList = new … Read more

AbstractTableModel GUI display issue

Because database access is inherently asynchronous, you’ll surely want to retrieve rows in the background to avoid blocking the event dispatch thread; SwingWorker makes this relatively easy. Fetch rows in your implementation of doInBackground(), publish() interim results, and add them to the table model in your implementation of process(). A complete example that outlines the … Read more