【问题标题】:Update data from other frame从其他帧更新数据
【发布时间】:2015-02-23 02:32:07
【问题描述】:

我们的 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();


    }
});

如您所见,这里有一个线程正在寻找布尔值的变化,当用户在 personFrame 中按下“确定”或取消按钮时,我将其变为真!目的是从 personFrame 中获取信息()并将其设置在 PageA (第一帧)中,并通过 this 完成,但此线程会产生“线程中的异常”AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException: 5 >= 5”。

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

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

** 更新

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

**

// 几乎解决的程序:

公共类 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;


}

}

【问题讨论】:

  • 您能否粘贴完整的异常跟踪以及您获得异常的具体哪一行?是在 getInfo 中得到异常吗?
  • @almasshaikh : 例外 : i.stack.imgur.com/UnZnd.png 0>=0 每次都不同时更改为 6>=6 或 4>=4 !?
  • 仍然很难确定代码中的哪一行抛出异常,因为它不是完整的堆栈跟踪。
  • 我认为问题在于 AWT 线程和我的线程!因为我正在改变其他挥杆的挥杆数据!
  • 没有代码和完整的堆栈跟踪很难评论。

标签: java multithreading swing exception eventqueue


【解决方案1】:

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

【讨论】:

  • dispose 到底是做什么的?所以最好先检查一下这个框架是否存在?
  • @Germany21 你的第一个问题是:使用dispose 却不知道它的作用;)
  • @Germany21 框架在您处置时存在,但之后框架的容器无法正常显示,因为它希望此框架成为其布局的一部分。
  • 对不起我是一个php程序员其实我只是想完成这个不是我的程序!?我知道 setVisible(false)l 和 dispose 会消失页面,但不知道如何!
【解决方案2】:

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

            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 问题也更新了数据 ^_^(现在:|)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多