Example usage for javax.swing DefaultCellEditor DefaultCellEditor

List of usage examples for javax.swing DefaultCellEditor DefaultCellEditor

Introduction

In this page you can find the example usage for javax.swing DefaultCellEditor DefaultCellEditor.

Prototype

public DefaultCellEditor(final JComboBox<?> comboBox) 

Source Link

Document

Constructs a DefaultCellEditor object that uses a combo box.

Usage

From source file:de.codesourcery.eve.skills.ui.spreadsheet.SpreadSheetTableModel.java

public static void main(String[] args) {

    final ITableFactory cellFactory = new ITableFactory() {

        @Override//w ww . j a  va2 s  . c  om
        public ITableCell createEmptyCell(int row, int column) {
            return new SimpleCell();
        }

        @Override
        public TableRow createRow(SpreadSheetTableModel tableModel) {
            return new TableRow(tableModel);
        }

    };

    final SpreadSheetTableModel model = new SpreadSheetTableModel(cellFactory);

    final JTable table = new JTable(model);

    table.setFillsViewportHeight(true);

    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //      table.setPreferredSize( new Dimension(400,200 ) );
    table.setBorder(BorderFactory.createLineBorder(Color.black));

    frame.getContentPane().add(new JScrollPane(table));

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            frame.pack();
            frame.setLocationRelativeTo(null);

            frame.setVisible(true);

            model.addRow(new SimpleCell("First row") {
                public boolean isEditable() {
                    return true;
                }

                public void setValue(Object value) {
                    System.out.println("new value: " + value);
                }
            });
            model.addRow(new SimpleCell("Second row #1"), new SimpleCell("Second row #2"));
            model.addRow(new SimpleCell("Third row #1"), new SimpleCell("Third row #2"),
                    new SimpleCell("Third row #3"));

            JTextField tf = new JTextField();

            table.setModel(model);
            table.setDefaultEditor(ITableCell.class, new DefaultCellEditor(tf));
        }
    });

}

From source file:Main.java

public static void insertComponentInColumn(JTable jTable, int col, JComponent jComponent) {
    TableColumn sportColumn = jTable.getColumnModel().getColumn(col);
    if (jComponent instanceof JComboBox) {
        sportColumn.setCellEditor(new DefaultCellEditor((JComboBox) jComponent));
    } else if (jComponent instanceof JCheckBox) {
        sportColumn.setCellEditor(new DefaultCellEditor((JCheckBox) jComponent));
    } else if (jComponent instanceof JTextField) {
        sportColumn.setCellEditor(new DefaultCellEditor((JTextField) jComponent));
    }//w  w w  . j  a  v a 2  s  .  c o m
}

From source file:Main.java

public Main() {
    DefaultTableModel m = new DefaultTableModel(new Object[][] { { "2", 2, 3 }, { "1", 4, 5 } },
            new Object[] { 1, 2, 3 });
    JTable t = new JTable(m);
    t.getColumnModel().getColumn(0)//from  w w w.  j a  v  a  2 s . c  om
            .setCellEditor(new DefaultCellEditor(new JComboBox(new String[] { "1", "2" })));
    t.getColumnModel().getColumn(0).setCellRenderer(getCellRenderer());
    t.setRowHeight(25);
    getContentPane().add(new JScrollPane(t));
    pack();
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());

    String[] items1 = { "Red", "Blue", "Green" };
    JComboBox<String> comboBox1 = new JComboBox<String>(items1);
    DefaultCellEditor dce1 = new DefaultCellEditor(comboBox1);
    editors.add(dce1);/*from w  w w.j  a v  a 2s .  c  o m*/

    String[] items2 = { "Circle", "Square", "Triangle" };
    JComboBox<String> comboBox2 = new JComboBox<String>(items2);
    DefaultCellEditor dce2 = new DefaultCellEditor(comboBox2);
    editors.add(dce2);

    String[] items3 = { "Apple", "Orange", "Banana" };
    JComboBox<String> comboBox3 = new JComboBox<String>(items3);
    DefaultCellEditor dce3 = new DefaultCellEditor(comboBox3);
    editors.add(dce3);

    Object[][] data = { { "Color", "Red" }, { "Shape", "Square" }, { "Fruit", "Banana" }, { "Plain", "Text" } };
    String[] columnNames = { "Type", "Value" };
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        public TableCellEditor getCellEditor(int row, int column) {
            int modelColumn = convertColumnIndexToModel(column);

            if (modelColumn == 1 && row < 3)
                return editors.get(row);
            else
                return super.getCellEditor(row, column);
        }
    };

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);//from w  w  w  .  j ava2  s  .  com

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:MainClass.java

public MainClass() {
    super("Table With DefaultCellEditor Example");
    setSize(500, 300);/*  w w  w .j  av  a  2  s  . com*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new AbstractTableModel() {
        ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0], colors[1],
                colors[2], colors[3], colors[4] };

        public int getColumnCount() {
            return 3;
        }

        public int getRowCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            switch (c) {
            case 0:
                return (r + 1) + ".";
            case 1:
                return "Some pithy quote #" + r;
            case 2:
                return data[r];
            }
            return "Bad Column";
        }

        public Class getColumnClass(int c) {
            if (c == 2)
                return ColorName.class;
            return String.class;
        }

        public boolean isCellEditable(int r, int c) {
            return c == 2;
        }

        public void setValueAt(Object value, int r, int c) {
            data[r] = (ColorName) value;
        }
    });

    table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
    table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
    table.setRowHeight(20);
    getContentPane().add(new JScrollPane(table));
}

From source file:TableCellRenderTest.java

public TableCellRenderFrame() {
    setTitle("TableCellRenderTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new PlanetTableModel();
    JTable table = new JTable(model);
    table.setRowSelectionAllowed(false);

    // set up renderers and editors

    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    table.setDefaultEditor(Color.class, new ColorTableCellEditor());

    JComboBox moonCombo = new JComboBox();
    for (int i = 0; i <= 20; i++)
        moonCombo.addItem(i);//from   www  .j av a 2s . c o  m

    TableColumnModel columnModel = table.getColumnModel();
    TableColumn moonColumn = columnModel.getColumn(PlanetTableModel.MOONS_COLUMN);
    moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
    moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
    moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));

    // show table

    table.setRowHeight(100);
    add(new JScrollPane(table), BorderLayout.CENTER);
}

From source file:de.erdesignerng.visual.editor.convertmodel.ConvertPropertyAdapter.java

@Override
public void model2view(Object aModel, String aPropertyName) {

    ConversionInfos theInfos = (ConversionInfos) aModel;

    String theCurrentTypeName = helper.getText(ERDesignerBundle.CURRENTDATATYPE);
    String theTargetTypeName = helper.getText(ERDesignerBundle.TARGETDATATYPE);

    DataType[] theTargetTypes = new DataType[theInfos.getTypeMapping().keySet().size()];

    List<DataType> theCurrentTypes = new ArrayList<>();
    theCurrentTypes.addAll(theInfos.getTypeMapping().keySet());

    Collections.sort(theCurrentTypes, new BeanComparator("name"));
    for (int i = 0; i < theCurrentTypes.size(); i++) {
        theTargetTypes[i] = theInfos.getTypeMapping().get(theCurrentTypes.get(i));
    }/*from  w  ww .  j  av a  2 s . c  o m*/

    DefaultTable theTable = (DefaultTable) getComponent()[0];
    ConversionTableModel theModel = new ConversionTableModel(theCurrentTypeName, theTargetTypeName,
            theCurrentTypes, theTargetTypes);
    theTable.setModel(theModel);

    DefaultComboBox theTargetTypesEditor = new DefaultComboBox();
    theTargetTypesEditor.setBorder(BorderFactory.createEmptyBorder());
    theTargetTypesEditor.setModel(new DefaultComboBoxModel(theInfos.getTargetDialect().getDataTypes()
            .toArray(new DataType[theInfos.getTargetDialect().getDataTypes().size()])));
    theTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(theTargetTypesEditor));
    theTable.setRowHeight((int) theTargetTypesEditor.getPreferredSize().getHeight());
}

From source file:au.com.jwatmuff.eventmanager.gui.wizard.SeedingPanel.java

private TableCellEditor getSeedingCellEditor(int numPlayers) {
    Object[] values = new Object[numPlayers + 1];
    values[0] = "None";
    for (int i = 1; i <= numPlayers; i++)
        values[i] = "" + i;

    return new DefaultCellEditor(new JComboBox<>(values));
}

From source file:de.erdesignerng.visual.editor.openxavaexport.ConvertPropertyAdapter.java

@Override
public void model2view(Object aModel, String aPropertyName) {

    OpenXavaOptions theOptions = (OpenXavaOptions) aModel;

    String theCurrentTypeName = helper.getText(ERDesignerBundle.CURRENTDATATYPE);
    String theTargetTypeName = helper.getText(ERDesignerBundle.TARGETDATATYPE);
    String theStereoTypeName = helper.getText(ERDesignerBundle.STEREOTYPE);

    String[] theTargetTypes = new String[theOptions.getTypeMapping().keySet().size()];
    String[] theStereoTypes = new String[theOptions.getTypeMapping().keySet().size()];

    List<DataType> theCurrentTypes = new ArrayList<>();
    theCurrentTypes.addAll(theOptions.getTypeMapping().keySet());

    Collections.sort(theCurrentTypes, new BeanComparator("name"));
    int theRow = 0;
    for (DataType theType : theCurrentTypes) {
        OpenXavaTypeMap theMap = theOptions.getTypeMapping().get(theType);
        theTargetTypes[theRow] = theMap.getJavaType();
        theStereoTypes[theRow] = theMap.getStereoType();
        theRow++;/* w  ww.  j a va 2  s. c o m*/
    }

    DefaultTable theTable = (DefaultTable) getComponent()[0];
    OpenXavaExportTableModel theModel = new OpenXavaExportTableModel(theCurrentTypeName, theTargetTypeName,
            theStereoTypeName, theCurrentTypes, theTargetTypes, theStereoTypes);
    theTable.setModel(theModel);

    theTable.getColumnModel().getColumn(0).setCellRenderer(ModelItemDefaultCellRenderer.getInstance());
    theTable.getColumnModel().getColumn(1).setCellRenderer(ModelItemDefaultCellRenderer.getInstance());
    theTable.getColumnModel().getColumn(2).setCellRenderer(ModelItemDefaultCellRenderer.getInstance());

    DefaultComboBox theTargetTypesEditor = new DefaultComboBox();
    theTargetTypesEditor.setBorder(BorderFactory.createEmptyBorder());
    theTargetTypesEditor.setModel(new DefaultComboBoxModel(OpenXavaOptions.SUPPORTED_STEREOTYPES));
    theTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(theTargetTypesEditor));
    theTable.setRowHeight((int) theTargetTypesEditor.getPreferredSize().getHeight());
}