简体   繁体   English

在jtable上方绘制jtable单元编辑器

[英]Paint jtable cell editor above jtable

Designing a custom JTable I already know that DefaultCellEditor admits a JComboBox in his constructor. 设计一个自定义的JTable我已经知道DefaultCellEditor在他的构造函数中允许一个JComboBox。 This JComboBox, when clicked to display the list items, shows above the other cells of the JTable. 单击此JComboBox以显示列表项时,该JComboBox显示在JTable的其他单元格上方。 The problem is that I need a more sophisticated behavior as what JComboBox offers, so that I've implemented a JTextField with a JList and a JButton, when the JButton gets clicked (or the user enters text in the JTextField) the elements in the JList become visible. 问题是我需要像JComboBox所提供的那样更复杂的行为,以便在单击JButton(或用户在JTextField中输入文本)时,我实现了带有JList和JButton的JTextField。变得可见。 This 3 elements are in a JPanel. 这3个元素位于JPanel中。 When I try to use this panel as a cell editor (extending AbtractCellEditor and implementing TableCellEditor) the elements in the list show inside the editing cell but I cannot mimic the behavior of the DefaultCellEditor with the combo so that the list elements show above the JTable. 当我尝试将此面板用作单元格编辑器(扩展AbtractCellEditor并实现TableCellEditor)时,列表中的元素显示在编辑单元格内,但是我无法通过组合模仿DefaultCellEditor的行为,因此列表元素显示在JTable上方。

Here I define the custom cell editor: (very short version); 在这里,我定义了自定义单元格编辑器:(非常简短的版本);

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;
}

} }

And this a main program with a jtable with this panel as a cell editor: 这是一个带有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);
    }

}

} }

Can someone help me? 有人能帮我吗?

Thanks!! 谢谢!!

Using a JComboBox as a table editor relies on it being a button that displays a popup component when pressed. 使用JComboBox作为表编辑器依赖于它是一个在按下时显示弹出组件的按钮。 As you have several components in a cell, consider these alternatives: 由于单元中有多个组件,请考虑以下替代方法:

  • Add the components to a panel, as shown here for a group of JRadioButton instances. 组分添加到面板,如图这里为一组的JRadioButton实例。

  • Add the components to a modal dialog, as shown here using JDialog . 将组件添加到模式对话框,如此处所示使用JDialog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM