开发者

Use a JCheckBox as DefaultCellEditor for my JTable's cells

I am using this function to create a jTable, where some special cells have a jComboBox as editor :

void fillTable_(){
        final JComboBox myEditor = new JComboBox(new String[] {"yes", "no", "maybe"});
        String[][] data = new String[10][2];
            data[0][0] = "0,0";
            data[0][1] = "0,1";
            data[1][0] = "1,0";
            data[1][1] = "1,1";
            data[2][0] = "2,0";
            data[2][1] = "2,1";
            String[] columnNames = {"Nom", "Valeur"};
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            jTable1 = new JTable(model){
                DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);
                @Override
                public TableCellEditor getCellEditor(int row, int column){
                    int modelColumn = convertColumnIndexToModel(column);
                    int modelRow = convertRowIndexToModel(row);
                    if(modelColumn == 1 && model开发者_运维问答Row == 1){
                        return myCellEditor;
                    } else {
                        return super.getCellEditor(row, column);
                    }
                }
            };
    }

But the jTable stays empty not even plain text display. Is there something wrong?


But the JTable stays empty; not even plain text is displayed.

You need a renderer that corresponds to your editor, as suggested in this example that contains both a ValueRenderer and a ValueEditor.

I just need to put a JComboBox in some cells of the JTable:

As discussed in How to Use Tables: Concepts: Editors and Renderers, "a single cell renderer is generally used to draw all of the cells that contain the same type of data."

I specified both row and column, but nothing is happening

Your code appears to work when I add the new JTable to a container.

Use a JCheckBox as DefaultCellEditor for my JTable's cells

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

/** @see http://stackoverflow.com/questions/7200500 */
public class TableTest extends JPanel {

    public TableTest() {
        this.setPreferredSize(new Dimension(320, 240));
        final JComboBox myEditor = new JComboBox(
            new String[]{"yes", "no", "maybe"});
        String[][] data = new String[10][4];
        data[0][0] = "0,0";
        data[0][5] = "0,1";
        data[1][0] = "1,0";
        data[1][6] = "1,1";
        data[2][0] = "2,0";
        data[2][7] = "2,1";
        String[] columnNames = {"Nom", "Valeur"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model) {

            DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                int modelColumn = convertColumnIndexToModel(column);
                int modelRow = convertRowIndexToModel(row);
                if (modelColumn == 1 && modelRow == 1) {
                    return myCellEditor;
                } else {
                    return super.getCellEditor(row, column);
                }
            }
        };
        this.add(table);
    }

    private void display() {
        JFrame f = new JFrame("TableTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableTest().display();
            }
        });
    }
}


1) create JTable, TableModel, JComboBox, then put that together as example from official tutorial

2) for Boolean Component isn't required create own TableCellEditor, JTable by default supporting String, Integer, Double, Boolean, Date and Icon/ImageIcont in the TableCell

another examples for a.m.... here, here from this forum here, here, and here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜