0

在过去的两天里,我尝试了很多想法,但我无法刷新框架:/我连接到我的数据库并进行选择查询......在我的控制台窗口中,我在我的表单上也看到了 4 个 ID-s。 .. 在我的数据库中创建另一个数据后,在我的控制台窗口中我看到 5 个 ID-s 但在我的表格 4 上......如果我把frame.setvisible按钮放在另一个表格上......

这是我的课:

JLabel timeLabel;


private JPanel contentPane;
 private ArrayList<JLabel> lbllist= new ArrayList<JLabel>();
 private ArrayList<JButton> btnlist= new ArrayList<JButton>();
static int brojac = 0;
private JLabel lblNewLabel;
private JButton btnRefresh;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {


                Mainframe = new Main();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
           //constructor (I need every time to see data from the database)
               public Main() {


    getContentPane().setLayout(null);

    lblNewLabel = new JLabel("New label");
    lblNewLabel.setBounds(0, 185, 46, 14);
    getContentPane().add(lblNewLabel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    getContentPane().removeAll();
    btnRefresh = new JButton("Refresh");
    btnRefresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            do_btnRefresh_actionPerformed(arg0);
        }
    });
    btnRefresh.setBounds(335, 228, 89, 23);
    contentPane.add(btnRefresh);

    EntityManager em = Connection.getEmf().createEntityManager();
    TypedQuery<Entity> query = em.createQuery(
            "select n from Entityn where finished= 0",
            PokusajNarudzbe.class);
    List<Entity> resultList = query.getResultList();
    int a = 20;
    for (Entitypokusaj entity: resultList) {

        System.err.println(entity.getName());//here i see all data (but not
                                                   //in the form

        JLabel list= new JLabel(entity.getBrojName());
        lista.setBounds(10, a, 200, 20);
        lista.setText(entity.getNarudzbaID() + " "
                + entity.Name() + " ";
        lbllist.add(list);
        getContentPane().add(list);
        list.setVisible(true);
        list.repaint();
        list.revalidate();
        JButton btn= new JButton(entity.getName());
        btn.setBounds(500, a, 200, 20);
        btn.setText("Gotovo");
        btnlist.add(btn);
        getContentPane().add(btn);
        a += 20;
        gumb.setVisible(true);
        gumb.repaint();
        gumb.revalidate();
    }

    getContentPane().repaint();
    resultList.clear();
    em.clear();
    em.close();

}
`               protected void do_btnRefresh_actionPerformed(ActionEvent arg0) {
    Main frame1 = new Main();
    SwingUtilities.updateComponentTreeUI(frame1);
    frame1.invalidate();
    frame1.validate();
    frame1.repaint();
//      frame1.revalidate();
//      frame1.setVisible(true); (if I uncomment this another form apears...)

}


@Override
public int read(CharBuffer arg0) throws IOException {
    // TODO Auto-generated method stub
    return 0;
}
}

我能做些什么?

4

1 回答 1

1

您不应该在构造函数中从数据库中获取数据。这样您就无法更新表单 - 它仅在创建时加载数据。将数据获取逻辑移至单独的方法并在您的按钮单击处理程序中调用它。

您现在拥有的只会创建新表单,不会更新原始表单。这就是为什么如果您取消注释setVisible另一个表单打开的部分。

于 2013-05-12T14:11:13.940 回答