How to add JRadioButton to group in JTable

As an alternative, use a JComboBox as an editor for mutually exclusive choices within a row. For convenience, DefaultCellEditor offers a constructor especially for this. The result also makes more efficient use of horizontal space in the row. DependentColumn is an example, shown below. See also these other alternatives. Figure from @mKorbel’s StatusRenderer and StatusEditor, … Read more

refreshing background color for a row in jtable

for example, import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import java.util.Vector; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellRenderer; public class Forum implements ListSelectionListener { private JFrame frame = new JFrame(“Frame”); private JPanel fatherCenter = new JPanel(); private JScrollPane tableScroll = new JScrollPane(); private myTableModel tableModel; private JTable … Read more

How to maintain JTable cell rendering after cell edit

When your editor concludes, the table’s editingStopped() method collects the new value via getCellEditorValue() and uses it to setValueAt() in the model. The model, in turn, should fireTableCellUpdated(), which will invoke the prescribed renderer. Extending the default should be enough to handle Number formatting. In other cases, it may be convenient to use an instance … Read more

Most simple code to populate JTable from ResultSet

I think the simplest way to build a model from an instance of ResultSet, could be as follows. public static void main(String[] args) throws Exception { // The Connection is obtained ResultSet rs = stmt.executeQuery(“select * from product_info”); // It creates and displays the table JTable table = new JTable(buildTableModel(rs)); // Closes the Connection JOptionPane.showMessageDialog(null, … Read more

Multiple row selection in JTable

Using @Hovercraft’s example and @camickr’s advice, the example below shows a suitable user interface. Although it uses buttons, the SelectionAction would also be suitable for a menu or popup. import java.awt.*; import java.awt.event.ActionEvent; import javax.swing.*; import javax.swing.DefaultListSelectionModel; import javax.swing.table.DefaultTableModel; /** @see http://stackoverflow.com/questions/4526779 */ public class CheckABunch extends JPanel { private static final int CHECK_COL = … Read more

How do I get the CellRow when there is an ItemEvent in the JComboBox within the cell

It sounds like you are Using a Combo Box as an Editor. If so, the TableCellEditor method, getTableCellEditorComponent(), includes the row as a parameter. There’s a related example here. Addendum: To change a value in the same row you’ve edited, just have the model return the correct value for the “other column” based on the … Read more

How can I put a control in the JTableHeader of a JTable?

The article How to Use Tables: Using Custom Renderers offers TableSorter as an example of how to detect mouse events on a column header. Using a similar approach, SelectAllHeader extends JToggleButton and implements TableCellRenderer in the example below to achieve a similar effect. A TableModelListener is used to condition the toggle button when all the … Read more

How to add JTable in JPanel with null layout?

Nested/Combination Layout Example The Java Tutorial has comprehensive information on using layout managers. See the Laying Out Components Within a Container lesson for further details. One aspect of layouts that is not covered well by the tutorial is that of nested layouts, putting one layout inside another to get complex effects. The following code puts … Read more

Set color of JTable row at runtime

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if(!isSelected) { //Important check, see comment below! boolean levelBreak = row == 0; if (!levelBreak) { Object prior = table.getValueAt(row – 1, 0); Object current = … Read more