Help with Actionlisteners


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help with Actionlisteners

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Help with Actionlisteners

    Okay I'm having a problem with this code. I am using a JDesktop pane with 3 Jinternal Frames. I would like to enable the top internal frame so that When I input data it goes into a table which is the third internal frame. same with the 2nd internal frame.

    Now I have public variables such as public buttons so I first thought I would be able to use these variables throughout the entire class, However I am doing something wrong and the internal frames are not getting the data from each other. Can someone help.

    Here is one of the problems I am having.

    I will include the entire program if anyone wishes to view it does compile and work but not the way that I want it to, Any suggestions or help would be appreciated, thanks!

    /*Action Performed, I set Object Source equal to e.getsource, when the btnEnter is clicked I want
    it to check if it the text boxes in the first frame are blank if not, then output the data and convert
    to strings and then put that data into a table. I cannot figure what is wrong.*/

    public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (btnEnter == source) {
    if (txtname.getText().equals("") || txtinit.getText().equals("") || txtlast.getText().equals("") || txtareacode.getText().equals("") ||
    txtfirstphone.getText().equals("") || txtsecphone.getText().equals("") || txtadress.getText().equals("") ||
    txtcity.getText().equals("") || txtstate.getText().equals("") || txtzip.getText().equals("") ) {
    JOptionPane dialog = new JOptionPane();
    dialog.showConfirmDialog( this, "Error, Need to complete all fields!",
    "Warning", JOptionPane.DEFAULT_OPTION,
    JOptionPane.WARNING_MESSAGE, null );
    }else{
    stfirst = txtname.getText();
    stmiddle = txtinit.getText();
    stlast = txtlast.getText();
    stareacode = txtareacode.getText();
    stfirstphone = txtfirstphone.getText();
    stsecphone = txtsecphone.getText();
    stadress = txtadress.getText();
    stcity = txtcity.getText();
    ststate = txtstate.getText();
    stzip = txtzip.getText();

    stphone = "(" + stareacode + ")" + stfirstphone + "-" + stsecphone;
    System.out.println(stphone);
    stname = txtname.getText() + "/" + stmiddle + "/" + stlast;
    System.out.println(stname);



    Object[][] data = {
    {stradio, stname, stphone, stadress, stzip},
    {stradio, stname, stphone, stadress, stzip},
    {stradio, stname, stphone, stadress, stzip},
    {stradio, stname, stphone, stadress, stzip},
    {stradio, stname, stphone, stadress, stzip}
    };

    txtname.setText(" ");
    txtinit.setText(" ");
    txtlast.setText(" ");
    txtareacode.setText(" ");
    txtfirstphone.setText(" ");
    txtsecphone.setText(" ");
    txtadress.setText(" ");
    txtcity.setText(" ");
    txtstate.setText(" ");
    txtzip.setText(" ");
    }
    }
    if (btnExit == source){
    JOptionPane dialog = new JOptionPane();
    int n = dialog.showConfirmDialog(this,"Do you really want to exit?",
    "Warning!",JOptionPane.YES_NO_OPTION,
    JOptionPane.WARNING_MESSAGE);
    if ( n == JOptionPane.YES_OPTION) {
    System.exit(0);
    }else if (n == JOptionPane.NO_OPTION){
    }
    } // if button exit is clicked


    }
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    How about having an ArrayList in the main frame (where the JDesktopPane is)
    that you add each new JInternalFrame to as the are created/displayed,
    and include this list as a parameter in each of the internalFrames.

    You could then have a piece of code in the internalFrames like:

    Code:
    public class TheFirst extends JInternalFrame {
      ArrayList iFrameList=null;
      public TheFirst (ArrayList iFrameList) {
        this.iFrameList=iFrameList;
      }
      .
      .
      // this is in internalFrame TheFirst when it wants to communicate 
      // something to internalFrame TheThird, no matter how many
      // instances that exists of TheThird...
      .
      .
      for (int i=0; i<iFrameList.size(); i++) {
        if (iFrameList.get(i) instanceof TheThird) { 
          TheThird tt=(TheThird)iFrameList.get(i);
          tt.setAValueOfSomeSort(somethng);
        }
      }
    I peril of stating the obvious: TheThird will need a method like:
    Code:
    public void setAValueOfSomeSort(SomeClass something) {
    .
    .
    }
    eschew obfuscation

  3. #3
    Join Date
    Sep 2004
    Posts
    103
    I understand the first half of the code but I don't quite understand thsi for loop.

    for (int i=0; i<iFrameList.size(); i++) {
    // sets an int to 0, then sets i < the fraomlist size, then it continues on through the loop
    if (iFrameList.get(i) instanceof TheThird) {
    // If the framelist gets value of i, then it gets what? I don't understand instance of?

    TheThird tt=(TheThird)iFrameList.get(i);
    // I don't understand the value of ThThird, it equals a new iframelist?

    tt.setAValueOfSomeSort(somethng);



    public void setAValueOfSomeSort(SomeClass something) {
    .
    .
    }

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Loops & stuff

    Code:
    // read this first like like:
    // start with the int i=0; keep on while i is less than the number of elements 
    // in the ArrayList iFrameList.
    
    for (int i=0; i<iFrameList.size(); i++) {
        if (iFrameList.get(i) instanceof TheThird) { 
          TheThird tt=(TheThird)iFrameList.get(i);
          tt.setAValueOfSomeSort(somethng);
        }
    }
    The framelist doesn't get the value of i, it returns the pointer to the
    element that it has stored at index # i. So, if you do like this:

    anArrayList.add(someObject);
    anArrayList.add(someOtherObject);

    then someOtherObject can be retrieved like:

    Object ob=anArrayList(1);

    and someObject can be retrieved like:

    Object ob=anArrayList(0);

    An ArrayList used like this returns pointers to instances of java.lang.Object.

    An instance is a variable created using the new operator,
    (just like the internalFrames you are using) and the statement:

    if (iFrameList.get(i) instanceof TheThird)

    gets the Object pointer at index i from the list and checks if it is a pointer to
    and instance of TheThird class.
    So, the for loop is getting all the TheThird internalFrames in the list and
    invokes the appropriate methods.

    I don't understand the value of ThThird, it equals a new iframelist?
    Nope, it is an already existing JInternalFrame that was stuffed into the
    list by the main frame when it created & displayed it.

    Remember, even though the list will continue to be stuffed with new internalframes
    after it has been passed around, it is still the same list, all over the place.
    Last edited by sjalle; 03-18-2005 at 08:48 AM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center