按钮单击时重新加载/刷新JFrame窗口

时间:2013-05-12 14:01:45

标签: java swing arraylist jbutton jlabel

我在最近两天尝试了很多想法,我只是无法刷新框架:/ 我在我的数据库上连接并进行选择查询...在我的控制台窗口中,我看到4个ID-s在我的表单上...在我的数据库中创建另一个数据后,在我的控制台窗口中我看到5个ID但是在我的表格上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;
}
}

我该怎么办?

1 个答案:

答案 0 :(得分:1)

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

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