从其他框架更新数据

时间:2014-12-25 09:29:17

标签: java multithreading swing exception eventqueue

我们有pageA,它有一个Jtable和World_info_object“info”。该表显示来自人的数据(人们在世界信息中)。我想编辑“信息”,这样每个人都有一个编辑按钮,而且这个页面还有一个“+ NEW”按钮。这些按钮有动作监听器:(编辑几乎相同)

 newPerson.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        personFrame=new PersonPage( getInfo() , null );

        Thread closing =new Thread( new Runnable() {

            @Override
            public void run() {

                while(true){
                    if(personFrame.getConfirmed()==true){

                        setWorldInfo(personFrame.getInfo());
                        personFrame.setVisible(false);
                        personFrame.dispose();
                        System.out.println("closed");
                        updateTableData(); // repaint table !

                        break;
                    }
                    System.out.println("open...");
                }   

            }
        }, "closingWindow");




        closing.start();


    }
});

你可以在这里看到一个线程在布尔确认中查找更改,当用户按下Okay或取消personFrame中的按钮时,我将其设为true!目的是从personFrame中获取getInfo()并在PageA(第一帧)中设置它,并且由此完成,但是此线程使“线程中的异常”AWT-EventQueue-0“java.lang.ArrayIndexOutOfBoundsException:5> = 5”

如果有人知道如何解决这个异常,或者我应该如何在关闭之前从其他页面发送setdata请告诉我......(或者问题可能与表格有关)

例外:http://i.stack.imgur.com/UnZnd.png

**更新

我想要做的就是从pageA制作pageB并向其发送信息(已完成)然后在确认或关闭pageB后运行函数update();在pageA!任何的想法 ? :)

**

//几乎已解决的程序:

公共类PageA扩展了JFrame {

private static HashMap<Integer, String>info=new HashMap<Integer,String>();
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {


    info.put(1, "Sofia");
    info.put(2, "XSR");


    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PageA frame = new PageA();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public PageA() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(80, 80, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JButton btnNewButton = new JButton("Open pageB");
    btnNewButton.setBounds(126, 11, 160, 23);
    btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            final PageB pageB=new PageB(info);
            pageB.addWindowListener(new WindowAdapter(){ // add listener to detect 
                 public void windowClosing(WindowEvent e){

                     setInfo(pageB.getInfo());
                     System.out.println("xxxx");

                 }
             });



        }
    });
    contentPane.add(btnNewButton);



}

public void setInfo(HashMap<Integer, String> uinfo){

    this.info=info;
}

}

公共类PageB扩展了JFrame {

private JPanel contentPane;
private HashMap<Integer, String> info;

public PageB(HashMap<Integer, String> info) {

    this.info=info;

    this.info.put(113, "Alfred");
    this.info.put(314, "Alfa");
    this.info.put(13, "Luter");

    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final PageB that=this;

    JButton btnOkayDone = new JButton("Okay , Done");
    btnOkayDone.setBounds(34, 228, 130, 23);
    btnOkayDone.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // add event for closing
            that.dispatchEvent(new WindowEvent(that, WindowEvent.WINDOW_CLOSING));

        }
    });
    contentPane.add(btnOkayDone);

    this.setVisible(true);



}


public HashMap<Integer, String> getInfo(){

    return info;


}

}

2 个答案:

答案 0 :(得分:0)

您的错误消息显示应用您的布局时出错,它希望找到一个组件,但却找不到。这可能是由于personFrame.dispose();使用不当而没有更新您的布局以减少一个元素。

答案 1 :(得分:0)

好的,所以我换了一些部分:

            personFrame=new PersonPage( getInfo() , null );

        personFrame.addWindowListener(new WindowAdapter(){ // close page with listener
             public void windowClosing(WindowEvent e){

                 System.out.println("Closed");
                 setWorldInfo(personFrame.getInfo()); // update data (as before)
                 updateTableData(); // repaint and update frame (as before)
             }
         });

在personPage中我添加了一个关闭的事件,以便在那里收听!这里代码:

    // cancel operation (Also for okay button I add this ** line after update info )

    btnCancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {


            that.dispatchEvent(new WindowEvent(that, WindowEvent.WINDOW_CLOSING)); 
            //** instead of using a boolean I used closing event !


        }
    });

这解决了AWT问题,也更新了数据^ _ ^(现在:|)