JLIST 的 setSelectedValue 方法出错
我在使用 JList 类时遇到问题。我正在从向量获取数据来填充 JList,当我在屏幕上显示该列表时,该列表看起来很好。列表中包含表名,选中的就是要显示的表。所以项目结构是基于List的getSelectedValue()。当我第一次运行程序 getSelectedValue 返回 null 时,所以我尝试使用 setSelectedValue("Alumnos", true) 自己修改它,我得到: 线程“main”中的异常 java.lang.NullPointerException 在 interfaz.DataBaseManagerJList.setSelectedValue(DataBaseManagerJList.java:53)
这没有任何意义,因为当我在屏幕上显示 Alumnos 时,它位于列表中。这是 JList 类代码:
public class DataBaseManagerJList extends JPanel
{
private static final long serialVersionUID = 1L;
private static JList tablas;
DefaultListModel model;
DatabaseTableManagers dtm = DatabaseTableManagers.getInstance();
private static DataBaseManagerJList instance = null;
public static DataBaseManagerJList getInstance()
{
if (instance == null)
{
instance = new DataBaseManagerJList();
}
return instance;
}
public DataBaseManagerJList()
{
model = new DefaultListModel();
ArrayList<String> aux = new ArrayList<String>(dtm.getTableNames());
for(int i =0;i<aux.size();i++)
{
model.addElement(aux.get(i));
System.out.println(aux.get(i));
}
JList tablas = new JList(model);
//tablas.setSelectedValue("Alumnos",true);
add(tablas);
}
public String devolver()
{
return (String) tablas.getSelectedValue();
}
public void setSelectedValue(String name)
{
tablas.setSelectedValue(name, true);
}
}
有什么想法吗?谢谢大家:)
I am having troubles with JList class. I am getting data from a vector to fill the JList and the list looks fine when I show it on screen. The list contains table names,and the selected one is the table to be displayed. So the proyect structure is based on the getSelectedValue() of the List. When I first run the program getSelectedValue returned null, so I tried modifiying it by myself with setSelectedValue("Alumnos", true) and I get this:
Exception in thread "main" java.lang.NullPointerException
at interfaz.DataBaseManagerJList.setSelectedValue(DataBaseManagerJList.java:53)
This makes no sense since Alumnos is in the list when I display it on screen. Here is the JList class code:
public class DataBaseManagerJList extends JPanel
{
private static final long serialVersionUID = 1L;
private static JList tablas;
DefaultListModel model;
DatabaseTableManagers dtm = DatabaseTableManagers.getInstance();
private static DataBaseManagerJList instance = null;
public static DataBaseManagerJList getInstance()
{
if (instance == null)
{
instance = new DataBaseManagerJList();
}
return instance;
}
public DataBaseManagerJList()
{
model = new DefaultListModel();
ArrayList<String> aux = new ArrayList<String>(dtm.getTableNames());
for(int i =0;i<aux.size();i++)
{
model.addElement(aux.get(i));
System.out.println(aux.get(i));
}
JList tablas = new JList(model);
//tablas.setSelectedValue("Alumnos",true);
add(tablas);
}
public String devolver()
{
return (String) tablas.getSelectedValue();
}
public void setSelectedValue(String name)
{
tablas.setSelectedValue(name, true);
}
}
Any ideas? Thanks everyone :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想您模型中的字符串与您尝试选择的字符串相比有空格或其他差异。而不是硬编码一个值。尝试仅从模型中选择第一项。例如:
这样您就可以保证该值位于模型中。 (假设列表不为空)。
另一个优点是校友不再是硬编码值,因此如果它不再是列表中的选项,您不必更改选择它的代码。
I would guess that the String in your model has a space or some other difference than the one you are trying to select. Instead of hardcoding a value. Try just selecting the first item from the model. For example:
That way you are guaranteed that the value is in the model. (Assuming the list is not empty).
Another advantage is Alumnos is no longer a hardcoded value so that if it is not longer an option in your list you don't have to change the code that selects it.
而不是
write 。
您创建本地实例,然后在 setter 中使用类字段,
Instead of
write
You create local instance but then use class field in setter.