如何使JComboBox表编辑器具有普通JComboBox的设计?

时间:2013-06-27 12:16:50

标签: java swing jtable jcombobox tablecelleditor

我在JComboBox中使用了JTable作为编辑器。在图片中,您可以在标有Produs的列中看到它们。我想在网格单元格中使用独立JComboBox的设计,特别是组合框的右侧部分,网格单元格中缺少三角形,因此用户将知道网格单元格是组合框,而无需单击其中一个。如何将JComboBoxIsBackFlush)的设计应用于网格中的JComboBoxes

基本上,我如何根据comboBox2的设计设计comboBox1?感谢。

cropped image

2 个答案:

答案 0 :(得分:5)

使用此完整example作为常用参考框架,请注意ITEM_COL列中未选定单元格的外观是如何归因于default renderer的。独立JComboBox的典型箭头按钮仅在引发单元格编辑器时出现,例如单击单元格或在选择单元格时按空格。您可以在自定义渲染器中添加三角形:

final JComboBox combo = new JComboBox(items);
TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
col.setCellRenderer(new DefaultTableCellRenderer(){

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
        label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
        return label;
    }
});

附录:由于@aterai而出现了一个更完整的例子here

答案 1 :(得分:3)

本着前进的精神,我将发布我之前谈论的解决方案。此解决方案实际上使用comboBox作为渲染器。因此它看起来与任何LAF中的真实comboBox相同。问题是文本被压扁了,所以我更喜欢trashgod的实现。

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 );
        table.getColumnModel().getColumn(1).setCellRenderer(new ComboBoxRenderer() );
    }

    class ComboBoxRenderer extends JComboBox implements TableCellRenderer
    {

        public ComboBoxRenderer()
        {
            setBorder(new EmptyBorder(0, 0, 0, 0));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column)
        {
//          setFocusable(false);
            removeAllItems();
            addItem( value );
            return this;
        }
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}