【问题标题】:ComboBox as a CellRenderer of JTableComboBox 作为 JTable 的 CellRenderer
【发布时间】:2014-06-27 20:04:43
【问题描述】:

我想画几个 JTable,每个都有一个 ComboBox 作为单元格渲染器。每个表中的每个单元格都代表一个数据项列表,我希望每次单击 ComboBox 箭头时都显示该列表。我的问题是 ComboBox 对鼠标点击没有反应,即没有出现下拉列表。

如何激活投递箱?

我在我的 GUI 和代码的图片下方添加了。

我是 java.swing 的新手。任何帮助将不胜感激。

// 借鉴how to add checkbox and combobox in table cell?的部分代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class MyGUI extends JPanel {
    private int _nTables;
    private int _nRows; 
    private int _nCols;
    private String[] _tableInfo;
    private String[][][][] _clusters; 
    private String[][][][] _clustersIntersections;

    public MyGUI(int nTables, int nRows, int nCols, String[] tableInfo, 
            String[][][][] clusters, 
            String[][][][] clustersIntersections) {
        super();
        _nTables = nTables;
        _nRows = nRows;
        _nCols = nCols;
        _clusters = clusters;
        _clustersIntersections = clustersIntersections;

        GridLayout layout = new GridLayout(0, 2);
        layout.setHgap(30);
        layout.setVgap(30);
        setLayout(layout); 

        for(int t=0; t<_nTables; t++){
            JPanel innerPanel = new JPanel(new BorderLayout());
            innerPanel.setBorder(new LineBorder(Color.black));
            JLabel label = new JLabel(tableInfo[t]);
            label.setOpaque(true);
            label.setBackground(Color.WHITE);
            innerPanel.add(label, BorderLayout.PAGE_START);

            JTable table = new JTable(new MyModel(_nRows, _nCols, 
                                              _clusters[t], 
                                              _clustersIntersections[t]));
            table.setBorder(new LineBorder(Color.black));
            table.setMinimumSize(new Dimension(300,80));    
            table.setMaximumSize(new Dimension(300,80));    
            table.setRowHeight(36);         
            table.setFillsViewportHeight(true);
            table.setCellSelectionEnabled(true);

            for(int c=0; c<nCols; c++){
                table.getColumnModel().getColumn(c).setCellRenderer(new ComboBoxCellRenderer());
            }
            innerPanel.add(table, BorderLayout.PAGE_END);

            add(innerPanel);            
        }       
    }

    class MyModel extends AbstractTableModel{   
        int _nRows;
        int _nCols;
        String[][][] _clusters; 
        String[][][] _clustersIntersections;    

        MyModel(int nRows, int nCols, 
                String[][][] clusters, 
                String[][][] clustersIntersections){
            _nRows = nRows;
            _nCols = nCols;
            _clusters = clusters;
            _clustersIntersections = clustersIntersections;
        }

        public int getColumnCount() {
            return _nCols;
        }

        public int getRowCount() {
            return _nRows;
        }

        public Object getValueAt(int row, int col) {
            return _clusters[row][col];
        }

        public Object getAdditionalInfo(int row, int col) {
            return _clustersIntersections[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
    }   

    class ComboBoxPanel extends JPanel {
        protected JComboBox<String> comboBox;

        public ComboBoxPanel() {
            super();
            comboBox = new JComboBox<String>() {
            @Override 
            public Dimension getPreferredSize() {
                Dimension d = super.getPreferredSize();
                return new Dimension(80, d.height);
            }
        };
        setOpaque(true);
        comboBox.setEnabled(true);
        comboBox.setEditable(false);
        add(comboBox);
      }
    }

    class ComboBoxCellRenderer extends ComboBoxPanel
                                                  implements TableCellRenderer {
        public ComboBoxCellRenderer() {
            super();
            setName("Table.cellRenderer");
        }

        @Override 
        public Component getTableCellRendererComponent(JTable table, Object value, 
                                                       boolean isSelected,
                                                       boolean hasFocus, 
                                                       int row, int column) {
            setBackground(isSelected?table.getSelectionBackground()
                                                        :table.getBackground());

            if(value!=null) {
                String[] clustersList = (String[]) value;
                comboBox.removeAllItems();
                for(String cluster:clustersList){
                    comboBox.addItem(cluster);
                }               
                System.out.print("Items count = "+comboBox.getItemCount());             
                System.out.print("    Items: {");
                int count = comboBox.getItemCount();
                for(int i = 0; i<count; i++){
                    System.out.print(comboBox.getItemAt(i)+" ");
                }
                System.out.print("}");              
                System.out.println("");
            }

            if(value!=null) {
                comboBox.setSelectedIndex(0);
            }
            return this;
        }
    }   

    public void draw(){

    }   

    public static void main(String[] args) {
        int nTables = 4;
        int nRows = 2;
        int nCols = 2; 
        String[] tableInfo = new String[nTables];
        String[][][][] clusters = new String[nTables][nRows][nCols][];
        String[][][][] clustersIntersections = new String[nTables][nRows][nCols][];
        for(int t=0; t<nTables; t++){
            tableInfo[t] = new String("   Info");
            for(int row=0; row<nRows; row++){
                for(int col=0; col<nCols; col++){
                    clusters[t][row][col] = 
                    new String[]{"   "+new Integer(t).toString(), 
                                  new Integer(row).toString(), 
                                  new Integer(col).toString()};

                    clustersIntersections[t][row][col] = 
                            new String[]{new Integer(t).toString(), 
                                          new Integer(row).toString(), 
                                          new Integer(col).toString()};
                }
            }
        }       
        //Create and set up the window.
        JFrame frame = new JFrame("MyGUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        //Create and set up the content pane.       
        MyGUI gui = new MyGUI(nTables, nRows, nCols, tableInfo, clusters, clustersIntersections);
        gui.setOpaque(true); //content panes must be opaque

        JScrollPane scrollPane = new JScrollPane(gui);
        frame.setContentPane(scrollPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}

【问题讨论】:

    标签: java swing jtable jcombobox


    【解决方案1】:

    默认单元格不可编辑。

    您需要覆盖自定义模型的 isCellEditable(...) 方法以返回 true 以使单元格可编辑。

    【讨论】:

    • 我想查看 ComboBox 项目的列表。我不想编辑单元格。
    • 查看列表的唯一方法是当单元格处于编辑模式时。如果无法更改值,查看列表有什么意义?
    • 我只想以这种形式显示我的数据。我不需要编辑它。
    • 那么就不需要下拉组合框了。表单的想法是显示数据,或者如果您可以更改数据,则显示下拉列表以便可以更改。无论如何,JTable 使用渲染器在单元格中显示数据。渲染器不是真正的组件,因此您不能像真正的组合框一样单击它。
    • 当 JTable 单元格不可编辑(单元格渲染器是 ComboBox)时,我在第一条消息中引用的链接中的代码有效。所以,我想要的是可以实现的,我就是想不通为什么我的代码不起作用。
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2011-05-31
    • 2014-10-12
    • 2015-01-13
    • 2013-09-29
    • 2011-06-05
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多