Results 1 to 5 of 5
  1. #1
    Nino V is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default How to read a table from another external frame?

    Hi,
    greetings to all!

    I have two frames in two different classes. In one of them there is a table, whose data I need in the second frame.

    Here is an extract of the code in Frame B which should read jTable1 in the external Frame A

    Java Code:
    TableModel TM8=jTable1.getModel(); // This doen's work. jTable1 is in an external Frame
      for (j=0; j<10; j++) {
         S=(""+TM8.getValueAt(0, j)).split("-");
         start = stop = 0;
         try {
          start = Integer.valueOf(S[0])-1;
          stop = Integer.valueOf(S[1]);
          System.println(s.substring(start, stop).trim(), i, j);
         } catch (NumberFormatException ex) {}
            catch (ArrayIndexOutOfBoundsException ex) {}
            catch (IndexOutOfBoundsException ex) {}
      }
    Thank you for your attention
    Nino
    Last edited by Fubarable; 07-29-2011 at 08:01 PM. Reason: [quote] tags changed to [code] tags to allow code to be readable

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    How do you create and display both gui's? Does one GUI hold a variable that refers to the other? What you need to do is pass a reference from the class that has the JTable into the class that needs it. You should give the class that has the JTable a public method that allows it to extract and return the information needed by the second class. Note that this is all basic Java and has little to do with GUI programming.

    Also note that I changed your quote tags into code tags so your posted code would be more readable.

  3. #3
    Nino V is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Hi Fubarable.
    Thank you for your quick reply.

    Quote Originally Posted by Fubarable View Post
    How do you create and display both gui's?
    I have two frame classes and I display the gui A, calling it by

    Java Code:
    frameA fA = new frameA();
    fA.setVisible(true);
    I populate the Table and close the window.

    Quote Originally Posted by Fubarable View Post
    Does one GUI hold a variable that refers to the other?
    No.
    Quote Originally Posted by Fubarable View Post
    What you need to do is pass a reference from the class that has the JTable into the class that needs it. You should give the class that has the JTable a public method that allows it to extract and return the information needed by the second class.
    I'm still a beginner . So could you explain me how to do that? or give me a reference reading.
    Quote Originally Posted by Fubarable View Post
    Note that this is all basic Java and has little to do with GUI programming.
    Do you mean that I did a mistake posting here? If so, please forgive me!
    Quote Originally Posted by Fubarable View Post
    Also note that I changed your quote tags into code tags so your posted code would be more readable.
    Thanks,

    Nino
    Last edited by Nino V; 07-29-2011 at 08:58 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Nino V View Post
    I have two frame classes and I display the gui A, calling it by

    Java Code:
    frameA fA = new frameA();
    fA.setVisible(true);
    I populate the Table and close the window.
    I'm still not sure exactly what is going on with your program. So one shows a JTable and then *poof* disappears? How does it get the information to create the JTable? Should it perhaps not be a JFrame but be displayed in a JDialog that is being displayed from the code in the other class?

    I'm still a beginner . So could you explain me how to do that? or give me a reference reading.
    You will want to Google on tutorials on Java reference variables. These are variables to pretty much anything that isn't a primitive (int, double, float, byte, long, char). Reference variables can be thought of almost as "pointers" because they refer or point to an object (or null if they are empty). If one object needs to call a public method of another object, it will need to hold a reference to the other object, often in a variable of the other object type. For instance if your main program needs to call nextInt() on a Random object, it often will hold a Random variable, e.g.,

    Java Code:
    Random myRandom;
    and have it refer to a Random object, done below by initializing an object of that type:
    Java Code:
    myRandom = new Random();
    and then it can use that variable and call methods off of it:

    Java Code:
    int randomInt = myRandom.nextInt(100);
    The same is true for any other class. So if one of your GUI classes needs to call methods off of another object, gui or otherwise, it will need to hold a reference to the other object.

    Creating GUI's in Java can be quite involved, and that is why it is always good to have a strong grounding in Java basics before trying to do Java GUI coding.

  5. #5
    Nino V is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your kind attention.

    I've found a workaround: I assigned the data I need - I don't need the whole table - to a global variable.

    Nino

Similar Threads

  1. Replies: 6
    Last Post: 06-03-2011, 11:01 AM
  2. read table data from javamail
    By jeev81 in forum Advanced Java
    Replies: 7
    Last Post: 05-19-2011, 05:48 PM
  3. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  4. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 11:40 AM
  5. Read/Write of components of frame window
    By Harish kumara M in forum AWT / Swing
    Replies: 0
    Last Post: 09-17-2008, 09:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •