0

We have pageA which has a Jtable and World_info_object "info" . that table shows data from people (people are in world info). I want to edit "info" so each person has an edit button and also this page has a " +NEW " button . These buttons have action listeners : (edit is almost the same)

 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();


    }
});

AS you can see here is a thread looks for change in boolean confirmed which I make it true when user press Okay or cancel button in personFrame ! Purpose is to getInfo() from personFrame and set it here in PageA (first frame) and it's done by this but this thread makes " Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5 " .

If some one know how to solve this exception or how should I setdata sent from other page just before closing it please tell me... (Or maybe problem is with table)

Exception : https://i.stack.imgur.com/UnZnd.png

** Update

All I want to do is making pageB from pageA and send info to it (Which is done) and then after confirm or closing pageB run function update(); in pageA ! Any Idea ? :)

**

// ALMOST SOLVED PROGRAM:

public class PageA extends 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;
}

}

public class PageB extends 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;


}

}

amir
  • 85
  • 8
  • Can you paste the full exception trace and which line exactly you get the exception? Is it in getInfo that you get exception? – SMA Dec 25 '14 at 09:31
  • @almasshaikh : Exception : http://i.stack.imgur.com/UnZnd.png 0>=0 changes to 6>=6 or 4>=4 each time it is different !? – amir Dec 25 '14 at 09:39
  • It's still difficult to makeout which line from your code is throwing exception as its not full stack trace. – SMA Dec 25 '14 at 09:41
  • i think problem is about AWT threads and my thread ! Because I'm changing a swing data from other swing ! – amir Dec 25 '14 at 09:43
  • All I want to do is making pageB from pageA and sent info to it (Which is done) and then after confirm or closing pageB run function update(); in pageA ! Any Idea ? :) – amir Dec 25 '14 at 09:48
  • 1
    it's difficult to comment without code and full stack trace. – SMA Dec 25 '14 at 09:48
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Dec 25 '14 at 11:05
  • @ANdrewThompson : Thanks I add some codes for better understanding my problem is almost solved :) – amir Dec 25 '14 at 11:27
  • *"Thanks I add some codes.."* 1) Small point, but an MCVE/SSCCE is one Java source code file (that might contain more than one class). 2) Have you read the link about multiple frames yet? It suggests one alternative which would likely fix the problems.. – Andrew Thompson Dec 25 '14 at 11:30
  • @ANdrewThompson :Your code was great =D – amir Dec 25 '14 at 11:56

2 Answers2

0

Your error message says that there was an error in applying your layout, where it expected to find a component but instead found none. This is likely due to improper use of personFrame.dispose(); without updating your layout to account for having one fewer element.

Vitruvie
  • 2,327
  • 18
  • 25
  • What exactly dispose does ? so it's better to first check if this frame even exists ? – amir Dec 25 '14 at 10:03
  • @Germany21 There's your first problem: using `dispose` without knowing what it does ;) – Vitruvie Dec 25 '14 at 10:04
  • @Germany21 The frame exists when you dispose it, but afterwards the frame's container can't display properly because it expects this frame to be part of its layout. – Vitruvie Dec 25 '14 at 10:05
  • Sorry I'm a php programmer in fact I just want to complete this program which is not mine !? I know setVisible(false)l and dispose will vanish the page but don't know how ! – amir Dec 25 '14 at 10:08
0

Okay so I changed some parts :

            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)
             }
         });

And in personPage I add an Event for closing to be listened there ! Here code :

    // 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 !


        }
    });

And this solved AWT problem also updating data ^_^ ( for now :| )

amir
  • 85
  • 8