gpt4 book ai didi

java - JTable 的特定行中的 Celleditor (JComboBox)

转载 作者:行者123 更新时间:2023-12-01 07:33:01 25 4
gpt4 key购买 nike

我不知道如何在特定行中设置 jcombobox...现在我已经为所有行设置了这个 jcombobox,但我希望它只在一行中:

JComboBox cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("CLICCA PER LE DATE");
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);

最佳答案

but I want it in only one row:

重写 getCellEditor(...) 方法以返回给定行的特定编辑器:

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

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

关于java - JTable 的特定行中的 Celleditor (JComboBox),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com