문제

I don't know how to do to set a jcombobox in a specific row...for now I've this jcombobox for all rows but I want it in only one row:

JComboBox cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("CLICCA PER LE DATE");
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);
도움이 되었습니까?

해결책 2

Update: As I was testing my (probably incomplete) answer out, I came across a very good SO Question that I think will help much better than I could: Putting JComboBox into JTable

Another Update: I read your question again, and I realized you asked for a specific row. The only way I can think of doing this is to create a custom CellEditor, something like:

private static class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

    DefaultCellEditor other = new DefaultCellEditor(new JTextField());
    DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"}));

    private DefaultCellEditor lastSelected;

    @Override
    public Object getCellEditorValue() {

        return lastSelected.getCellEditorValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        if(row == 0) {
            lastSelected = checkbox;
            return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
        lastSelected = other;
        return other.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

}

In this example, the custom CellEditor is actually two Editors, and depending on the row selected, the particular Editor will get the call (both figuratively and literally). I admit that the lastSelected seemed a bit hokey, but I honestly could not find an easier way to know which Editor value to return (as the getCellEditorValue has no args).

To make your Table appear "correct", you're probably going to have to do something with the Renderer as well (because the Renderer may or may not know to show the JComboBox's selected value as the initial value). This depends on how you're initializing the data in the actual table.


For completeness, my original answer is below:

You can add the JComboBox component to the row using addRow on the TableModel as shown here: How to add row in JTable?

See also: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

I think the main issue is you're mixing the idea of Column Editors/Renderers with the actual data that will be stored in each row.

다른 팁

but I want it in only one row:

Override the getCellEditor(...) method to return a specific editor for the given row:

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

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

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

        //  Create the table with default data

        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)
        {
            //  Determine editor to be used by row
            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 );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top