在jtable上面绘制jtable单元格编辑器

时间:2013-11-29 18:29:10

标签: java swing jtable

设计自定义JTable我已经知道DefaultCellEditor在他的构造函数中允许JComboBox。当单击以显示列表项时,此JComboBox显示在JTable的其他单元格上方。问题是我需要一个比JComboBox更复杂的行为,所以当JButton被点击(或者用户在JTextField中输入文本)JList中的元素时,我已经用JList和JButton实现了一个JTextField变得可见。这3个元素在JPanel中。当我尝试将此面板用作单元格编辑器(扩展AbtractCellEditor并实现TableCellEditor)时,列表中的元素显示在编辑单元格内,但我无法使用组合模仿DefaultCellEditor的行为,以便列表元素显示在JTable上方。

这里我定义了自定义单元格编辑器:(非常短的版本);

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

public class CustomCellEditor extends AbstractCellEditor implements     TableCellEditor {
private JList list;
private JButton button;
private JTextField editor;
private JPanel mainPanel;

public CustomCellEditor() {
    list = new JList(new String[] { "One", "Two", "Three" });
    editor = new JTextField();
    button = new JButton("Click me ");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            list.setVisible(!list.isVisible());
        }
    });

    JPanel auxPanel = new JPanel(new BorderLayout());
    auxPanel.add(editor, BorderLayout.CENTER);
    auxPanel.add(button, BorderLayout.EAST);
    mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(auxPanel, BorderLayout.NORTH);
    mainPanel.add(list, BorderLayout.CENTER);
}
@Override
public Object getCellEditorValue() {
    return editor.getText();
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    return mainPanel;
}

}

这是一个带有jtable的主程序,该面板作为单元格编辑器:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class CustomTable {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            CustomTable instance = new CustomTable();
            instance.createAndShowUI();
        }
    });
}

private void createAndShowUI() {       
    JTable table = new JTable(new CustomTableModel());
    //So that I can see the contents of the list when edited
    table.setRowHeight(60);
    TableColumn editableColumn = table.getColumnModel().getColumn(0);
    editableColumn.setPreferredWidth(250);
    editableColumn.setCellEditor(new CustomCellEditor());
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(table, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);     
}

private class CustomTableModel extends AbstractTableModel   {
    private final String[] columnNames = {"Editable column", "Other column"};

    private final Object[][] data = {
        {"Ricardo", "Mr."},
        {"Josefina", "Ms."}
    };

            @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (0 == col)
            return true;
        return false;
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

}

}

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用JComboBox作为表编辑器依赖于它是一个按下按钮时显示弹出组件的按钮。由于您在单元格中有多个组件,请考虑以下替代方法:

  • 将组件添加到面板,如JRadioButtonJDialog个实例所示{。}}。

  • 使用{{1}}将组件添加到模式对话框中,如here所示。