Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerListModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

public class Main {
    public static void main(String[] argv) throws Exception {
        JTable table = new JTable();
        DefaultTableModel model = (DefaultTableModel) table.getModel();

        model.addColumn("A", new Object[] { "item1" });
        model.addColumn("B", new Object[] { "item2" });

        String[] values = new String[] { "1", "2", "3" };
        TableColumn col = table.getColumnModel().getColumn(0);
        col.setCellEditor(new SpinnerEditor(values));
    }
}

class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {
    final JSpinner spinner = new JSpinner();

    public SpinnerEditor(String[] items) {
        spinner.setModel(new SpinnerListModel(Arrays.asList(items)));
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
            int column) {
        spinner.setValue(value);
        return spinner;
    }

    public boolean isCellEditable(EventObject evt) {
        if (evt instanceof MouseEvent) {
            return ((MouseEvent) evt).getClickCount() >= 2;
        }
        return true;
    }

    public Object getCellEditorValue() {
        return spinner.getValue();
    }
}