2

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

本质上,我如何comboBox2根据设计进行设计comboBox1?谢谢。

裁剪图像

4

2 回答 2

5

将此完整示例用作通用参考框架,请注意ITEM_COL列中未选择单元格的外观是如何归因于默认渲染器的。典型的独立箭头按钮JComboBox仅在调用单元格的编辑器时出现,例如通过单击单元格或Space在选择单元格时按下。您可以在自定义渲染器中添加三角形:

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

于 2013-06-27T16:19:34.190 回答
3

本着前进的精神,我将发布我之前谈到的解决方案。该解决方案实际上使用组合框作为渲染器。所以它看起来与任何 LAF 中的真实组合框相同。问题是文本被压扁了,所以我更喜欢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);
    }
}
于 2013-06-27T17:02:37.227 回答