JList 的 setSelectedValue 方法的问题

发布于 2024-11-24 03:14:20 字数 2114 浏览 0 评论 0原文

当我运行应用程序的主类时,我遇到了麻烦。事实是 setSelectedValue 方法不起作用。 主类的代码如下:

DatabaseConnection.getInstance().connect("org.sqlite.JDBC", "jdbc:sqlite:db/Universidad.sqlite");
DatabaseTableManagers managers = DatabaseTableManagers.getInstance();
DataBaseManagerJList.getInstance().setSelectedValue("Alumnos");
system.out.println(DataBaseManagerJList.getInstance().devolver() + "1");
    AlumnosTableManager atm = AlumnosTableManager.getInstance();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "2");
    CarrerasTableManager ctm = CarrerasTableManager.getInstance();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "3");
    managers.add(atm);
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "4");
    managers.add(ctm);
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "5");
    DataBaseManagerJFrame dbmf = new DataBaseManagerJFrame();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "6");
    dbmf.setVisible(true);


}

}

我在控制台中得到以下结果,这表明该方法没有设置我输入的默认值: 空1 空2 空3 空4 空5 null6

jlist 类的代码如下: 公共类 DataBaseManagerJList 扩展 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));
    }
    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 when i run the main class for the application. The fact is that the setSelectedValue method doesn't work.
the code for the main class is the following:

DatabaseConnection.getInstance().connect("org.sqlite.JDBC", "jdbc:sqlite:db/Universidad.sqlite");
DatabaseTableManagers managers = DatabaseTableManagers.getInstance();
DataBaseManagerJList.getInstance().setSelectedValue("Alumnos");
system.out.println(DataBaseManagerJList.getInstance().devolver() + "1");
    AlumnosTableManager atm = AlumnosTableManager.getInstance();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "2");
    CarrerasTableManager ctm = CarrerasTableManager.getInstance();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "3");
    managers.add(atm);
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "4");
    managers.add(ctm);
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "5");
    DataBaseManagerJFrame dbmf = new DataBaseManagerJFrame();
    System.out.println(DataBaseManagerJList.getInstance().devolver() + "6");
    dbmf.setVisible(true);


}

}

and i get the following result in console which shows me that the method doesn't set the default value i've put:
null1
null2
null3
null4
null5
null6

the code for the jlist class is the following:
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));
    }
    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);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谁把谁当真 2024-12-01 03:14:20

您描述的问题通常意味着在列表中找不到该对象。
setSelectedValue() 方法可以调用 object.equals() (在您的情况下为 String.equals()),因此可能的原因是 aux 中包含的字符串的大写方式与您尝试选择的字符串不同。

例如,aux 可能包含字符串“alumnos”而不是“Alumnos”。如果您不关心大小写,则有两种选择(然后将字符串“Alumnos”更改为 aux 中的任何内容),如果您确实关心大小写并且仍然想使用字符串,您可以简单地创建一个扩展 String 并覆盖 equals 方法的 MyString 类,例如:

public class MyString extends String {
    @Override
    public boolean equals(Object obj){
        if (obj instanceof String){
            String obj2 = (String)obj;
            return this.equalsIgnoreCase(obj2);
        }
        return false;
    }
}

另一种选择(也许更好)是为您的列表创建一个包装对象。

public class MyWrapper {
    private String payload;

    public MyWrapper(String payload){
        this.payload = payload;
    }

    @Override
    public String toString(){
        return payload;
    }

    @Override
    public boolean equals(Object obj){
        if (obj instanceof MyWrapper){
            MyWrapper obj2 = (MyWrapper)obj;
            return payload.equalsIgnoreCase(obj2.payload);
        }
        return false;
     }
}

The problem you are describing usually means that the object wasn't found in the list.
The setSelectedValue() method works doing calls to object.equals() (in your case String.equals()), so the probable cause is that the strings contained in aux are capitalized in a different way than the string you are trying to select.

For instace aux may contain the string "alumnos" instead of "Alumnos". There are two alternatives for this, if you don't care about capitalization (then change the string "Alumnos" to whatever is inside aux) if you do care about the capitalization and you still want to use strings, you could simple create a MyString class that extends String and overrides the equals method, something like:

public class MyString extends String {
    @Override
    public boolean equals(Object obj){
        if (obj instanceof String){
            String obj2 = (String)obj;
            return this.equalsIgnoreCase(obj2);
        }
        return false;
    }
}

Another alternative (perhaps a nicer one) is to create a wrapper object for your list.

public class MyWrapper {
    private String payload;

    public MyWrapper(String payload){
        this.payload = payload;
    }

    @Override
    public String toString(){
        return payload;
    }

    @Override
    public boolean equals(Object obj){
        if (obj instanceof MyWrapper){
            MyWrapper obj2 = (MyWrapper)obj;
            return payload.equalsIgnoreCase(obj2.payload);
        }
        return false;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文