JLIST 的 setSelectedValue 方法出错

发布于 2024-11-19 12:27:55 字数 1439 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日落 2024-11-26 12:27:55

我猜想您模型中的字符串与您尝试选择的字符串相比有空格或其他差异。而不是硬编码一个值。尝试仅从模型中选择第一项。例如:

 JList tablas = new JList(model);    
 tablas.setSelectedValue(aux.get(0), true);
 add(tablas);

这样您就可以保证该值位于模型中。 (假设列表不为空)。

另一个优点是校友不再是硬编码值,因此如果它不再是列表中的选项,您不必更改选择它的代码。

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:

 JList tablas = new JList(model);    
 tablas.setSelectedValue(aux.get(0), true);
 add(tablas);

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.

滥情稳全场 2024-11-26 12:27:55

而不是

JList tablas = new JList(model);

write 。

tablas = new JList(model);

您创建本地实例,然后在 setter 中使用类字段,

Instead of

JList tablas = new JList(model);

write

tablas = new JList(model);

You create local instance but then use class field in setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文