从另一个类获取 JList 选择值的最佳方法
我正在制作一个数据库管理器,它向您显示一个包含所有表数据的 JTable。想法是在左侧有一个带有表名称的 JList,您可以在其中选择要可视化的表。
所以我有一个类来管理所有表数据,还有一个类用于管理 JList。
JList 类扩展自 JPanel,我创建了一个 getInstance() 方法,这样我就可以创建一个 JList 实例。我还有一个名为 getSelectedValue() 的方法,主要在 JTable 类中调用,用于显示选定的表信息。
但这样我遇到了问题,我进行调试,并在 JList 构造函数中收到错误,这里是代码:
public class DatabaseJList extends JPanel {
private static DatabaseJList instance = null;
private static DefaultListModel listModel = new DefaultListModel();
private static JList list;
public DatabaseJList() {
setLayout(new BorderLayout());
model = new DefaultListModel();
list = new JList(model);
JScrollPane pane = new JScrollPane(list);
Vector<String> vector = new Vector(DatabaseTableManagers.getInstance().getTableNames());
for( int i=0; i<vector.size() ; i ++ )
{
String string = vector.elementAt(i);
model.addElement(string);
}
add(pane, BorderLayout.NORTH);
}
public Object getSelectedObject() {
return list.getSelectedValue();
}
public void setSelectedObject(String str)
{
list.setSelectedValue(str, true);
}
public static DatabaseJList getInstance()
{
if (instance == null)
{
instance = new DatabaseJList();
}
return instance;
}
当第一次调用 getInstance 方法时,它会继续生成“new DatabaseJList();”它进入构造函数并停止,至少这就是我以我糟糕的调试知识所看到的。有什么想法吗?谢谢大家;)
I am making a database manager which shows you a JTable with all the table data. The ideas is to have in the left side a JList with the table names where you can select the table to visualize.
So i have a class where I manage all the table data and so, an another one for the JList.
The JList class extends from JPanel and I created a getInstance() method so I can just create one instance of JList. I also have a method called getSelectedValue() that is mainly called in the JTable class to display the selected table information.
But this way I am having problems, I debug and I get an error in the JList constructor, here os the code:
public class DatabaseJList extends JPanel {
private static DatabaseJList instance = null;
private static DefaultListModel listModel = new DefaultListModel();
private static JList list;
public DatabaseJList() {
setLayout(new BorderLayout());
model = new DefaultListModel();
list = new JList(model);
JScrollPane pane = new JScrollPane(list);
Vector<String> vector = new Vector(DatabaseTableManagers.getInstance().getTableNames());
for( int i=0; i<vector.size() ; i ++ )
{
String string = vector.elementAt(i);
model.addElement(string);
}
add(pane, BorderLayout.NORTH);
}
public Object getSelectedObject() {
return list.getSelectedValue();
}
public void setSelectedObject(String str)
{
list.setSelectedValue(str, true);
}
public static DatabaseJList getInstance()
{
if (instance == null)
{
instance = new DatabaseJList();
}
return instance;
}
When the getInstance method is called for first time and it proceeds to make the "new DatabaseJList();" it goes to the constructor and stops, at less thats what I see with my poor debuging knowledge. Any ideas? Thanks everyone ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎对我来说效果很好。这是我尝试过的(一些小的调整,没什么大不了的)
Seems to work fine for me. Here's what I tried (some minor tweaks, nothing drastic)