简体   繁体   English

动态将JLabel添加到JPanel

[英]Adding JLabel dynamically to JPanel

I know there are many topics on this subject, but I didn't find my answer. 我知道有很多与此主题有关的主题,但是我没有找到答案。

I'm using NetBeans, so I created all my static components using the Designer. 我正在使用NetBeans,所以我使用设计器创建了所有静态组件。 But, at some point, I need to create dynamically JLabels. 但是,在某些时候,我需要动态创建JLabel。

I have one JFrame which contains 2 JPanels 我有一个包含2个JPanel的JFrame
First one HeaderPanel, we don't care 第一个HeaderPanel,我们不在乎
Second one BodyPanel, has a CardLayout that allows me to navigate between my different screens (which are JPanels). 第二个BodyPanel具有CardLayout,可让我在不同的屏幕(JPanel)之间进行导航。 The screen i'm struggling with is MappingPanel 我正在努力的屏幕是MappingPanel

        try {
        stmt = conn.createStatement();
        String sql;
        sql = "**working select query**";
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()){
            System.out.println(rs.getString("Field1")); //Display the right text in console
            JLabel a = new JLabel(rs.getString("Field1"));
            a.setVisible(true);
            MappingPanel.add(a); 
        }
    } catch (SQLException ex) {
        Logger.getLogger(MyFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
    MappingPanel.revalidate();
    MappingPanel.repaint();
    BodyPanel.revalidate();
    BodyPanel.repaint();
    this.pack();
}         

I can't get text to be displayed. 我无法显示文字。

Thanks a lot for any help 非常感谢您的帮助

If you are displaying a single column of data from your ResultSet, then easiest would be to use a JList. 如果要显示ResultSet中的单列数据,则最简单的方法是使用JList。 You'd create a JList with a DefaultListModel<String> , and then in your while loop above, populate the JList with data by calling .addElement(element) on the model. 您将使用DefaultListModel<String>创建一个JList,然后在上面的while循环中,通过在模型上调用.addElement(element)来用数据填充JList。

Something like: 就像是:

Declared in class: 在课堂上声明:

private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> myList = new JList<>();

... and in some event code: ...以及某些事件代码:

     try {
        stmt = conn.createStatement();
        String sql;
        sql = "**working select query**";
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
           listModel.addElement(rs.getString("Field1"));
        }
     } catch (SQLException ex) {
        Logger.getLogger(ListFun.class.getName()).log(Level.SEVERE, null, ex);
     }

Note that for safest Swing threading, the above code should be done in a background thread such as a SwingWorker, the field1 String should be exposed to the GUI on the Swing event thread such as through a SwingWorker's publish/process method pair. 请注意,对于最安全的Swing线程,以上代码应在诸如SwingWorker之类的后台线程中完成,field1 String应该通过诸如SwingWorker的发布/处理方法对在Swing事件线程上公开给GUI。 You'd also want to use a prepared statement to query the database with. 您还希望使用一条准备好的语句来查询数据库。

The JList of course would be held by one of the JPanels of your GUI. JList当然将由您的GUI的JPanel之一保存。

Note that if you need to display multicolumn data, then I recommend use of a JTable. 请注意,如果需要显示多列数据,则建议使用JTable。 If you absolutely must add JLabels, then a key here is the layout manager of the container JPanel that holds the JLabels (as I'm sure that your search results have already told you). 如果您绝对必须添加JLabels,那么这里的关键是包含JLabels的容器JPanel的布局管理器(因为我确定您的搜索结果已经告诉过您)。 You'd probably want it to be GridLayout(0, 1) or something similar -- which means a GridLayout with one column and a variable number of rows. 您可能希望它是GridLayout(0, 1)或类似的东西-这意味着GridLayout具有一列和可变的行数。 The container JPanel should be held in a JScrollPane. 容器JPanel应该保存在JScrollPane中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM