problem with setSelectedValue Method for JList
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");
Da开发者_JAVA百科tabaseTableManagers 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);
}
}
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;
}
}
精彩评论