gpt4 book ai didi

java - 使用 TableModelListener 设置 JComboBox 编辑器

转载 作者:行者123 更新时间:2023-12-02 02:51:52 25 4
gpt4 key购买 nike

我想使用 TableModelListener 将第二列设置为 JComboBox 编辑器。还想根据第一列中选​​定的组合框更改第二列中组合框的模型。在这里,我实现了一个监听第一列的监听器。

private class TableScheduleListener implements TableModelListener
{
//Listening for data changes.
@Override
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
}

if(column == 1)
{
for(int i = 0; i < createScheduleView.tblSchedule.getRowCount(); i++)
{
createScheduleView.tblSchedule.getCellEditor(i, 2);
}

int col = createScheduleView.tblSchedule.convertColumnIndexToModel(2);

if(col == 2 && createScheduleView.tblSchedule.getRowCount() < 7)
{
DefaultComboBoxModel cbModel = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
createScheduleView.cbBreakStartTime.setModel(cbModel);
}
}
}

但我在想如何在第二列中将 JComboBox 设置为编辑器。设置后是型号吗?任何帮助将不胜感激。

更新

我遵循 https://tips4java.wordpress.com/2009/06/28/combo-box-table-editor/ 中的 camickr 提示但想从不同的类覆盖 TableCellEditor

private class TableScheduleEditor extends JTable
{
// Determine editor to be used by row
@Override
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel(column);

if(modelColumn == 2 && row < 7)
{
DefaultComboBoxModel model = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
createScheduleView.getCbBreakStartTime().setModel(model);
return new DefaultCellEditor(createScheduleView.getCbBreakStartTime());
}

else
return super.getCellEditor(row, column);
};
}

我在想这个类如何注册到我的 JTable 中?我尝试在我的构造函数中注册它 this.createScheduleView.tblSchedule.setCellEditor(new TableScheduleEditor());但它说无法转换为 TableCellEditor,因为它扩展了 JTable。有什么技巧可以做到这一点吗?

最佳答案

Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column

重写 JTablegetCellEditor(...) 方法以返回适当的编辑器。

这是一个帮助您入门的基本示例:

import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);

public TableComboBoxByRow()
{
setLayout( new BorderLayout() );

// Create the editorData to be used for each row

editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );

// 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)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};

JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}

private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

在上面的示例中,“模型”根据正在编辑的行而变化。

在您的情况下,您将需要修改逻辑以根据第一列中的值返回编辑器。

关于java - 使用 TableModelListener 设置 JComboBox 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43695819/

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