gpt4 book ai didi

java - Action 事件和项目事件 : dealing with events on a dynamic JComboBox

转载 作者:行者123 更新时间:2023-12-01 14:29:55 28 4
gpt4 key购买 nike

我的代码遇到了一些问题,如果您对此有任何见解,我将不胜感激。

首先,这是一些上下文(关于以下代码):主要对象可以在左列的组合框编辑器中选择,每个对象都有自己的数据集,其中包含相同类型的对象,用于以后的比较和操作。右列的编辑器(再次组合框)更新以显示该数据集。然而,列表中的第一个元素应该作为向所述列表添加新元素的选项,并且当选择它时(actionEvent),将创建新的数据对象,但是一旦组合框被重新创建,选择后,它会创建并显示另一个数据对象。

这段代码代表了我的应用程序中产生问题的部分的基本结构,我已经将它(尽可能地)修剪为尽可能多的 SSCCE 格式的基本结构。有些对象仍然使用它们的法语名称,如果您不明白它们的用途,我可以更改它们。上一段解释了很多,但如果您想要精确,我很乐意继续。

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;


public class TestApp extends JFrame {

private JPanel contentPane;
private CustomTable table;
private JComboBox<GeneralPrimaryObject> selecteurObjets = new JComboBox<GeneralPrimaryObject>();

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestApp frame = new TestApp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public TestApp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(42, 46, 350, 103);
contentPane.add(scrollPane);

selecteurObjets.addItem(new FirstSubClass());

table = new CustomTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{null, ""},
{null, null},
{null, null},
{null, null},
{null, null},
},
new String[] {
"Primary object", "Sublist from object"
}
));
scrollPane.setViewportView(table);
table.getColumnModel().getColumn(1).setPreferredWidth(105);
table.setRowHeight(15);
TableColumn colonneObjets = table.getColumnModel().getColumn(0);
colonneObjets.setCellEditor(new DefaultCellEditor(selecteurObjets));
table.getModel().addTableModelListener(new TableModelListener(){
private static final int COLONNE_OBJETS = 0;
private GeneralPrimaryObject previousObject;
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int col = e.getColumn();
JComboBox<GeneralPrimaryObject> editor = new JComboBox<GeneralPrimaryObject>();
GeneralPrimaryObject confirmedObject;
Object value;
ArrayList<GeneralPrimaryObject> objects;
//new object standing in for the "New data set..." item
FirstSubClass newObjectStandIn = new FirstSubClass();

newObjectStandIn.setName("New data set...");
previousObject = newObjectStandIn;
TableModel modele = (TableModel)e.getSource();
value = modele.getValueAt(row, col);
if(col == COLONNE_OBJETS && value != null){
confirmedObject = (GeneralPrimaryObject)value;
objects = confirmedObject.getData();
for(GeneralPrimaryObject o : objects){
System.out.println("Fuck");
editor.addItem(o);
}
editor.addItem(newObjectStandIn);
editor.addItemListener(new ItemListener(){

@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){

}
}

});

editor.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {

JComboBox<GeneralPrimaryObject> source = (JComboBox<GeneralPrimaryObject>) e.getSource();

GeneralPrimaryObject object = null, objetSelectionne = (GeneralPrimaryObject)source.getSelectedItem();
String nom = "Data set " + (source.getItemCount());
previousObject.equals(objetSelectionne);




if(source.getSelectedIndex() == 0){
switch(objetSelectionne.getType()){
case FIRSTSUB:
object = new FirstSubClass();
object.setName(nom);
break;
case SECONDSUB:
object = new SecondSubClass();
object.setName(nom);
break;
default:
break;

}



}
if(object != null){
System.out.println("Flag1");
source.addItem(object);
source.setSelectedItem(object);

}
}

});
table.addEditor(editor, row);
}

}

});
}
private class CustomTable extends JTable{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<TableCellEditor> editors = new ArrayList<TableCellEditor>(8);

public CustomTable(){
super();
editors.ensureCapacity(8);
for (int i = 0; i < 8; i++) {
editors.add(null);
}
}

/*@Override
public TableCellRenderer getCellRenderer(int row, int column){
if(row == 1 && editeurs.size() > row && editeurs.get(row) != null) {
return editeurs.get(column).;
}
return super.getCellRenderer(row, column);
}
*/

@Override
public TableCellEditor getCellEditor(int row, int column){

int modelColumn = convertColumnIndexToModel( column );

if (modelColumn == 1 && row < 8){
return editors.get(row);
}else{
return super.getCellEditor(row, column);
}
}

public void addEditor(JComboBox<GeneralPrimaryObject> newEditor, int row){
editors.set(row, new DefaultCellEditor(newEditor));

this.repaint();
}
}
private abstract class GeneralPrimaryObject{
protected String name;
protected ObjectType type;
private ArrayList<GeneralPrimaryObject> data = new ArrayList<GeneralPrimaryObject>();

private GeneralPrimaryObject(){

}

public abstract String getName();
public abstract void setName(String name);
public abstract ObjectType getType();

public ArrayList<GeneralPrimaryObject> getData(){
return data;
}

public String toString(){
return this.name;
}
}
private class FirstSubClass extends GeneralPrimaryObject{
public FirstSubClass(){
super();
setName("SubclassObject");
this.type = ObjectType.FIRSTSUB;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {

this.name = name;
}
@Override
public ObjectType getType() {

return this.type;
}

}
private class SecondSubClass extends GeneralPrimaryObject{
public SecondSubClass(){
super();
setName("OtherSubclassObject");
this.type = ObjectType.SECONDSUB;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {

this.name = name;
}
@Override
public ObjectType getType() {

return this.type;
}

}
private enum ObjectType{
FIRSTSUB, SECONDSUB;
}
}

我一直在使用 ItemListeners 和 ActionListeners,但似乎无法弄清楚问题是什么。这可能很简单,但我已经解决这个问题很长一段时间了,我认为这意味着我需要重新审视这个问题,否则我将寻找很长一段时间。

哦,还有这个问题:Why is itemStateChanged on JComboBox is called twice when changed?似乎没有解决问题(也许我错了)。

无论如何,感谢您的回复!

最佳答案

the first element in the list is supposed to stand in as the option to add a new element to said list, and when it is selected (actionEvent),

不确定我是否理解这个问题,但我建议您不要在组合框中使用 ActionListener(或 ItemListener)。

相反,我会尝试将 TableModelListener 添加到 TableModel。当选择“新数据集...”时,模型将被更新。因此,在 TableModelListener 中,您可以创建下一个数据集并将该数据集添加到编辑器中。

关于java - Action 事件和项目事件 : dealing with events on a dynamic JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16931103/

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