从另一个类获取 JList 选择值的最佳方法

发布于 2024-11-16 21:22:02 字数 1387 浏览 4 评论 0原文

我正在制作一个数据库管理器,它向您显示一个包含所有表数据的 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 技术交流群。

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

发布评论

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

评论(1

玉环 2024-11-23 21:22:02

似乎对我来说效果很好。这是我尝试过的(一些小的调整,没什么大不了的)

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());
        DefaultListModel model = new DefaultListModel();

        list = new JList(model);
        JScrollPane pane = new JScrollPane(list);

        Vector<String> vector = new Vector<String>();
        vector.add("test1");
        vector.add("test2");
        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;

    }

    public static void main(String a[])
    {
        DatabaseJList list = DatabaseJList.getInstance();
        JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.getContentPane().add(list);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

Seems to work fine for me. Here's what I tried (some minor tweaks, nothing drastic)

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());
        DefaultListModel model = new DefaultListModel();

        list = new JList(model);
        JScrollPane pane = new JScrollPane(list);

        Vector<String> vector = new Vector<String>();
        vector.add("test1");
        vector.add("test2");
        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;

    }

    public static void main(String a[])
    {
        DatabaseJList list = DatabaseJList.getInstance();
        JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.getContentPane().add(list);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

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