正如我们所知,该ComboBoxModel
接口用于在女巫中创建一个类,我们可以指定如何将对象集合(模型)与组合框相关联,基本上是通过提供有关如何检索项目和设置当前项目的必要“信息”。通常,我编写了那些声明为成员 a 的类Collection <of a concrete type>
,并且只是将一些功能委托给已实现方法中的集合对象。当所有包含的对象的实际类都是非代理对象时,一切都很好(肯定有 90% 的情况下我们会遇到这种情况),但是这一次面对一个事实,即引用了代理对象,事情就奇怪地出错了。JComboBox 行为出错,因为它无法更改当前选择。
我正在尝试获取更多信息,但是到目前为止,我只知道接口的方法setSelectedItem
,ComboBoxModel
即任何具体类实现的方法在周围有代理对象时都不会调用。这是我的问题:发生了什么,更重要的是,它是否可以修复?
我留下一个例子,准备自己看看。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
/**
*
* @author Administrador
*/
public class AComboBoxWithProxyProblem extends JFrame implements ActionListener
{
ComboBoxModel modelWithProxies = new ItemComboBoxModelWithProxies();
ComboBoxModel modelWithoutProxies = new ItemComboBoxModelWithoutProxies();
public AComboBoxWithProxyProblem()
{
final JComboBox comboBox = new JComboBox();
comboBox.addActionListener( this );
getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.LINE_AXIS));
getContentPane().add(comboBox);
JRadioButton btnProxy = new JRadioButton("Proxy model");
btnProxy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
comboBox.setModel(modelWithProxies);
comboBox.setSelectedIndex(0);
}
});
getContentPane().add(btnProxy);
JRadioButton btnNoProxy = new JRadioButton("Non Proxy model");
btnNoProxy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
comboBox.setModel(modelWithoutProxies);
comboBox.setSelectedIndex(0);
}
});
getContentPane().add(btnNoProxy);
ButtonGroup group = new ButtonGroup();
group.add(btnProxy);
group.add(btnNoProxy);
setTitle("Mmmm...");
}
@Override
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox = (JComboBox)e.getSource();
Item item = (Item)comboBox.getSelectedItem();
System.out.println("[actionPerformed] - " + item.getId() + " : " + item.getDescription() );
}
interface ItemInterface
{
String getDescription();
int getId();
@Override
String toString();
}
class Item implements AComboBoxWithProxyProblem.ItemInterface
{
private int id;
private String description;
public Item(int id, String description)
{
this.id = id;
this.description = description;
}
@Override
public int getId()
{
return id;
}
@Override
public String getDescription()
{
return description;
}
@Override
public String toString()
{
return description;
}
}
private class ItemComboBoxModelWithoutProxies extends AbstractListModel implements ComboBoxModel
{
List<ItemInterface> foos;
ItemInterface selected;
public ItemComboBoxModelWithoutProxies()
{
foos = new ArrayList<> ();
foos.add(new Item(1,"One"));
foos.add(new Item(2,"Two"));
foos.add(new Item(3,"Three"));
}
@Override
public Object getSelectedItem()
{
return selected;
}
@Override
public void setSelectedItem(Object tournament)
{
System.out.println("[setSelectedItem] " + tournament);
selected = (ItemInterface) tournament;
}
@Override
public int getSize()
{
return this.foos.size();
}
@Override
public Object getElementAt(int i)
{
return this.foos.get(i);
}
}
private class ItemComboBoxModelWithProxies extends AbstractListModel implements ComboBoxModel
{
List<ItemInterface> foos;
Object selected;
public ItemComboBoxModelWithProxies()
{
foos = new ArrayList<> ();
ItemInterface item;
item = (ItemInterface) Proxy.newProxyInstance(Item.class.getClassLoader(),
Item.class.getInterfaces(),
new ItemInvocationHandler (new Item(1,"One")));
foos.add(item);
item = (ItemInterface) Proxy.newProxyInstance(Item.class.getClassLoader(),
Item.class.getInterfaces(),
new ItemInvocationHandler (new Item(2,"Two")));
foos.add(item);
item = (ItemInterface) Proxy.newProxyInstance(Item.class.getClassLoader(),
Item.class.getInterfaces(),
new ItemInvocationHandler (new Item(3,"Three")));
foos.add(item);
}
@Override
public Object getSelectedItem()
{
return selected;
}
@Override
public void setSelectedItem(Object tournament)
{
System.out.println("[setSelectedItem] " + tournament);
selected = (ItemInterface) tournament;
}
@Override
public int getSize()
{
return this.foos.size();
}
@Override
public Object getElementAt(int i)
{
return this.foos.get(i);
}
private class ItemInvocationHandler implements InvocationHandler {
Item item;
public ItemInvocationHandler(Item item)
{
this.item = item;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
return method.invoke(this.item, args);
}
}
}
public static void main(String[] args)
{
JFrame frame = new AComboBoxWithProxyProblem();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible( true );
}
}
好吧,仅此而已!
谢谢!!
胜利者。