JList 的 setSelectedValue 方法的问题
当我运行应用程序的主类时,我遇到了麻烦。事实是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的问题通常意味着在列表中找不到该对象。
setSelectedValue()
方法可以调用object.equals()
(在您的情况下为String.equals()
),因此可能的原因是 aux 中包含的字符串的大写方式与您尝试选择的字符串不同。例如,
aux
可能包含字符串“alumnos”而不是“Alumnos”。如果您不关心大小写,则有两种选择(然后将字符串“Alumnos”更改为 aux 中的任何内容),如果您确实关心大小写并且仍然想使用字符串,您可以简单地创建一个扩展 String 并覆盖 equals 方法的 MyString 类,例如:另一种选择(也许更好)是为您的列表创建一个包装对象。
The problem you are describing usually means that the object wasn't found in the list.
The
setSelectedValue()
method works doing calls toobject.equals()
(in your caseString.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 insideaux
) 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:Another alternative (perhaps a nicer one) is to create a wrapper object for your list.