关于我的代码,我遇到了某种问题,如果对此有任何见解,我将不胜感激。
首先,这里有一些上下文(在下面的代码中):可以在左列的组合框编辑器中选择的主要对象,每个都有自己的数据集,其中包含相同类型的对象,用于以后的比较和操作。右列的编辑器(同样是组合框)更新以显示此数据集。但是,列表中的第一个元素应该作为向所述列表添加新元素的选项,当它被选中时(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 ,似乎无法弄清楚问题所在。这可能很简单,但我在这个问题上已经有一段时间了,我认为这意味着我需要重新审视这个问题,否则我会寻找相当长的时间。
哦,还有这个问题:为什么 JComboBox 上的 itemStateChanged 在更改时会被调用两次?似乎没有解决问题(也许我错了)。
无论如何,感谢您的任何回复!