import javax.swing.*; import javax.swing.table.DefaultTableModel; public class JTableExample { public static void main(String[] args) { JFrame frame = new JFrame(); JTable table = new JTable(new DefaultTableModel(new Object[]{"Name","Age","Gender"}, 0)); JScrollPane scrollPane = new JScrollPane(table); frame.add(scrollPane); frame.setSize(300,200); frame.setVisible(true); table.setDefaultEditor(String.class, new DefaultCellEditor(new JTextField())); } }
import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; public class JTableExample { public static void main(String[] args) { JFrame frame = new JFrame("JTable Example"); frame.setLayout(new BorderLayout()); JTable table = new JTable(new DefaultTableModel(new Object[]{"Name","Age","Gender"}, 0)); JScrollPane scrollPane = new JScrollPane(table); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(400,300); frame.setVisible(true); table.setDefaultEditor(Integer.class, new SpinnerEditor()); } static class SpinnerEditor extends AbstractCellEditor implements TableCellEditor { SpinnerNumberModel model = new SpinnerNumberModel(0, 0, 120, 1); JSpinner spinner = new JSpinner(model); @Override public Object getCellEditorValue() { return spinner.getValue(); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { spinner.setValue(value); return spinner; } } }In this example, we create a JTable with three columns: Name, Age, and Gender. We use setDefaultEditor to set the default editor for the Integer data type to be a SpinnerEditor class which extends AbstractCellEditor and implements TableCellEditor. This custom editor allows users to edit the age column using a spinner component. The package library used in these examples is the javax.swing package.